turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Communities Library
- /
- How to simulate the LEAD() function (opposite of L...

- Article History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Email to a Friend
- Printer Friendly Page
- Report Inappropriate Content

Labels:

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:

- Can you Lag and Lead at the same time? If using the SAS DATA step, yes you can
- Sample 36199: Simulating a LEAD function using the POINT= option on the SET statement
- Sample 24666: Simulating the LEAD function by comparing the value of a variable to its value in the ...
- Sample 24694: Obtaining the previous value of a variable within a BY-Group

5 Comments (5 New)

Hide Comments

Comments