Sentry Page Protection
Data Manipulation [15-18]
Variable _n_ and _error_
The variable _n_ and _error_ are two automatic variables that exist in the data step.
Example
The TEMP data set contains two variables: NUM1 and NUM2.
Variable _n_
Variable _n_ shows the observation number in the data set.
Similar to the variable "a" from the (in=a) option, variable _n_ does not exist in the data set.
To see the variable _n_, you must copy _n_ into a variable that is visible in the data set.
Example
Data Temp2;
Set Temp;
obsnum = _n_;
Run;
The variable obsnum is the carbon copy of the variable _n_.
It shows the observation number of the data set.
In this example, it shows the number from 1 to 11, which is the observation no. for each observation.
Note: the application of _n_ will be explained in later modules.
Variable _error_
The variable _error_ is another automatic variable built in the data step.
It identifies the observation with error.
Example
Data Temp3;
Set Temp;
Num3 = Num1 / Num2;
obserr = _error_;
Run;
In this example, we attempt to divide the variable NUM1 by NUM2.
However, NUM2 is zero (0) in one of the observation. This causes an error from the division.
The variable _error_ flags the observation as "1" when there is an error, and "0" when there is not.
You can identify the problematic observation by simply using the _error_ variable.
Exercise
Locate the CARS data set from the SASHelp library.
Create a data set that contains the top 3 cars with the highest horsepower.
Locate the CARS data set from the SASHelp library.
Create a data set that contains the top 3 cars with the highest horsepower.
Need some help?
HINT:
You may want to sort the data set to identify the cars with the highest horsepower.
SOLUTION:
Proc Sort Data=sashelp.cars Out=cars;
By Descending Horsepower;
Run;
Data Cars2;
Set Cars;
if _n_ <=3;
Run;
Fill out my online form.