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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
WarrenKuhfeld
Ammonite | Level 13

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

8 REPLIES 8
Reeza
Super User

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;
jeniffer_jacob
Fluorite | Level 6

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.

 

stat_sas
Ammonite | Level 13
Hi,

Try this.

data want;
set test;
Perform = 0;
if Days > 30 then Perform = 1;
run;
Reeza
Super User

@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.

WarrenKuhfeld
Ammonite | Level 13

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.

jeniffer_jacob
Fluorite | Level 6

It worked!

 

Thanks a lot for all the kind replies.

ballardw
Super User

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.

jeniffer_jacob
Fluorite | Level 6

A very good point about missing values.

 

Thanks a lot.

SAS Innovate 2025: Call for Content

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!

Submit your idea!

How to Concatenate Values

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.

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
  • 8 replies
  • 3153 views
  • 2 likes
  • 5 in conversation