Sample 32470: Using macro logic to dynamically delete variables that have only missing values
Overview
Identify and remove any variables, character or numeric, that have only missing values.
NOTE: This sample code is for SAS 9 releases and above.
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.
/** SAS 9 and greater **/
/** Drop all variables that contain only missing values **/
options mprint;
data test;
input char1 $ char2 $ num1 num2;
datalines;
. . . .
. . 5 .
. . . .
. A . .
;
%macro lst(dsn);
%let x=;
%let dsid=%sysfunc(open(&dsn));
%let cnt=%sysfunc(attrn(&dsid,nvars));
%do i = 1 %to &cnt;
%let x=&x %sysfunc(varname(&dsid,&i));
%end;
%let rc=%sysfunc(close(&dsid));
ods output onewayfreqs(match_all=datasetnames);
run;
proc freq data=&dsn;
tables &x;
run;
ods output close;
run;
data final;
set &datasetnames;
run;
data droppings; set final; by table;
if first.table and frequency=._;
run;
proc sql noprint;
select table
into :dropvar separated by ' '
from droppings;
quit;
data &dsn;
set &dsn;
drop %sysfunc(tranwrd(&dropvar,Table,%str()));
run;
%mend lst;
%lst(test)
/* PRINT the results */
proc print;
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.
Obs char2 num1
1 .
2 5
3 .
4 A .
Identify and remove any variables, character or numeric, that have only missing values.
| Type: | Sample |
| Topic: | SAS Reference ==> Macro
|
| Date Modified: | 2009-02-16 16:39:17 |
| Date Created: | 2008-06-17 15:23:13 |
Operating System and Release Information
| SAS System | Base SAS | z/OS | 9 TS M0 | |
| Microsoft® Windows® for 64-Bit Itanium-based Systems | 9 TS M0 | |
| Microsoft Windows Server 2003 Datacenter 64-bit Edition | 9 TS M0 | |
| Microsoft Windows Server 2003 Enterprise 64-bit Edition | 9 TS M0 | |
| Microsoft Windows 2000 Advanced Server | 9 TS M0 | |
| Microsoft Windows 2000 Datacenter Server | 9 TS M0 | |
| Microsoft Windows 2000 Server | 9 TS M0 | |
| Microsoft Windows 2000 Professional | 9 TS M0 | |
| Microsoft Windows NT Workstation | 9 TS M0 | |
| Microsoft Windows Server 2003 Datacenter Edition | 9 TS M0 | |
| Microsoft Windows Server 2003 Enterprise Edition | 9 TS M0 | |
| Microsoft Windows Server 2003 Standard Edition | 9 TS M0 | |
| Microsoft Windows XP Professional | 9 TS M0 | |
| 64-bit Enabled AIX | 9 TS M0 | |
| 64-bit Enabled HP-UX | 9 TS M0 | |
| 64-bit Enabled Solaris | 9 TS M0 | |
| HP-UX IPF | 9 TS M0 | |
| Linux | 9 TS M0 | |
| OpenVMS Alpha | 9 TS M0 | |
| Tru64 UNIX | 9 TS M0 | |