Hi,
I'm having an impasse with if then else if statement options in sas. In brief:
data file 1 (keep= var1 var2 var3);
rename abc= var1 def=var2 ghi=var3;
set libref.file1 (where(var 4 NE .));
/*so far no problems with the above */
if 0<= var2 < 1 then var3="100%";
else if 1 <= var2 <2 then var3="110%";
else var3 = "110-120%"
run;
The final output is fine for all variables except for var3, which is really messed up. In fact, var3 is set to "110-" for every single observation. What am I doing wrong here and what is the matter with my syntax structure?
Thanks in advance for your time on this!
With regard to getting "110-120%" every time, the problem is with the RENAME statement. As a standalone statement, RENAME applies when all the processing is done and SAS is ready to output observations. You would have to use the original variable name (GHI) in your IF/THEN statements instead of VAR3.
There should at least be some sort of warning in your log about the rename not being possible. Your IF/THEN statements create VAR3, and your RENAME statement then attempts to rename GHI to an existing name.
SAS determines the length of a variable by the first use of it, unless you specify it. So it sees the first "110%" and assumes you want the var3 variable to be four characters long.
Resolve this by using a LENGTH statement (reference😞
LENGTH var3 $8;
Include this before your IF statement.
Thank you! This resolved the length issue. However, I'm still not seeing anything for var3. Now, instead of the "110-" that I was seeing earlier for every single row, I am now seeing blank in var 3, for every single row. What is wrong with the syntax of my if statement. Is SAS not seeing the ranges conditiones?
For one, I'm assuming there shouldn't be a space between var4 in your WHERE statement or between file and 1 in the top line. There should also be a semicolon after the ELSE statement.
It wouldn't hurt to put parentheses () around the expressions, though it should work without them.
What does the var2 variable look like? Are the values all positive numbers? Are they numeric or character values?
With regard to getting "110-120%" every time, the problem is with the RENAME statement. As a standalone statement, RENAME applies when all the processing is done and SAS is ready to output observations. You would have to use the original variable name (GHI) in your IF/THEN statements instead of VAR3.
There should at least be some sort of warning in your log about the rename not being possible. Your IF/THEN statements create VAR3, and your RENAME statement then attempts to rename GHI to an existing name.
Thank you very much. Though I didn't get any warnings (I just got blanks for each observation), I used the original variable name and everything worked! Thank you for the very helpful observation; conceptually this is useful going forward for me.
Moved post since this is a programming issue, not a community one.
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.