SAS Studio

Write and run SAS programs in your web browser
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-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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