Sample 24613: Delete numeric variables that have missing values on any observation
The sample code on the Full Code tab illustrates how to remove numeric variables that have at least one missing value on any observation of a SAS data set.
Note: To delete variables when all values are missing, see
Sample 24622, "Drop variables from a SASĀ® data set whose values are all missing."
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
/* Create sample data */
data sample;
input x y z;
datalines;
1 2 3
4 . 6
7 . .
;
run;
/* Use PROC MEANS to count the missing values for each variable */
proc means data=sample nmiss noprint;
output out=new(drop=_type_ _freq_) nmiss=;
run;
/* Capture the names of any variables where the count of missing values */
/* is greater than zero and put those variable names into macro variables. */
/* Another macro variable is created with the total count of the variables */
/* to be deleted. */
data _null_;
length name $35 ;
/* Read the data set created from the PROC MEANS */
set new;
/* Load all the numeric variables into the array TEST using the reserved */
/* word _NUMERIC_. This is helpful if you do not know the names of all */
/* the numeric variables in your data set. */
array test(*) _numeric_;
/* For every variable in the array TEST, check to see if its value is */
/* greater than zero. If so, increment a counter (N) and assign the */
/* variable name whose value is greater than zero into a new variable */
/* called NAME via the CALL routine VNAME. (The VNAME function could be */
/* used as well.) Use CALL SYMPUT to create a macro variable, VARn, to */
/* hold the value of NAME. These macro variables will be used in the */
/* following macro to build the DROP statement. Create the macro */
/* variable NUM using CALL SYMPUT. NUM will hold the final number of */
/* numeric variables that contained a missing value. NUM will be used */
/* as the upper bound of macro DO loop to build the DROP statement in the */
/* following macro. */
do i= 1 to dim(test);
if test(i) > 0 then do;
n+1;
call vname(test(i),name);
call symput('var'||left(put(n,8.)),name);
end;
call symput('num',left(put(n,8.)));
end;
run;
options mprint;
/* The macro do loop in the DROP= option writes the names of the */
/* variables to be dropped on the SET statement. */
%macro test;
data sample2;
set sample(drop= %do i = 1 %to #
&&var&i
%end;
);
run;
%mend test;
%test
proc print data=sample2;
run;
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
This sample shows how to remove numeric variables that have at least one missing value on any observation of a SAS
® data set.
Type: | Sample |
Topic: | SAS Reference ==> DATA Step Common Programming Tasks ==> Utilities
|
Date Modified: | 2006-12-23 03:02:56 |
Date Created: | 2004-09-30 14:08:58 |
Operating System and Release Information
SAS System | Base SAS | All | n/a | n/a |