A dataset "want" has 4 variables car1, car2, car3, car4. I want to see if any of the 4 variables have values 5,6,7,8 in them.
data need;
set want;
if car: in (5,6,7,8) then output;
run;
Error at car:
ERROR 22-322: Syntax error, expecting one of the following: <, <=, =, >, >=, EQ, GE, GT, LE, LT, NE, NG, NL, ^=, ~=.
How do I use all four variables without writing each one of them in the code
Thank you
Here is one way:
data have;
input car1-car4;
cards;
1 3 4 9
1 5 2 2
5 2 3 7
;
data want;
set have;
array cars car:;
do i=1 to dim(cars);
if cars(i) in (5,6,7,8) then do;
output;
leave;
end;
end;
run;
Art, CEO, AnalystFinder.com
Here is another way:
data HAVE;
input CAR1-CAR4;
cards;
1 3 4 9
1 5 2 2
5 2 3 7
;
data WANT;
set HAVE;
MATCH= prxmatch('/[5678]/',catt(of CAR1-CAR4)) > 0 ;
run;
| CAR1 | CAR2 | CAR3 | CAR4 | MATCH |
|---|---|---|---|---|
| 1 | 3 | 4 | 9 | 0 |
| 1 | 5 | 2 | 2 | 1 |
| 5 | 2 | 3 | 7 | 1 |
it's a bit like taking a sledgehammer to crack a nut but I like that 🙂
I would have done nearly the same as @art297 demonstrated
data want;
set have;
array cars car:;
do over cars;
if cars in (5,6,7,8) then Flag=1;
end;
run;Cheers
- Cheers -
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.