BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Sandra_L
Calcite | Level 5

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:

bsp.GIF

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

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

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):

  1. If the new IF conditions (regarding maxi[...]) are not met, the assignment statements for local[_n_] will not be executed. But in this case the corresponding assignment statements in the previous blocks of code have not been executed either, because the IF conditions there are stronger. (local[_n_] will be missing in this case.)
  2. If (either of) the new IF conditions are met, the pertinent assignment statement will be executed. But in this case the corresponding identical assignment statement in the previous blocks of code might have been executed already: in the case that the additional condition (involving lag(dec2) or jan1follow, respectively) was met.
  3. So, the only situation where the added code can influence the results is that (either of) the new IF conditions are met, whilst the corresponding additional condition was not satisfied.

View solution in original post

4 REPLIES 4
Sandra_L
Calcite | Level 5

o.k. i think i have to tell SAS to treat last.idgem different (not last.jahr)! but still it makes no difference

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

FreelanceReinh
Jade | Level 19

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):

  1. If the new IF conditions (regarding maxi[...]) are not met, the assignment statements for local[_n_] will not be executed. But in this case the corresponding assignment statements in the previous blocks of code have not been executed either, because the IF conditions there are stronger. (local[_n_] will be missing in this case.)
  2. If (either of) the new IF conditions are met, the pertinent assignment statement will be executed. But in this case the corresponding identical assignment statement in the previous blocks of code might have been executed already: in the case that the additional condition (involving lag(dec2) or jan1follow, respectively) was met.
  3. So, the only situation where the added code can influence the results is that (either of) the new IF conditions are met, whilst the corresponding additional condition was not satisfied.
Sandra_L
Calcite | Level 5
Hi Reinhard,
thak you for the lonx exlplication!
It was just the missing by statement, now it works nicely!
The local maximum for m5 in the situation m5<m6 is because m5 is the last value of the time series and m6 already belongs to a different station, so to make sure even if m5<m6 but m5>m4, that it declares m5 as max.
Thanks a lot,
Sandra

sas-innovate-2024.png

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.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 773 views
  • 0 likes
  • 3 in conversation