Why isn't my code flagging variables correctly?

Accepted Solution Solved
Reply
Contributor
Posts: 37
Accepted Solution

Why isn't my code flagging variables correctly?

I am trying to flag one of my variables (tsi_days) only when it is less than 0. However my code seems to also be flagging my variable when it is also missing. 

 

this is my code:

 

if tsi_days < 0 then tsi_exclude=1;
	else if tsi_days=. then tsi_exclude=0;
	else tsi_exclude=0;

What am I doing wrong?

 

this is the output:

 

tsi.png


Accepted Solutions
Solution
‎01-25-2017 01:46 PM
Respected Advisor
Posts: 4,926

Re: Why isn't my code flagging variables correctly?

Posted in reply to confused_saser

In SAS, a missing numeric value is < 0. Thus, you must test

 

tsi_exclude = tsi_days < 0 and not missing(tsi_days);

PG

View solution in original post


All Replies
Solution
‎01-25-2017 01:46 PM
Respected Advisor
Posts: 4,926

Re: Why isn't my code flagging variables correctly?

Posted in reply to confused_saser

In SAS, a missing numeric value is < 0. Thus, you must test

 

tsi_exclude = tsi_days < 0 and not missing(tsi_days);

PG
Respected Advisor
Posts: 3,799

Re: Why isn't my code flagging variables correctly?

Posted in reply to confused_saser

confused_saser wrote:

I am trying to flag one of my variables (tsi_days) only when it is less than 0. However my code seems to also be flagging my variable when it is also missing. 

 

this is my code:

 

if tsi_days < 0 then tsi_exclude=1;
	else if tsi_days=. then tsi_exclude=0;
	else tsi_exclude=0;

What am I doing wrong?

 

this is the output:

 

tsi.png


That's because missing is less than 0.  Do the test for missing first.

 

 

Contributor
Posts: 37

Re: Why isn't my code flagging variables correctly?

Posted in reply to data_null__

@data_null__ what do you mean do the test for missing? I'm not sure I understand what that means

Respected Advisor
Posts: 3,799

Re: Why isn't my code flagging variables correctly?

Posted in reply to confused_saser

confused_saser wrote:

@data_null__ what do you mean do the test for missing? I'm not sure I understand what that means


In your code you have a test for missing

if tsi_days < 0 then tsi_exclude=1;
	else if tsi_days=. then tsi_exclude=0;
	else tsi_exclude=0;

I'm saying do that first and then do the test for TSI_DAYS < 0.

Super Contributor
Posts: 252

Re: Why isn't my code flagging variables correctly?

Posted in reply to confused_saser

That's because SAS flags missing values as less than negative, so your first condition is flagging them as such.

 

One way of doing your code as you wish is to use the sign function. This returns values of -1 for negative, 0 for zero and 1 for positive. Or you can change the order of your conditions (I use select because it makes for ease of reading)

 

select;
   when(missing(tsi_days))
       tsi_exclude = 0;
   when(tsi_days < 0)
       tsi_days = 1;
   otherwise 
tsi_exclude = 0; end;
☑ This topic is solved.

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

Discussion stats
  • 5 replies
  • 155 views
  • 1 like
  • 4 in conversation