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 ;

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!

New Learning Events in April

 

Join us for two new fee-based courses: Administrative Healthcare Data and SAS via Live Web Monday-Thursday, April 24-27 from 1:00 to 4:30 PM ET each day. And Administrative Healthcare Data and SAS: Hands-On Programming Workshop via Live Web on Friday, April 28 from 9:00 AM to 5:00 PM ET.

LEARN MORE

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