SAS Programming Professionals,
Did you know that special
SAS name lists can be very handy in simplifying everyday programming?
SAS has three special SAS name lists:
- _NUMERIC_ - specifies all of the numeric variables currently defined in the DATA step
- _CHARACTER_ - specifies all of the character variables currently defined in the DATA step
- _ALL_ - specifies all of the variables currently defined in the DATA step
You can use these special SAS name lists to streamline your
SAS programs. Consider this example:
/* Create test data with missing values */
data class;
set sashelp.class;
if mod(_n_,5)
= 0 then age=.;
if mod(_n_,7)
= 0 then do;
height
= .;
weight
= .;
end;
run;
/* Use variable list to populate an array and
look for missing values */
data _null_;
set class;
array
nums{*} _numeric_;
do I = 1
to dim(nums);
if
missing(nums{i}) then do;
x
= vname(nums{i});
put
x " = " nums{i} "
in observation number " _n_;
put
;
end;
end;
run;
The first DATA step merely sets up our test data by
making a copy of the SASHELP.CLASS data set and setting some of the numeric
variables to missing values.
The _NUMERIC_ special name list is used in the
second DATA step. We create an array
named NUMS and populate the array with all of the numeric variables (AGE,
WEIGHT, HEIGHT) by specifying _NUMERIC_.
This tells SAS that the array should be made up of all of the numeric
variables found in the observation.
Next, we use a DO loop to iterate through the numeric variables looking
for a missing value. (Note that we used the DIM function to
determine the number of elements in the array to use as the upper boundary of
our iterative DO group). If a
missing value is found for any numeric variable in the observation, we write a
note out to the log.
The PUT statements
in the DATA _NULL_ step print the following in the log:
Age = . in
observation number 5
Height = . in
observation number 7
Weight = . in
observation number 7
Age = . in
observation number 10
Height = . in
observation number 14
Weight = . in
observation number 14
Age = . in observation number 15
We could have easily done the same thing for character
variables using the _CHARACTER_ special names list. I don’t know about you, but I can never learn
enough techniques for checking my data for missing values!
Best of luck in all of your SAS endeavors!
----MMMMIIIIKKKKEEEE
(aka
Michael A. Raithel)
Excerpt from the book:
Did You Know That? Essential Hacks for Clever SAS Programmers
I plan to post each and every one of the hacks in the book to social media on a weekly basis. Please pass them along to colleagues who you know would benefit.
No comments:
Post a Comment