Creating a Subject Number in the DATA Step: A Common Mistake - SAS Programming

Suppose you have a source of raw data which consists of one record per subject with scores on two variables for each subject. The records are not identified by subject number,but you want to print out each subject by sequential number with the accompanying data.You, therefore, want a program to generate incremental subject numbers.The following program almost does the job

Example

******* INCORRECT PROGRAM *******;
DATA NOGOOP;
SUBJECT«SUBJECT+1;
INPUT SCORE1 SCORE2?
DATAklNES;
3 4
5 6
7 8
/
PROC PRINT DATA*NQGQODj
TITLE1 'Incorrect Program';
RON?

The resulting output shows a data set that contains three observations, but they have missing values (.) for subject numbers.

Output from Example - Creating a Subject Number in the DATA Step: A Common Mistake

Output from Example  - Creating a Subject Number in the DATA Step: A Common Mistake
This program did not perform as desired for the following reason: For each iteration of the DATA step, all variables, including SUBJECT, were initialized to missing (.)• Values for SCORE1 and SCORE2 were then read in, but no values were read in for SUBJECT.When you add 1 to a missing value, as you do in the following line, the result is also missing :

SUBJECT=SUBJECT+1;

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

SAS Programming Topics