Hi,
I have a large dataset with many different column names. I want to have an if statement that filters for all columns.
For example, I have a table:
row_number | Name1 | Name2 | Name3 | Name4 | Name5 |
1 | 3 | 4 | 9 | 1 | XXX |
2 | 7 | XXX | 2 | 9 | 5 |
3 | 2 | 2 | 2 | 8 | 6 |
4 | 7 | 2 | 1 | XXX | 3 |
5 | 10 | 9 | 3 | 5 | 2 |
I want the output to be rows with the value XXX in it.
row_number | Name1 | Name2 | Name3 | Name4 | Name5 |
1 | 3 | 4 | 9 | 1 | XXX |
2 | 7 | XXX | 2 | 9 | 5 |
4 | 7 | 2 | 1 | XXX | 3 |
My table name is not uniform like that (not like 1,2,3,4...). They are just english words.
Thank you.
Can i assume the vars name1--name6 are all char vars?
if yes,
data have;
input row_number (Name1 Name2 Name3 Name4 Name5) ($);
cards;
1 3 4 9 1 XXX
2 7 XXX 2 9 5
3 2 2 2 8 6
4 7 2 1 XXX 3
5 10 9 3 5 2
;
data want;
set have;
array t name1--name5;
if 'XXX' in t;
run;
the double -- takes care of the variable list even it is not sequential list 1-6 meaning any variable name from left to right
or it the vars are mix of char and num, grouping the array var on all chars and filtering it should work fine too like
data want;
set have;
array t _character_;
if 'XXX' in t;
run;
WHICHC()
if whichc('XXX', of name1--name5);
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.
Ready to level-up your skills? Choose your own adventure.