Quantcast
Channel: ListenData
Viewing all articles
Browse latest Browse all 425

First. and Last. Variables

$
0
0
First. and Last. Variables

Two automatic variables, FIRST.variable and LAST.variable can be created if we use a by statement along with a set statement in a data step.

FIRST.VARIABLE assigns the value of 1 for the first observation in a BY group and the value of 0 for all other observations in the BY group.

LAST.VARIABLE assigns the value of 1 for the last observation in a BY group and the value of 0 for all other observations in the BY group.

Data set must be in sort order.

SAMPLE DATA SET

IDNameScore
1David45
1David74
2Sam45
2Ram54
3Bane87
3Mary92
3Bane87
4Dane23
5Jenny87
5Ken87
6Simran63
8Priya72

Create this data set in SAS


Use PROC SORT to sort the data set by ID.


Note : FIRST./LAST. variables are temporary variables. That means they are not visible in the newly created data set. To make them visible, we need to create two new variables. In the program above, i have created First_ID and Last_ID variables.



STORING UNIQUE AND DUPLICATE VALUES




-The DATA statement creates two temporary SAS data sets: DUPLICATES AND UNIQUE.

-The SET statement reads observations from data set READIN 

-The BY statement tells SAS to process observations by ID. Variables FIRST.ID and LAST.ID are created. 

-The observations where both First_ID and Last_ID are not equal to 1 go to the newly created data set DUPLICATES.

-The ELSE statement outputs all other observations (i.e., where First_ID and Last_ID equal to 1) to data set UNIQUE.

Viewing all articles
Browse latest Browse all 425

Trending Articles