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.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

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
  • 2 replies
  • 3639 views
  • 4 likes
  • 2 in conversation