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

Hi,

 

I just realized that my if then, else if statement didn't work. I know because all bllresult is equal to zero and there should be some bllresults equal to one. I had no errors or warnings in the log regarding this. I found out when I was doing proc statements using this variable that the variable was not created correctly. Please see the code below. Does anyone have any segestions? I would appreciate any sugestions.

 

Thanks,

LMP

 

libname mydata "/folders/myshortcuts/myfolder";

run;


Data mydata.temp;

set mydata.bllcinckids2014;

 
/* if result# is greater than or equal to 5 then BLLresult is 1 , if less than 5 then the BLLresult is equal to 0 */

If result >= 5 then bllresult=1; else if result <5 then bllresult=0;
 run;

 

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Show some test data in the form of a datastep.  Could your resulvariable be character, if so your logic will not work.  Also note, for binary choices ifn/ifc functions are simpler:

data want;
  set have;
  bllresult=ifn(result >= 5,1,0);
run;

View solution in original post

7 REPLIES 7
Shmuel
Garnet | Level 18

I don't see any error in your code.

As a value can be either Greater-Equal or Less-Than you can try next code:

Data mydata.temp;
   set mydata.bllcinckids2014;
       If result GE 5 then bllresult=1; 
                      else bllresult=0;
 run;
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Show some test data in the form of a datastep.  Could your resulvariable be character, if so your logic will not work.  Also note, for binary choices ifn/ifc functions are simpler:

data want;
  set have;
  bllresult=ifn(result >= 5,1,0);
run;
LMP
Obsidian | Level 7 LMP
Obsidian | Level 7

Thank you!

Kurt_Bremser
Super User

If it was because the variable was of type character, a look into the log would have alerted you about the automatic conversion.

Such notes are always a bad sign.

ballardw
Super User

@LMP wrote:

Hi,

 

I just realized that my if then, else if statement didn't work. I know because all bllresult is equal to zero and there should be some bllresults equal to one. I had no errors or warnings in the log regarding this. I found out when I was doing proc statements using this variable that the variable was not created correctly. Please see the code below. Does anyone have any segestions? I would appreciate any sugestions.

 

Thanks,

LMP

 

libname mydata "/folders/myshortcuts/myfolder";

run;


Data mydata.temp;

set mydata.bllcinckids2014;

 
/* if result# is greater than or equal to 5 then BLLresult is 1 , if less than 5 then the BLLresult is equal to 0 */

If result >= 5 then bllresult=1; else if result <5 then bllresult=0;
 run;

 


What specifically makes you think "that the variable was not created correctly"?

If it is because you are getting 0 when result is actually missing that is the correct behavior for SAS: Missing is smaller than anything.

Or are getting an error? Show the error

Some value other than 0 or 1 (including missing)? Show the entire code for the data step AND the input data. You may have changed the value of result or bllresult prior to or after this step.

Note that example data should be in the form of a data step so we don't have to guess about characteristics of the variable. Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the {i} icon or attached as text to show exactly what you have and that we can test code against.

LMP
Obsidian | Level 7 LMP
Obsidian | Level 7

Hi,

 

There was no error. And there arent any missing results. The reason I know the if then else statement wasn't working is because if I look at the result section there are values greater than 5 and ther bllresult is still labeled as zero.

 

Kindly,

LMP

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 7 replies
  • 10776 views
  • 0 likes
  • 5 in conversation