# Choosing Every nth Observation from a SAS Data Set - SAS Programming

Here is an example where you find a useful application of the MOD function. Most people would skip right over this function when reading their SAS manuals, hardly remembering that they once knew what a modulus was (eleventh grade math?).To review, a number n modulo m, is the remainder when n is divided by m.For instance,5 mod 3 is 2 (when you divide 5 by 3, you get 1 with a remainder of 2).And 17 mod 3 is also 2 (3 goes into 17 five times with a remainder of 2). How is this useful? Suppose you want to select every third observation from a SAS data set.The very simple code following, using the MOD function, does just that:

Example

DATA THIRD;
SET OLD;
IF MOD <_N_,3)=1;
RON;

The function MOD(n,m) (read n mod m) is used here with the automatic SAS System variable _N_.This variable is automatically created when a SAS data set is built and is incremented by 1 for each iteration of the DATA step. _N_ is loosely referred to as the observation counter and is used here as the first argument with 3 (the selection criteria) as the second. MOD (_N_, 3) is equal to 1 when _N_ is 1, 4, 7, etc. (i.e. every third record beginning with the first observation).

Caution:Don't automatically accept _N_ as a true observation counter in all DATA steps (that's why we said loosely). _N_ is incremented by 1 at each iteration of the DATA step. DATA steps that contain loops and OUTPUT statements may cause the relationship of the observation number and the internal variable _N_ to fall apart. This is not a problem with the system—it's the way it was designed to work. You may very well want to include code such as N+1; in a program to create your own observation counter.

Suppose you want to convert pounds to kilograms and inches to centimeters, and you want the results rounded to the nearest kilogram and the nearest tenth of a centimeter (millimeter) respectively. You can use the ROUND function to accomplish this task.

SAS Programming Topics