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

Good morning.

 

The below dataset I have all but the Wanted column. That is what I require. So for every first.reference the Wanted should be 1. For the next reference, if it is the same value as the reference in the line above, check the FLAG value. If it is the same as the line above, it should be the same as the Wanted value above. If the FLAG value is different from the line above (i.e. from 0 to 1 or 1 to 0), the WANTED value should be lag(Wanted )+ 1. DO that until the next Reference and if it is the first reference start again.

 

I have tried so many pieces of code. The lag creates a blank. Also the lag when using a loop seems to keep an old version of the lag. I am just unsure as to where to start...

 

Ta,

Arch

 

Month_AMonth_BReferenceDate_CFLAGWanted
Nov-23Jan-24A2/11/202311
Oct-22Jul-23B3/10/202211
Nov-23Jan-24B3/10/202202
May-23Jan-24C15/05/202311
Feb-23Sep-23D9/02/202311
Oct-23Jan-24D9/02/202302
May-23Dec-23E30/05/202311
Mar-23Mar-23F14/03/202311
Apr-23Jun-23F27/04/202311
Jul-23Jan-24F27/04/202302
May-23May-23G31/05/202311
Mar-23Apr-23H6/03/202311
May-23Nov-23H6/03/202302
Dec-23Jan-24H5/12/202313
1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

No need to use LAG().  Just tell SAS to treat FLAG as one of the BY variables.  Make sure to include the NOTSORTED keyword since the observations are not actually sorted.

data want;
  set have;
  by reference flag notsorted;
  if first.reference then WANT=0;
  WANT + first.flag;
run;

proc print;
 var reference flag wanted want ;
run;

Result

Obs    Reference    FLAG    Wanted    WANT

  1        A          1        1        1
  2        B          1        1        1
  3        B          0        2        2
  4        C          1        1        1
  5        D          1        1        1
  6        D          0        2        2
  7        E          1        1        1
  8        F          1        1        1
  9        F          1        1        1
 10        F          0        2        2
 11        G          1        1        1
 12        H          1        1        1
 13        H          0        2        2
 14        H          1        3        3

If you really did want to use LAG() you could do that also.

 

For example if you are not positive if the observations are really in chronological order you could include DATE_C in the BY statement so SAS will check.   Then use LAG() (or since it is numeric perhaps DIF()) to find when the FLAG variable changed.  Make sure to execute the LAG() function on every observations so you are really comparing to the previous observations value.

data want;
  set have;
  by reference date_c;
  if first.reference then WANT=0;
  WANT + (first.reference or lag(flag) ne flag);
run;

 

View solution in original post

2 REPLIES 2
Tom
Super User Tom
Super User

No need to use LAG().  Just tell SAS to treat FLAG as one of the BY variables.  Make sure to include the NOTSORTED keyword since the observations are not actually sorted.

data want;
  set have;
  by reference flag notsorted;
  if first.reference then WANT=0;
  WANT + first.flag;
run;

proc print;
 var reference flag wanted want ;
run;

Result

Obs    Reference    FLAG    Wanted    WANT

  1        A          1        1        1
  2        B          1        1        1
  3        B          0        2        2
  4        C          1        1        1
  5        D          1        1        1
  6        D          0        2        2
  7        E          1        1        1
  8        F          1        1        1
  9        F          1        1        1
 10        F          0        2        2
 11        G          1        1        1
 12        H          1        1        1
 13        H          0        2        2
 14        H          1        3        3

If you really did want to use LAG() you could do that also.

 

For example if you are not positive if the observations are really in chronological order you could include DATE_C in the BY statement so SAS will check.   Then use LAG() (or since it is numeric perhaps DIF()) to find when the FLAG variable changed.  Make sure to execute the LAG() function on every observations so you are really comparing to the previous observations value.

data want;
  set have;
  by reference date_c;
  if first.reference then WANT=0;
  WANT + (first.reference or lag(flag) ne flag);
run;

 

Archenar
Calcite | Level 5
Tom - thanks for your time. You are right - I missed that BY should have had the other variable in there too. Works like a dream. Thanks so much.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 2 replies
  • 337 views
  • 3 likes
  • 2 in conversation