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

Hi all,

We are using SAS 9.1.3 on Mainframe z/OS.

My colleague approached me asking how in an "if" statement he could check if a variable was in a range of numbers without specifying the individual members of the range. I remembered seeing a colon being used for this purpose in these forums, e.g.:

data _null_;

  a=4;

  if a in (1:7) then

    put 'True';

  else

    put 'False';

run;

which my colleague then put in his code and he hasn't got back to me so I assume it is OK. This got me thinking:

Is there any direct equivalent for an alphabetic range in an "if" statement? E.g. akin to ('A':'G') but this syntax does not work

I should be back in tomorrow if you have any questions - or better still any answers!

Thanks in advance,

Amir.

1 ACCEPTED SOLUTION

Accepted Solutions
MikeZdeb
Rhodochrosite | Level 12

hi ... no character ranges, but for a single character you could try the RANK function ... A=65, G=71

data x;

length in $3;

input x :$1. @@;

in = ifc(rank(x) in (65:71), 'YES' , 'NO');

datalines;

F G H I

;


x    in

F    YES

G    YES

H    NO

I    NO

ps  never knew that the default length of variables created with IFC is 200

View solution in original post

5 REPLIES 5
MikeZdeb
Rhodochrosite | Level 12

hi ... no character ranges, but for a single character you could try the RANK function ... A=65, G=71

data x;

length in $3;

input x :$1. @@;

in = ifc(rank(x) in (65:71), 'YES' , 'NO');

datalines;

F G H I

;


x    in

F    YES

G    YES

H    NO

I    NO

ps  never knew that the default length of variables created with IFC is 200

Astounding
PROC Star

Amir,

This is relatively straightforward, using a colon modifier in a slightly different way.  For example:

if 'A' <=: lastname <: 'C';

This statement selects all values for LASTNAME that begin with A or B.

The colon indicates that the comparison should be made by using the number of characters in whichever string has the shorter length.  Issues to be wary of:  capitalization makes a difference, and the collating sequence is different on ASCII vs. EBCDIC systems.  Finally, note that this is different than what you were doing before.  In your numeric list, 3.5 would not be in the list.  The list of 1:7 includes the integer values only.  This character-based version is selecting differently, based on the range, not based on a list of values.

Good luck.

Amir
PROC Star

Hi,

Thank you very much for your answers; as usual I have learnt a lot more than the original question was posing.

Mike: I tried looking up an opposite for the "rank" function (returns the character for a given rank) to help this kind of code work on different platforms, because I had to change the range to (193:199) for z/OS, but I could not find any - not a problem though.

Astounding: Thank you for your solution and flagging the subtleties of the ranges for both numeric and character.

Kind regards,

Amir.

MikeZdeb
Rhodochrosite | Level 12

Hi ... opposite of RANK is BYTE.  Try (for ASCII, Windows SAS) ...

data _null_;

do j=32 to 126;

   char_var = byte(j);

   put j 3. '|'  char_var +(-1) '|';

end;

run;

portion of LOG ...

32| |

33|!|

34|"|

35|#|

36|$|

37|%|

38|&|

39|'|

40|(|

Amir
PROC Star

Hi Mike,

I already looked at the byte() and collate() functions that were referenced in the "See Also" section of the rank() definition, and neither struck me as being the clear opposite of  rank() - thanks for putting me right. This forum is such an education.

Thanks again,

Amir.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

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
  • 5 replies
  • 3764 views
  • 6 likes
  • 3 in conversation