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

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!

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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.

View solution in original post

7 REPLIES 7
paulkaefer
Lapis Lazuli | Level 10

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.

blakezen
Obsidian | Level 7

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?

paulkaefer
Lapis Lazuli | Level 10

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?

blakezen
Obsidian | Level 7
Thanks for your reply. You're right no spaces and there is a semicolon after the else statement. Sorry about not having that clear from the beginning.

var 2 is a numeric character with length 8. However it has many decimal places to 10 sig digits, i.e 1.0134568920 format for each observation.
Astounding
PROC Star

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.

blakezen
Obsidian | Level 7

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.

LinusH
Tourmaline | Level 20

Moved post since this is a programming issue, not a community one.

Data never sleeps

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
  • 7 replies
  • 1623 views
  • 3 likes
  • 4 in conversation