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