We’re smarter together. Learn from this collection of community knowledge and add your expertise.

How to simulate the LEAD() function (opposite of LAG)

by Regular Contributor on ‎11-03-2015 11:43 AM (861 Views)

Several publishings and code examples have been published on LAG() function in SAS.  However, if one looks for a LEAD function, there is nothing documented in the language reference. However, there are some ideas published in SAS conference papers using POINT= and a few other methods.  This article shows a simple way to shift the values ahead  to get the leading values and also show the LAG() values beside the original value with an example.  

 

 

/* one way to simulate an opposite of a LEAD() function in SAS */

DATA in;
 INPUT X $1. @@; if X ne ' ';
DATALINES;
12345
;
run;

data leadx(rename=x=LEADX); 
set in;
if _n_>1;
run;

data out;
 set in;
 set leadx;
 LAGX=lag1(x);
run;

PROC PRINT DATA=out NOOBS; 
 var lagx X LEADX;
 TITLE 'SIMULATION OF A MISSING LEAD FUNCTION';
RUN; 

 

  

This code generated an output that is shown for the readers' reference: 

SIMULATION OF A MISSING LEAD FUNCTION
LAGX X LEADX
. 1 2
1 2 3
2 3 4
3 4 5

 

For other ideas to simulate LEAD() function, see:

 

Comments
by Super User
on ‎11-03-2015 11:50 AM

How would this be extended if you had a BY group variable?

by PROC Star
on ‎11-05-2015 11:41 AM
It's worth noting that this method results in the last observation (x=5) being excluded from the output dataset.
by Regular Contributor
‎11-05-2015 07:19 PM - edited ‎11-05-2015 07:20 PM

Have been caught up.... couldn't respond sooner. I apologize. ....

 

To Reeza's point, a bit of customization would be needed to tailor the By group variable requirement but should be doable..... 

 

To Quentin's observation - a good catch..Thanks for presenting it. I have a variation presented below, hope it helps. 

 

DATA in;
 INPUT X $1. @@; if X ne ' ';
DATALINES;
12345
;
run;
DATA NEWX;
	 SET IN END=LAST; 
	 LAGX=LAG(X); 
	 OUTPUT;
	 IF LAST 
	 THEN 
	 	DO; 
	 		LAGX=X;
	 		X=.; 
	 		OUTPUT;
	 	END;
RUN;
DATA NEWX ;
	SET NEWX;
	IF _N_ = 1 
	THEN 
		DELETE; 
	LAGX2=LAG(LAGX);
RUN;
PROC DATASETS NOPRINT;
     MODIFY NEWX;
     RENAME X=LEADX
     		LAGX=X
     		LAGX2=LAGX;
RUN;
PROC PRINT; 
RUN;

Generates the following output... just for reference...

 

Obs	LEADX	X	LAGX
1	2	1	 
2	3	2	1
3	4	3	2
4	5	4	3
5	.	5	4

 

Thanks again...!!!

 

Kannan Deivasigamani.

by Occasional Contributor JMS
on ‎11-26-2015 01:35 PM

/* prepare sample data */

data a; do x=1 to 5; output; end; run;

 

/* lead */
data b;
  set a end=end;
  leadX = x;
  x = lag(x);
  if _n_>1 then output;
  if end then do; x=leadx; leadx=.; output; end;
run;

 

by Regular Contributor
on ‎12-01-2015 03:43 PM

Read-ahead merge seems far easier.

 

ie:

 

 

data want;
  merge have(rename=x=lagx) have(firstobs=2 in=_base) have(firstobs=3 rename=x=leadx));
  if _base;
run;

 

Contributors
Your turn
Sign In!

Want to write an article? Sign in with your profile.