Can anybody tell me how to drop a variable conditionally?

Reply
Contributor
Posts: 27

Can anybody tell me how to drop a variable conditionally?

For example, lets say there are 3 variables a b c.

If all the values of the variable a are missing then we have drop the variable from the data set.

data abc;

input a b c;

cards;

. 2 3

. 5 6

. 8 9

;

run;

Super User
Posts: 19,851

Re: Can anybody tell me how to drop a variable conditionally?

Posted in reply to bharathtuppad

You can't within a single datastep, but you can find your missing variables and then drop them in a second step.

See here how to find them:

Super Contributor
Posts: 1,636

Re: Can anybody tell me how to drop a variable conditionally?

Hi Reeza,

Time flies by, hard to believe that the discussion you provided happened almost a year ago.

Linlin

Trusted Advisor
Posts: 1,931

Re: Can anybody tell me how to drop a variable conditionally?

Posted in reply to bharathtuppad

You would need a macro to do this.

First, I would probably run proc means to see how many of each variable are present (i.e. non-missing)

Then, the macro could delete variables if none of the values were present. You probably need a datastep here to work inside the macro to delete the variables.

Respected Advisor
Posts: 3,156

Re: Can anybody tell me how to drop a variable conditionally?

Posted in reply to bharathtuppad

I concur with Fareeza, you can't achieve your goal in one data step, but you can somehow cheat it using call execute:

data abc;

input a b c;

cards;

. 2 3

. 5 6

. 8 9

;

data _null_;

  set abc end=last;

  array t a--c;

  array s s1-s3;

  length cat $100.;

  do i=1 to dim(t);

  s(i)+t(i);

if last and missing (s(i)) then cat=catx(' ',cat,vname(t(i)));

  end;

  if last then do;

  call symputx('cat',cat);

call execute('data want; set abc; drop &cat; run;');

end;

run;

Haikuo

Super User
Posts: 5,516

Re: Can anybody tell me how to drop a variable conditionally?

What a neat little trick, using single quotes within CALL EXECUTE to delay resolution of &CAT.  I may have to steal it!   Smiley Happy

Ask a Question
Discussion stats
  • 5 replies
  • 267 views
  • 3 likes
  • 6 in conversation