Return to Archive

SAS Tip of the Month
November 2008

A month ago I had a programmer ask me how could she set all the character variables in a SAS dataset to missing. This follows a tip I gave some time ago referring to creating a dataset with no observations, avoiding the message indicating that there were initialized variables.

In SAS, the variables in a dataset are grouped by numeric (_NUMERIC_) and character (_CHAR_). Using this it is possible to set all the numeric or character variables to missing. The following code shows its use by setting the numeric variables to missing if the condition is met:

   141 *** Load our test data;
   142 data PregnancyTestResults;
   143    infile cards;
   144    input SubjectID $ 1-6 Sex $ 8 Tested Result;
   145    ** TESTED and RESULT have codes 1=Yes, 0=No;
   146    put (_all_) (=);
   147 cards;

   SubjectID=000101 Sex=M Tested=1 Result=.
   SubjectID=000102 Sex=F Tested=1 Result=0
   SubjectID=000103 Sex=F Tested=0 Result=.
   SubjectID=000104 Sex=M Tested=. Result=.
   SubjectID=000105 Sex=F Tested=1 Result=1
   153 ;
   154 run;
   NOTE: The data set WORK.PREGNANCYTESTRESULTS has 5 observations and 4 variables.
   155 *** If Male then set TESTED and RESULT to missing (both numeric);
   156 data PregnancyTestResultsModified;
   157    set PregnancyTestResults;
   158    if sex='M' then do;
   159       put 'WAR' 'NING: Male subject cannot have ' /
   160         @10 'a pregnancy test -- TESTED and RESULT ' /
   161         @10 'will be set to blank: ' /
   162         @10  (_all_) (=);
   163       call missing(of _numeric_);
   164    end;
   165 run;

   WARNING: Male subject cannot have
            a pregnancy test -- TESTED and RESULT
            will be set to blank:
            SubjectID=000101 Sex=M Tested=1 Result=.
   WARNING: Male subject cannot have
            a pregnancy test -- TESTED and RESULT
            will be set to blank:
            SubjectID=000104 Sex=M Tested=. Result=.
NOTE: There were 5 observations read from the data set WORK.PREGNANCYTESTRESULTS.
NOTE: The data set WORK.PREGNANCYTESTRESULTSMODIFIED has 5 observations and 4 variables.

   166 *** Put the dataset out to the SAS LOG, like a mini PROC PRINT;
   167 data _null_;
   168    set PregnancyTestResultsModified;
   169    if _n_=1 then put 'Pregnancy Test Results' /;
   170    put (_all_) (=);
   171 run;

   Pregnancy Test Results

   SubjectID=000101 Sex=M Tested=. Result=.
   SubjectID=000102 Sex=F Tested=1 Result=0
   SubjectID=000103 Sex=F Tested=0 Result=.
   SubjectID=000104 Sex=M Tested=. Result=.
   SubjectID=000105 Sex=F Tested=1 Result=1
NOTE: There were 5 observations read from the data set WORK.PREGNANCYTESTRESULTSMODIFIED.
________________________________
Updated November 1, 2008