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.
@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.
@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.
data a;
var1="K";
if rank('A') <= rank(var1)>= rank('G') then var2=1;
run;
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 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.