How to drop variable that might not exist in given dataset

Reply
Frequent Contributor
Posts: 90

How to drop variable that might not exist in given dataset

I would like to write something like this:

 

data want;

set have (drop=pk_: fk_: bk_:);

run;


 

The problem occurs when there is no variable that starts with fk_, is it possible to set it conditionally (with out necessity to write macro that checks whether variable exists in a given file)? Thanks

Super User
Posts: 21,527

Re: How to drop variable that might not exist in given dataset

Posted in reply to Uknown_user
You can consider turning off the errors generated by the DROP/KEEP statements not listing, see the SAS Options list for the specific command. If you include that statement within the data step it gives a NOTE not an error. Or, as you noted, you can use macro logic. Or, you could use call execute/DO SUBL to generate your code conditionally based on the variables - similar to a macro.
Super User
Posts: 5,987

Re: How to drop variable that might not exist in given dataset

Posted in reply to Uknown_user

You can insert this line before your DATA step:

 

options dkricond=NoWarn;

 

Then after the DATA step, switch back to the one that you prefer:

 

options dkricond=Warn;

options dkricond=Error;

 

Think of this as Drop Keep Rename on Input Condition.

Ask a Question
Discussion stats
  • 2 replies
  • 101 views
  • 0 likes
  • 3 in conversation