BookmarkSubscribeRSS Feed
deleted_user
Not applicable
Hi,
here's my problem: I want to compare an observation in the second row with an observation in the first row. E.g :
fruit Fruit1 (I want to create that variable)
1 orange apple
2 apple banana
3 banana

I know with the LAG() function you can compare with the next row. Is there any function doing the same thing but with the previous row??

thanks,

Jean-Sebastien
2 REPLIES 2
Nancy_B
Calcite | Level 5
Hi Jean-Sebastien,

Try something like this (not tested):

data new;
merge fruits fruits (firstobs=2 rename=(fruit=fruit1));
run;

Hope this helps,
Nancy
TobyDunn_hotmail_com
Fluorite | Level 6
In SAS there is no lead function. You could reverse sort your data and then use the lag function or you could create your own code to mimic the lead function:


Eaxample:


Data Have ;
Infile Cards ;
Input ID Fruit $ ;
Cards ;
1 Apple
2 Orange
3 Banana
;
Run ;

Data Need ;
Set Have NObs = NObs End = EOF ;

MyPoint = _N_ + 1 ;

If ( MyPoint <= NObs ) Then Do ;
Set Have ( Keep = Fruit Rename = ( Fruit = OldFruit ) ) Point = MyPoint ;
End ;

If Eof Then Call Missing( OldFruit ) ;

Run ;

Proc Print
Data = Need ;
Run ;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

Health and Life Sciences Learning

 

Need courses to help you with SAS Life Sciences Analytics Framework, SAS Health Cohort Builder, or other topics? Check out the Health and Life Sciences learning path for all of the offerings.

LEARN MORE

Discussion stats
  • 2 replies
  • 1582 views
  • 0 likes
  • 3 in conversation