Sandra L.
Sandra L.

Reputation: 3

sas local maximum time series

I have a time series of precipitation data for 30 years (jahre) for a lot of stations (idgem). Each year has a value for a half month step like this:

idgem year jan1 jan2 feb1 feb2 ... dec 1 dec 2
1 1960 20 22 25 10 ... 32 30
1 1961 22 25 30 20 ... 30 25

[![example data][1]][1]

Now I want to find out the local maxima in the way that the values are kept, if they are bigger than the previous and the next. I tried it like this

data test3;
set test2;
 array maxi [24] jan1 jan2 feb1 feb2 mar1 mar2 apr1 apr2 may1 may2 jun1 
                jun2 jul1 jul2 aug1 aug2 sep1 sep2 oct1 oct2 nov1 nov2 dec1 dec2;
 do i=1 to 24;
 if maxi [i-1] < maxi [i]> maxi [i+1] then maxi [i] = maxi [i];
 else maxi [i]=.;
end;
run;

I always get an error message "Array subscript out of range ".

Any ideas how I can tell SAS to compare wiht the previous and next value and keep the compared value if it is bigger? And how do I compare the value for example of jan1 of 1961 to dec2 of 1960?

[1]: https://i.sstatic.net/CId0x.gif
i got help and now i have this solution

data test3;
 set test2;
  array local [24]; 
  do _n_ = 1 to 24;
  local[_n_] = "" ;
 end;
 array maxi [24] jan1 jan2 feb1 feb2 mar1 mar2 apr1 apr2 may1 may2 jun1 
      jun2 jul1 jul2 aug1 aug2 sep1 sep2 oct1 oct2 nov1 nov2 dec1 dec2;
 do _n_ = 2 to 23;
  if (maxi [_n_-1] < maxi [_n_]) and (maxi [_n_]> maxi [_n_+1]) then 
   local [_n_] = maxi [_n_];
  if maxi [_n_] = maxi [_n_-1] then local [_n_] = maxi [_n_];
  if maxi [_n_] = maxi [_n_+1] then local [_n_] = maxi [_n_];
 end;
 do _n_ = 1; 
  if (maxi [_n_] > maxi [_n_+1]) and (maxi [_n_] > lag(dec2)) then local 
    [_n_] = maxi [_n_];
  if (maxi [_n_] = maxi [_n_+1]) and (maxi [_n_] = lag(dec2)) then local 
    [_n_] = maxi [_n_];
 end;
 do _n_ = 24; ???
run;


with the LOG FUNKTION i can compare jan1 with the value dec2 from the previous observation. but how can i compare dec2 with jan1 of the following observation? any ideas?

Upvotes: 0

Views: 79

Answers (1)

Tom
Tom

Reputation: 51621

So you could just use the LAG() function to find the previous version of the last element (DEC2) and use a "lead" technique to get the next version of the first element. So your MAXI array can now be made with 26 elements and new LOCAL maximum array will have 24 elements. That will prevent indexing outside of the array bounds.

data test3;
  set test2;
  set test2 (firstobs=2 keep=jan1 rename=(jan1=next)) test2(obs=1 drop=_all_);
  array local [24]; 
  previous = lag(dec2);
  array maxi 
    previous
    jan1 jan2 feb1 feb2 mar1 mar2 apr1 apr2 may1 may2 jun1 jun2 
    jul1 jul2 aug1 aug2 sep1 sep2 oct1 oct2 nov1 nov2 dec1 dec2
    next
  ;
  do i=1 to dim(local);
    local(i)= max(maxi(i),maxi(i+1),maxi(i+2));
  end;
  drop i;
run;

Upvotes: 0

Related Questions