Building models with SAS Enterprise Miner, SAS Factory Miner, SAS Visual Data Mining and Machine Learning or just with programming

drop variables from a data set conditionally

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 82
Accepted Solution

drop variables from a data set conditionally

In SAS, how can I do the equivalent of:

data a;

set b;

if (somecondition = true)

drop col2; /*col2 is a column included in b*/

run;


Accepted Solutions
Solution
‎07-06-2017 01:26 PM
Super User
Posts: 11,343

Re: drop variables from a data set conditionally

Posted in reply to eagles_dare13

Your example doesn't do what you think when I supply data and a variety of conditions. But there are no syntax errors so it creates dataset A identical to B. Run proc compare on the two.

HINT: see what happens with If condition then drop col2; <= This is a syntax error with a "Statement is not valid or it is used out of proper order.

Drop is more of a global statement and isn't conditional.

This isn't how it works but consider this scenario:

Suppose the first record doesn't meet your condition. Then col2 is written to the output dataset. The column will now be present for all records regardless.

You can exclude entire records from your output.

You can change the value of the variable for specific records.

The variable (column) is either present or not.

View solution in original post


All Replies
Super User
Posts: 19,815

Re: drop variables from a data set conditionally

Posted in reply to eagles_dare13

You can't easily.

What are you trying to do, perhaps there's another way?

Frequent Contributor
Posts: 82

Re: drop variables from a data set conditionally

Thanks.

This seems to work, but is it correct?

In SAS, how can I do the equivalent of:

data a;

set b;

if (somecondition = true) then do;

drop col2;

end;/*col2 is a column included in b*/

run;

Solution
‎07-06-2017 01:26 PM
Super User
Posts: 11,343

Re: drop variables from a data set conditionally

Posted in reply to eagles_dare13

Your example doesn't do what you think when I supply data and a variety of conditions. But there are no syntax errors so it creates dataset A identical to B. Run proc compare on the two.

HINT: see what happens with If condition then drop col2; <= This is a syntax error with a "Statement is not valid or it is used out of proper order.

Drop is more of a global statement and isn't conditional.

This isn't how it works but consider this scenario:

Suppose the first record doesn't meet your condition. Then col2 is written to the output dataset. The column will now be present for all records regardless.

You can exclude entire records from your output.

You can change the value of the variable for specific records.

The variable (column) is either present or not.

Super User
Posts: 10,035

Re: drop variables from a data set conditionally

Posted in reply to eagles_dare13

If I understood what you mean , you need two data step to get it. One is to pick up column name, the other is to drop column.

data _null_;

set class;

if name='Arthur' then call symputx('drop','age');

run;

data want;

set class;

drop &drop ;

run;

Xia Keshan

Super User
Posts: 10,035

Re: drop variables from a data set conditionally

Posted in reply to eagles_dare13

If I understood what you mean , you need two data step to get it. One is to pick up column name, the other is to drop column.

data _null_;

set class;

if name='Arthur' then call symputx('drop','age');

run;

data want;

set class;

drop &drop ;

run;

Xia Keshan

🔒 This topic is solved and locked.

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

Discussion stats
  • 5 replies
  • 5497 views
  • 0 likes
  • 4 in conversation