BookmarkSubscribeRSS Feed
div44
Calcite | Level 5

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

 

3 REPLIES 3
art297
Opal | Level 21

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

ChrisNZ
Tourmaline | Level 20

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

 

Oligolas
Barite | Level 11

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 -

SAS Innovate 2025: Register Today!

 

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.


Register now!

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