DATA Step, Macro, Functions and more

sas if else then not processing

Accepted Solution Solved
Reply
Contributor
Posts: 38
Accepted Solution

sas if else then not processing

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!


Accepted Solutions
Solution
‎05-16-2016 02:40 PM
Super User
Posts: 5,093

Re: sas if else then not processing

[ Edited ]

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


All Replies
Regular Contributor
Posts: 161

Re: sas if else then not processing

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.

Contributor
Posts: 38

Re: sas if else then not processing

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?

Regular Contributor
Posts: 161

Re: sas if else then not processing

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?

Contributor
Posts: 38

Re: sas if else then not processing

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.
Solution
‎05-16-2016 02:40 PM
Super User
Posts: 5,093

Re: sas if else then not processing

[ Edited ]

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.

Contributor
Posts: 38

Re: sas if else then not processing

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.

Super User
Posts: 5,260

Re: sas if else then not processing

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

Data never sleeps
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 499 views
  • 3 likes
  • 4 in conversation