Help using Base SAS procedures

Dropping variables with null values

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 145
Accepted Solution

Dropping variables with null values

I have dataset with around 80-90 variables. I want to create dataset  in such way that I have to drop those variables which are null for all records. It should be dynamic in such way if new variable is added with null value it should be dropped.

Thanks


Accepted Solutions
Solution
‎09-04-2014 10:16 AM
Super User
Posts: 9,681

Re: Dropping variables with null values

My code is better ,shorter and faster than support . sas . com .  Smiley Happy

data class;
set sashelp.class;
call missing(age,name);
run;

proc sql noprint ;
 select cat('n(',strip(name),') as ',name)     into : list separated by ','
  from dictionary.columns
   where libname='WORK' and memname='CLASS';
create table temp as 
 select &list from class;
quit;
data _null_;
 set temp;
 length drop $ 4000; 
 array x{*} _numeric_;
 do i=1 to dim(x);
  if x{i}=0 then drop=catx(' ',drop,vname(x{i}));
 end;
 call symputx('drop',drop);
run;
data want;
 set class(drop= &drop );
run;



Xia Keshan

View solution in original post


All Replies
Super User
Super User
Posts: 7,401

Re: Dropping variables with null values

Q1 = Why 80-90 variables - consider normalizing your data!

Try the below example and modify for your particular data:

data work.have;
  length a b c d $20;
  a="GHGYT";b="£$"; output;
run;

proc sql;
  create table TO_PROCESS
  (
    NAME char(50),
    CNT num
  );
quit;

data tmp;
  set sashelp.vcolumn (where=(libname="WORK" and MEMNAME="HAVE"));
  call execute('proc sql;
                  insert into WORK.TO_PROCESS
                  set NAME="'||strip(NAME)||'",
                      CNT=(select count('||strip(NAME)||') from WORK.HAVE where '||strip(NAME)||' is not null);
                quit;');
run;
proc sql noprint;
  select  NAME
  into    Smiley Very HappyROP_LIST separated by ' '
  from    WORK.TO_PROCESS
  where   CNT=0;
quit;
data want (drop=&drop_list.);
  set have;
run;

Regular Contributor
Posts: 233

Re: Dropping variables with null values

Solution
‎09-04-2014 10:16 AM
Super User
Posts: 9,681

Re: Dropping variables with null values

My code is better ,shorter and faster than support . sas . com .  Smiley Happy

data class;
set sashelp.class;
call missing(age,name);
run;

proc sql noprint ;
 select cat('n(',strip(name),') as ',name)     into : list separated by ','
  from dictionary.columns
   where libname='WORK' and memname='CLASS';
create table temp as 
 select &list from class;
quit;
data _null_;
 set temp;
 length drop $ 4000; 
 array x{*} _numeric_;
 do i=1 to dim(x);
  if x{i}=0 then drop=catx(' ',drop,vname(x{i}));
 end;
 call symputx('drop',drop);
run;
data want;
 set class(drop= &drop );
run;



Xia Keshan

Respected Advisor
Posts: 3,124

Re: Dropping variables with null values

and whoever has the answer,

Is this another "undocumented" feature? I could NOT find the Docs addressing the use of function N() in your code.

1)In the Docs I can find, N( ) is for numeric.

2) N( ) is NOT a summary function for Proc SQL.

So ???

Thanks,

Haikuo

Respected Advisor
Posts: 3,124

Re: Dropping variables with null values

Never mind. Found it. I wasn't thorough enough.

SAS(R) 9.3 SQL Procedure User's Guide

Thanks,

Haikuo

Frequent Contributor
Posts: 125

Re: Dropping variables with null values

Hi @Ksharp, Is it possible to drop null variables without specifying by variable names because I have hundreds of them?

Super User
Posts: 9,681

Re: Dropping variables with null values

Check @MikeZeb 's solution.

Valued Guide
Posts: 765

Re: Dropping variables with null values

hi ... you got a very nice answer from Ksharp, but here's another idea that use PROC FREQ rather than PROC SQL to find the variables with all missing values ...

data class;

set sashelp.class;

call missing(age,name);

run;

ods output nlevels=nlvs (where=(nnonmisslevels eq 0));

proc freq data=class nlevels;

ods select nlevels;

run;

proc sql noprint ;

select tablevar into :drop separated by ' '  from nlvs;

quit;

data want;

set class(drop=&drop);

run;

ps  more on NLEVELS at 30867 - Modernizing Your SAS Code: PROC FREQ Applications and in Appendix C of  http://www.lexjansen.com/nesug/nesug11/ds/ds12.pdf

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 8 replies
  • 7461 views
  • 11 likes
  • 7 in conversation