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:
In SAS, a missing numeric value is < 0. Thus, you must test
tsi_exclude = tsi_days < 0 and not missing(tsi_days);
In SAS, a missing numeric value is < 0. Thus, you must test
tsi_exclude = tsi_days < 0 and not missing(tsi_days);
@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:
That's because missing is less than 0. Do the test for missing first.
@data_null__ what do you mean do the test for missing? I'm not sure I understand what that means
@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.
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;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.