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

I would like to subset my dataset to include only records where one or more of a set of variable meet a certain value. 

My code looks like: 

 

data work.test2

data work.test

if VAR1 | VAR 2 | VAR3 ... | VAR 30 = 'VALUE';

run;

 

How do I condense that to either VAR1-VAR30 = ' VALUE' or VARn = 'VALUE'? Using (keep=VAR1-VAR30) does not seem applicable in this case. 

 

Thank you 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Your posted code might be valid syntax, but it does not do what you claim to want.

 

There is special syntax for using IN with an array that could help.

data work.test2 ;
  set data work.test;
  array vars var1-var30;
  if 'VALUE' in vars ;
run;

Or you could use the WHICHC() function. That will actually return which variable number was the first one found to have the value. You don't need an ARRAY for that. A value of 0 (which SAS will treat as FALSE when evaluating as boolean) will indicate that is was not found.

data work.test2 ;
  set data work.test;
  if whichc('VALUE',of var1-var30);
run;

 

View solution in original post

2 REPLIES 2
Tom
Super User Tom
Super User

Your posted code might be valid syntax, but it does not do what you claim to want.

 

There is special syntax for using IN with an array that could help.

data work.test2 ;
  set data work.test;
  array vars var1-var30;
  if 'VALUE' in vars ;
run;

Or you could use the WHICHC() function. That will actually return which variable number was the first one found to have the value. You don't need an ARRAY for that. A value of 0 (which SAS will treat as FALSE when evaluating as boolean) will indicate that is was not found.

data work.test2 ;
  set data work.test;
  if whichc('VALUE',of var1-var30);
run;

 

gekco
Fluorite | Level 6
Thanks! WHICHC() works perfectly for this.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 3774 views
  • 4 likes
  • 2 in conversation