Hey guys,
i have a dataset where the observations are grouped by an ID (idgem) and each grout has observations for several years (jahr) ba halv month steps (jan1 - dec2) looks like this:
i wrote an array to find the local maximum (precipitaion data) in comparing each variable with the following and the previous variable.
the dec2 value is compared with the jan1 value of the following (by copying the column) year and jan1 is compared with dec2 of the previous year (by the lag function).
now i have the problem with the first an the last year within a group (idgem).
how do i tell SAS to compare dec2 of the last year just to dec1 of the same year and ignore jan1 of the following year.
i tried with an if last.jahr then option, but it doesn't work. my code looks like this (the corresponding part is marked green at the end of it):
data test4; set mani; array local [24]; /* kreiert eine neue Datenmatrix mit den Überschriften local1-local24, ohne Werte, Variablen wird ein Indice zugewiesen in [] */ do _n_ = 1 to 24; local[_n_] = "" ; end; array maxi [24] jan1 jan2 feb1 feb2 mar1 mar2 apr1 apr2 may1 may2 jun1 jun2 /* definiert quasi die Variablen des 'Quell-arrays' hier: maxi mit den Quelldaten */ 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; /* lag Funktion vergleicht mit dem Wert dec2 der vorhergehenden Observation */ 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; if (maxi [_n_] > maxi [_n_-1]) and (maxi [_n_] > jan1follow) then local [_n_] = maxi [_n_]; if (maxi [_n_] = maxi [_n_-1]) and (maxi [_n_] = jan1follow) then local [_n_] = maxi [_n_]; end; if (last.jahr) then do _n_ = 24; if (maxi [_n_] > maxi [_n_-1]) then local [_n_] = maxi [_n_]; if (maxi [_n_] = maxi [_n_-1]) then local [_n_] = maxi [_n_]; end; if first.jahr then do _n_ = 1; if (maxi [_n_] > maxi [_n_+1]) then local [_n_] = maxi [_n_]; if (maxi [_n_] = maxi [_n_+1]) then local [_n_] = maxi [_n_]; end; run;
there is no error message but also no difference in the results!
any ideas?
greets,
Sandra
Hallo Sandra,
Your programming logic, using first.idgem and last.idgem of course, looks correct to me (except that I'm not sure why in a situation like m4 = m5 < m6, value m5 should be regarded as a local maximum), but you have to insert a BY statement after the SET statement to make first./last.idgem work:
by idgem;
Whether the newly added (green) blocks of code then have an impact on the results, still depends on the data (in dataset MANI):
o.k. i think i have to tell SAS to treat last.idgem different (not last.jahr)! but still it makes no difference
A simple change to the structure of your data will make your life a lot easier:
IDGEM JAHR HALV_MONTH RESULT
0100100 1996 01JAN 1
0100100 1996 02JAN 0
...
Then you can simply group by whichever grouping you need.
Hallo Sandra,
Your programming logic, using first.idgem and last.idgem of course, looks correct to me (except that I'm not sure why in a situation like m4 = m5 < m6, value m5 should be regarded as a local maximum), but you have to insert a BY statement after the SET statement to make first./last.idgem work:
by idgem;
Whether the newly added (green) blocks of code then have an impact on the results, still depends on the data (in dataset MANI):
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.