Sample 36199: Simulating a LEAD function by using the POINT= option on the SET statement
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 on a second SET statement, a subsequent observation can be accessed (read into the Program Data Vector) in addition to the current observation.
Since a variable name can only exist once in the Program Data Vector, the second SET statement would ordinarily result in the value of the variables from the first SET statement being over-written. 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.
This sample demonstrates how to read the subsequent observation in a data set in addition to the current observation during the same iteration of the DATA step. The value of a variable from the subsequent observation is subtracted from the same variable in the current observation to demonstrate this functionality. Conditional logic is included so as not to attempt to read a subsequent observation when the current observation is the last observation in the data set.
/*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;
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 . .
The SET statment provides random access to any observation by using the POINT= option to specify a particular observation number to be read
| 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: | 2009-09-16 15:24:59 |
| Date Created: | 2009-06-10 14:41:53 |
Operating System and Release Information
| 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 | | |