BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
rakeshvvv
Quartz | Level 8

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

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

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

8 REPLIES 8
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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    :DROP_LIST separated by ' '
  from    WORK.TO_PROCESS
  where   CNT=0;
quit;
data want (drop=&drop_list.);
  set have;
run;

Ksharp
Super User

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

Haikuo
Onyx | Level 15

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

Haikuo
Onyx | Level 15

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

SAS(R) 9.3 SQL Procedure User's Guide

Thanks,

Haikuo

Cruise
Ammonite | Level 13

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

Ksharp
Super User
Check @MikeZeb 's solution.

MikeZdeb
Rhodochrosite | Level 12

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

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 8 replies
  • 19893 views
  • 14 likes
  • 7 in conversation