Internal variables and internal order of variables

Last changed: 10 February 2021

Internal variables are variables that are available within SAS in special situations. Using these we can find the first or last observation of a group or use the row numbers in a data set that has no such variable included.

Row number 

Using the internal variable _N_ we can create a variable containing the row number:

data water1;
set water;
obsnr= _N_;
run;
  

First.variable and last.variable 

Other useful internal variable are first.variable and last.variable . We illustrate this by presenting the highest total nitrogen value for each month. First sort the data with respect to month and totN:

proc sort data=water1;
by month Ntot;
run;

data water2;
set water1;
by month;
if last.month=1 then output;
run;

proc print data=water2;
run;

First the data set water1 is sorted accoring to month and total nitrogen. In the data set water2 the last observation is outputted for each month, since the data is sorted according to total Nitrogen the last value is the largest value. When printing this data set we get the 12 monthly max values. The get the minimum value for each of the month we use first.month.
Since we here have biweekly data we can also compute the maximum value per year and month:

data water3;
set water1;
year=year(date);
run;

proc sort data=water3;
by year month Ntot;
run;

data water4;
set water3;
by year month;
if last.month=1 then output;
run;

proc print data=water4; run;

Internal ordering of variables

In all procedure and data steps where you need to give a list of variables you can use the internal ordering of the variable in SAS. When you read the data to SAS the program remembers the order of all variables. Assume you have a data set with variables Monday, Tuesday, Wednesday, Thursday and Friday in this order. You now want to compute the mean for each of the variables:

proc means data=thedataset;
var monday -- friday;
run;

You write -- instead of listing all variable. The variable inbetween Monday and Friday will automatically be added.

 


Contact