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

Very asinine question, but why doesn't the following syntax in the when statement of the select block not work to set x=1 for any values of var 1 between 101-109, 201-209, 301-309?

 

 

data New;
     set Old;
     select (var1);
         ...
         when (101:109, 201:209, 301:309) x=1;
         ...
         otherwise;
     end;
run;

 

 

I get an error.  I have also tried using -- (no error but doesn't work), - (just subtracts), to (error), between (error)...

 

scratching my head...

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Because the WHEN clause of the SELECT/END statement doesn't support that syntax.  Note you would only get subtraction if you asked for subtraction by using a - between two values in an expression.

 

But the IN operator does support using N:M as a shortcut for a range of integers.

data test;
  input var1 @@ ;
  x=var1 in (101:109, 201:209, 301:309);
  select ;
    when (var1 in (101:109, 201:209, 301:309)) y=1;
    otherwise y=0;
  end;
  output;
cards;
1 101 105.6 200 209 305 
;
Obs     var1    x    y

 1       1.0    0    0
 2     101.0    1    1
 3     105.6    0    0
 4     200.0    0    0
 5     209.0    1    1
 6     305.0    1    1

View solution in original post

4 REPLIES 4
Tom
Super User Tom
Super User

Because the WHEN clause of the SELECT/END statement doesn't support that syntax.  Note you would only get subtraction if you asked for subtraction by using a - between two values in an expression.

 

But the IN operator does support using N:M as a shortcut for a range of integers.

data test;
  input var1 @@ ;
  x=var1 in (101:109, 201:209, 301:309);
  select ;
    when (var1 in (101:109, 201:209, 301:309)) y=1;
    otherwise y=0;
  end;
  output;
cards;
1 101 105.6 200 209 305 
;
Obs     var1    x    y

 1       1.0    0    0
 2     101.0    1    1
 3     105.6    0    0
 4     200.0    0    0
 5     209.0    1    1
 6     305.0    1    1
runrunbunny
Obsidian | Level 7

Thanks a lot.

novinosrin
Tourmaline | Level 20

Hi @Jayme   i would think you need an IN operator to execute without errors unless you use that an assignment to initialize value at compile time in array, retain statement etc

 


data New;
     set Old;
     select ;
         ...
         when (var1 in (101:109, 201:209, 301:309)) x=1;
         ...
         otherwise;
     end;
run;
Shmuel
Garnet | Level 18

Have you checked does next simplified code work:

data New;
     set Old;
     select (var1);
         ...
         when (101:109) x=1;
         when (201:209) x=2;
         when (301:309) x=3;
         ...
         otherwise;
     end;
run;

Do you mean for any range #01 to #09 where # is any digit ?

Then try:  if mod(var1, 100) between 1 and 9 then x=1;

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
  • 4 replies
  • 4492 views
  • 1 like
  • 5 in conversation