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