I try to create a new binary variable Perform (1 or 0) based on another numeric variable Days. I am using SAS Enterprise guide 6.1
Example:
data want;
set test;
if Days =< 30 then Perform = 0;
else if Days > 30 then Perform = 1;
run;
But the Perform only shows 0 although many observations have Days > 30. I also tried to switch the order of the codes as follow:
Example:
data want;
set test;
if Days > 30 then Perform = 1;
else if Days =< 30 then Perform = 0;
run;
I got the same results. The new variable Perform shows 0 for all the observations.
Can anybody help me with this problem?
Any help is highly appreciated.
There seems to be this widespread idea that you need to use IF and ELSE to create a binary variable. You do not. The result of a Boolean expression is 0 or 1. This will do what you are trying to do:
Perform = Days ge 30;
without resorting to two statements. As @Reeza previously noted, ensure that your values coming in really have the values that you think they do.
Can you show the output of a proc means on your data first?
proc means data=test n min max median p5 p95;
var perform;
run;
Hello Reeza:
There is no Perform in the data test. Should I create this new variable first before I use it? If I should, could you show me how?
I thought that when I used a new variable, I created this variable automatically in the dataset want.
@jeniffer_jacob wrote:
Hello Reeza:
There is no Perform in the data test. Should I create this new variable first before I use it? If I should, could you show me how?
I thought that when I used a new variable, I created this variable automatically in the dataset want.
Sorry, that should have been the DAYS variable. Basically you need to check the distribution to see if the error is in your data, your code or your understanding of the data.
There seems to be this widespread idea that you need to use IF and ELSE to create a binary variable. You do not. The result of a Boolean expression is 0 or 1. This will do what you are trying to do:
Perform = Days ge 30;
without resorting to two statements. As @Reeza previously noted, ensure that your values coming in really have the values that you think they do.
It worked!
Thanks a lot for all the kind replies.
A caution when using @WarrenKuhfeld's solution if your variable may contain missing values. Missing is always less than any given value. So if you do not want missing to actually be assigned to 0 you need to ensure that you filter your data such as
If not missing(Days) then Perform = Days ge 30;
Note that If / Then/ Else approach has the same potential flaw.
A very good point about missing values.
Thanks a lot.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.