

Selecting Last Observation within a Group Note : It returns first observation among values of a group (total 7 observations). The IF statement subsets data when IF is not used in conjunction with THEN or ELSE statements. It is very easy to do it with IF statement. Suppose you need to select only the first observation among a group of observations. Selecting First Observation within a Group (See ID = 4 in the above data for reference) When FIRST.variable = 1 and LAST.VARIABLE = 1, it means there is only a single value in the group. LAST.variable = 0 when an observation is not the last observation in each group values of variable ID. LAST.variable = 1 when an observation is the last observation in each group values of variable ID. In the program above, i have created First_ID and Last_ID variables.įIRST.variable = 1 when an observation is the first observation in each group values of variable ID.įIRST.variable = 0 when an observation is not the first observation in each group values of variable ID. To make them visible, we need to create two new variables. That means they are not visible in the newly created data set.


It is required to sort the data before using first. Use PROC SORT to sort the data set by ID. Copy the program below and paste it into SAS program editor and run/submit it. The program below creates the dataset in SAS. The variable ID is a grouping variable and it contains duplicates. Suppose you have a dataset consisting 3 variables and 12 observations. Note : Data set must be sorted BY group before applying FIRST. 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.
