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

How to write a sas statement to find the range of letters and numbers, such as

if var1 = 'A'  to 'G' then var2= 1;

if var3 = 1- 20 then var4 =' group1';

thanks.

1 ACCEPTED SOLUTION

Accepted Solutions
andreas_lds
Jade | Level 19

@huizhao26 wrote:

How to write a sas statement to find the range of letters and numbers, such as

if var1 = 'A'  to 'G' then var2= 1;

if var3 = 1- 20 then var4 =' group1';

thanks.


For numeric variable you could use

if 1 <= var3 <= 20 then var4 = 'group1'

But using formats is the better way to solve such tasks.

View solution in original post

5 REPLIES 5
andreas_lds
Jade | Level 19

@huizhao26 wrote:

How to write a sas statement to find the range of letters and numbers, such as

if var1 = 'A'  to 'G' then var2= 1;

if var3 = 1- 20 then var4 =' group1';

thanks.


For numeric variable you could use

if 1 <= var3 <= 20 then var4 = 'group1'

But using formats is the better way to solve such tasks.

irena_g
Calcite | Level 5

data a;
var1="K";
if rank('A') <= rank(var1)>= rank('G') then var2=1;
run;

Astounding
PROC Star

When working with character data, there are pitfalls.  In the simplest case, you could use:

if ('A' <= var1 <= 'G') then var2 = 1;

However, you have to pay attention to a few issues.  Will VAR1 always be in uppercase?  Uppercase letters are different than lowercase letters.  Will VAR1 always contain a single character?  If it contains "George" should that be included or excluded?  Here is a more careful version that should be considered:

if ('A' <= upcase(var1) < 'H') then var2 = 1;

But it would be helpful to know something about your data before deciding on a solution.

 

sas-innovate-wordmark-2025-midnight.png

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