5.4 - Programming For Missing Values

5.4 - Programming For Missing Values

Example 5.4

This if-then-else stuff seems easy enough! Let's try creating another status variable for our grades data set, but this time let's allow its value to depend on the value of the student's fourth exam (e4) rather than the value of the student's first exam (e1):

DATA grades;
	input name $ 1-15 e1 e2 e3 e4 p1 f1;
	* if the fourth exam is less than 65 indicate failed;
	if (e4 < 65) then status = 'Failed';
	* otherwise indicate passed;
	else status = 'Passed';
	DATALINES;
Alexander Smith  78 82 86 69  97 80
John Simon       88 72 86  . 100 85
Patricia Jones   98 92 92 99  99 93
Jack Benedict    54 63 71 49  82 69
Rene Porter     100 62 88 74  98 92
;
RUN;

PROC PRINT data = grades;
	var name e4 status;
RUN;

Launch and run the SAS program. Review the output from the PRINT procedure to convince yourself that the values of the character variable status have been assigned correctly. Oooops! What happened?! SAS assigned a "Failed" status to John Simon, seemingly because his exam score is missing. That's certainly one way of assigning grades, but it's probably not going to make John very happy.

The important point to remember is that SAS considers a missing value to be smaller than any other numerical value. That is, a missing value (.) is considered smaller than 12, smaller than 183, and even smaller than 0. Thus, we should stick to another good programming habit: always program for missing values. Say it to yourself over and over and over again ... always program for missing values ... until you remember it. It may save you alot of trouble down the road.

Example 5.5

Now, let's look at our SAS program again, but this time having written the program so that SAS is told to assign status a missing value (a blank space ' ' since it is a character variable) if e4 is missing (a period . since it is a numeric variable):

DATA grades;
    length status $ 6;
	input name $ 1-15 e1 e2 e3 e4 p1 f1;
	* if the fourth exam is missing indicate missing;
	* else if the fourth exam is less than 65 indicate failed;
	* otherwise indicate passed;
	     if (e4 = .)  then status = ' ';
	else if (e4 < 65) then status = 'Failed';
	else                   status = 'Passed';
	DATALINES;
Alexander Smith  78 82 86 69  97 80
John Simon       88 72 86  . 100 85
Patricia Jones   98 92 92 99  99 93
Jack Benedict    54 63 71 49  82 69
Rene Porter     100 62 88 74  98 92
;
RUN;

PROC PRINT data = grades;
	var name e4 status;
RUN;

Launch and run the SAS program. Review the output from the PRINTprocedure to convince yourself that this time the values of the character variable status really have been assigned correctly. Note that this program also illustrates the use of more than one ELSE statement. You can use as many ELSE statements as necessary, as long as they are attached to a preceding IF-THEN statement.


Legend
[1]Link
Has Tooltip/Popover
 Toggleable Visibility