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

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

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

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

5 REPLIES 5
PGStats
Opal | Level 21

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

 

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

PG
data_null__
Jade | Level 19

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

 

 

confused_saser
Obsidian | Level 7

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

data_null__
Jade | Level 19

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

LaurieF
Barite | Level 11

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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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
  • 5 replies
  • 679 views
  • 1 like
  • 4 in conversation