problems with the lag() function

Reply
N/A
Posts: 0

problems with the lag() function

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
New Contributor
Posts: 3

Re: problems with the lag() function

Hi Jean-Sebastien,

Try something like this (not tested):

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

Hope this helps,
Nancy
Occasional Contributor
Posts: 10

Re: problems with the lag() function

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 ;
Ask a Question
Discussion stats
  • 2 replies
  • 376 views
  • 0 likes
  • 3 in conversation