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: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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