The concept of a LEAD function infers the ability to look ahead to a subsequent observation in a data set. This functionality can be obtained by using the SET statement's POINT= variable option.
By assigning an observation number to the numeric variable specified with the option, and then using the option in a second SET statement, a subsequent observation can be accessed (read into the program data vector, or PDV) in addition to the current observation.
Because a variable name can exist only once in the PDV, the second SET statement would ordinarily result in the value of the variables from the first SET statement being overwritten. To avoid this behavior, the KEEP= and RENAME= data set options should be used with the second SET statement to read in only the pertinent variables and to give them a new name.
When the POINT= option is used with a SET statement, and that specific SET statement is the only statement that reads an observation from a data set, then a STOP statement is required. The STOP statement lets the SAS® program know when to terminate the DATA step. In this sample, the initial SET statement (without the POINT= option) reads every observation sequentially. As such, SAS will terminate the DATA step when all the observations have been read via that SET statement.
See also SAS Note 24666, "Creating the equivalent of the LEAD function by comparing the value of a variable to its value in the next observation."
These sample files and code examples are provided by SAS Institute Inc. "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.
/*Sample data set*/
data one;
input var1 $ var2;
datalines;
A 10
B 22
C 5
D 41
E 33
;
run;
data two;
/*Add the END= option to facilitate checking for last observation.*/
set one end=finished;
/*Look ahead only if not on last observation.*/
if not finished then do;
/*Define or calculate the observation number to be read. In this example,*/
/*simply add one to the _N_ variable to read the very next observation.*/
pt = _N_ + 1;
/*Be sure to keep only pertinent variables and rename them.*/
set one (keep= var2 rename= (var2 = next_var2)) point=pt;
/*Subtract next value of var2 from current value of var2.*/
diff = var2 - next_var2;
end;
else next_var2 = .;
run;
proc print; run;
These sample files and code examples are provided by SAS Institute Inc. "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.
next_ Obs var1 var2 var2 diff 1 A 10 22 -12 2 B 22 5 17 3 C 5 41 -36 4 D 41 33 8 5 E 33 . .
Type: | Sample |
Topic: | SAS Reference ==> Statements ==> File-handling ==> SET ==> with POINT= SAS Reference ==> Statements ==> File-handling ==> SET SAS Reference ==> Functions ==> Special SAS Reference ==> DATA Step |
Date Modified: | 2020-04-20 12:14:35 |
Date Created: | 2009-06-10 14:41:53 |
Product Family | Product | Host | SAS Release | |
Starting | Ending | |||
SAS System | Base SAS | z/OS | ||
OpenVMS VAX | ||||
Microsoft® Windows® for 64-Bit Itanium-based Systems | ||||
Microsoft Windows Server 2003 Datacenter 64-bit Edition | ||||
Microsoft Windows Server 2003 Enterprise 64-bit Edition | ||||
Microsoft Windows XP 64-bit Edition | ||||
Microsoft® Windows® for x64 | ||||
OS/2 | ||||
Microsoft Windows 95/98 | ||||
Microsoft Windows 2000 Advanced Server | ||||
Microsoft Windows 2000 Datacenter Server | ||||
Microsoft Windows 2000 Server | ||||
Microsoft Windows 2000 Professional | ||||
Microsoft Windows NT Workstation | ||||
Microsoft Windows Server 2003 Datacenter Edition | ||||
Microsoft Windows Server 2003 Enterprise Edition | ||||
Microsoft Windows Server 2003 Standard Edition | ||||
Microsoft Windows Server 2008 | ||||
Microsoft Windows XP Professional | ||||
Windows Millennium Edition (Me) | ||||
Windows Vista | ||||
64-bit Enabled AIX | ||||
64-bit Enabled HP-UX | ||||
64-bit Enabled Solaris | ||||
ABI+ for Intel Architecture | ||||
AIX | ||||
HP-UX | ||||
HP-UX IPF | ||||
IRIX | ||||
Linux | ||||
Linux for x64 | ||||
Linux on Itanium | ||||
OpenVMS Alpha | ||||
OpenVMS on HP Integrity | ||||
Solaris | ||||
Solaris for x64 | ||||
Tru64 UNIX |