BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Melk
Lapis Lazuli | Level 10

I am trying to calculate the difference of a variable Y from the previous visit by subject and by limb.

 

Sample data:

 

Y     ID    Limb   Visit

1      1      R        1

3      1      R        2

4      1      R        3

5      1      L        1

5      1      L        2

3      1      L        3

2      2      R        1

3      2      R        2

4      2      R        3

4      2      L        1

7      2      L        2

1      2      L        3

 

 

I am using this code:

proc sort data=dat; by id limb visit;
run;

data dat;
    set dat;
    by id limb;
    retain r_days;
    r_days = lag(age);
    if not first.id then do;
        days = age - r_days;
    end;
    drop r_days;
run;

 

But it is not doing this by LIMB, only by ID. Cant figure out what is wrong with my code.

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

 if not first.id <- change this to limb, not ID. Your FIRST. should be at the lowest level you're looking to analyze.

View solution in original post

3 REPLIES 3
Reeza
Super User

 if not first.id <- change this to limb, not ID. Your FIRST. should be at the lowest level you're looking to analyze.

Melk
Lapis Lazuli | Level 10
That was simple- thanks!
ballardw
Super User

Since your example data doesn't include AGE (hint) as a variable and you don't show an expected result (hint) this is a stab at an alternate approach.

 

data dat;
    set dat;
    by id limb;
    days=dif(age);
    if first.limb then days=.;
run;

People often forget there is a complementary function that behaves similar to LAG called DIF that does the calculation you are looking for.

 

I am not sure why you think you need to retain r_days as you never use the value from the previous record.

Of course you may have actually want to calculate the cumulative days from the first value of limb which would look more like

data dat;
   set dat;
   by id limb;
   retain r_days;
   if first.limb then r_days=age;
   else days= age-r_days;
   drop r_days;
run;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 3 replies
  • 6424 views
  • 1 like
  • 3 in conversation