## If then else if statement not working

Solved
Occasional Contributor
Posts: 14

# If then else if statement not working

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;```

Accepted Solutions
Solution
‎03-16-2017 01:31 PM
Super User
Posts: 7,720

## Re: If then else if statement not working

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;```

All Replies
Posts: 1,469

## Re: If then else if statement not working

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;``````
Super User
Posts: 7,431

## Re: If then else if statement not working

Run this:

``````proc sql;
select max(result) from mydata.bllcinckids2014;
quit;
``````

Also make sure that result is a numeric variable.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Solution
‎03-16-2017 01:31 PM
Super User
Posts: 7,720

## Re: If then else if statement not working

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;```
Occasional Contributor
Posts: 14

Thank you!

Super User
Posts: 7,431

## Re: If then else if statement not working

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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Posts: 11,128

## Re: If then else if statement not working

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.

Occasional Contributor
Posts: 14

## Re: If then else if statement not working

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

☑ This topic is solved.