DATA Step, Macro, Functions and more

Help with if/else statement

Accepted Solution Solved
Reply
Contributor
Posts: 21
Accepted Solution

Help with if/else statement

Can anyone provide some insight as to why this code is wrong:

 

data analytic4;
set analytic3;

if admitdif1 lt "30" OR admitdif1a lt "30" OR admitdif2 lt "30" OR admitdif2a lt "30" OR admitdif3 lt "30" OR admitdif3a lt "30"
OR admitdif4 lt "30" OR admitdif4a lt "30" OR admitdif5 lt "30" OR admitdif5a lt "30" OR admitdif6 lt "30" OR admitdif6a lt "30" then admit30day="1";

else admit30day="0";
RUN;

 

All the observations are coding as a "1" 


Accepted Solutions
Solution
‎08-31-2016 02:53 PM
Super User
Posts: 17,775

Re: Help with if/else statement

See the comment above about missing values. It's highly likely this is the issue. 

Use the min function to get smallest non missing value. 

 

If Min(of admitdif1-admitdif6) < 30 then var=1;

else var=0;

View solution in original post


All Replies
Super User
Posts: 10,476

Re: Help with if/else statement

Provide some of your input data where you do not expect to get 1 for a result.

 

A common issue when using LT or LE comparisons is forgetting that Missing values are always less than any value. So if any of your variables is missing then that LT condition is true and would get the 1 assignment.

 

 

 

Contributor
Posts: 21

Re: Help with if/else statement

The data set I have is temporary so I am not sure how I would send it to you. 

Solution
‎08-31-2016 02:53 PM
Super User
Posts: 17,775

Re: Help with if/else statement

See the comment above about missing values. It's highly likely this is the issue. 

Use the min function to get smallest non missing value. 

 

If Min(of admitdif1-admitdif6) < 30 then var=1;

else var=0;

Super User
Posts: 5,079

Re: Help with if/else statement

Are your variables really character variables? 

 

If so, making character comparisons is tricky.  For example:

 

if "100" < "30" then var=1;

 

This comparison is true.  As a character string, "100" is less than "30" because "1" is less than "3".

 

Still, as has been suggested, the first place to look is for missing values.

Contributor
Posts: 21

Re: Help with if/else statement

Thank you so very much! That worked!
☑ This topic is SOLVED.

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

Discussion stats
  • 5 replies
  • 248 views
  • 1 like
  • 4 in conversation