how to create a new binary variable based on the value of another numeric varialbe?

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 9
Accepted Solution

how to create a new binary variable based on the value of another numeric varialbe?

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.


Accepted Solutions
Solution
‎12-18-2017 12:15 PM
SAS Super FREQ
Posts: 497

Re: how to create a new binary variable based on the value of another numeric varialbe?

Posted in reply to jeniffer_jacob

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.

View solution in original post


All Replies
Super User
Posts: 23,224

Re: how to create a new binary variable based on the value of another numeric varialbe?

Posted in reply to jeniffer_jacob

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

Re: how to create a new binary variable based on the value of another numeric varialbe?

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.

 

Trusted Advisor
Posts: 1,270

Re: how to create a new binary variable based on the value of another numeric varialbe?

Posted in reply to jeniffer_jacob
Hi,

Try this.

data want;
set test;
Perform = 0;
if Days > 30 then Perform = 1;
run;
Super User
Posts: 23,224

Re: how to create a new binary variable based on the value of another numeric varialbe?

Posted in reply to jeniffer_jacob

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.

Solution
‎12-18-2017 12:15 PM
SAS Super FREQ
Posts: 497

Re: how to create a new binary variable based on the value of another numeric varialbe?

Posted in reply to jeniffer_jacob

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.

Occasional Contributor
Posts: 9

Re: how to create a new binary variable based on the value of another numeric varialbe?

Posted in reply to WarrenKuhfeld

It worked!

 

Thanks a lot for all the kind replies.

Super User
Posts: 13,283

Re: how to create a new binary variable based on the value of another numeric varialbe?

Posted in reply to WarrenKuhfeld

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.

Occasional Contributor
Posts: 9

Re: how to create a new binary variable based on the value of another numeric varialbe?

A very good point about missing values.

 

Thanks a lot.

☑ This topic is solved.

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

Discussion stats
  • 8 replies
  • 163 views
  • 0 likes
  • 5 in conversation