hello,
If searching for a date in between two dates, could someone offer a helpful tip (with explanation)?
In example A, the following query worked and does yield desired results:
data mc_1 mc_2 mc_3 mc_4 mc_5 mc_6 mc_7 mc_8 ; set Have ; if ts_acn_opn_dt<’01jan2010’d then output mc_1; else if ts_acn_opn_dt>=‘01jan2010’d and ts_acn_opn_dt<’31dec2010’d then output mc_2; else if ts_acn_opn_dt>=‘01jan2011’d and ts_acn_opn_dt<’31dec2011’d then output mc_3; else if ts_acn_opn_dt>=‘01jan2012’d and ts_acn_opn_dt<’31dec2012’d then output mc_4; else if ts_acn_opn_dt>=‘01jan2013’d and ts_acn_opn_dt<’31dec2013’d then output mc_5; else if ts_acn_opn_dt>=‘01jan2014’d and ts_acn_opn_dt<’31dec2014’d then output mc_6; else if ts_acn_opn_dt>=‘01jan2015’d and ts_acn_opn_dt<’31dec2015’d then output mc_7; else output mc_8; run;
However, in example B, it does NOT yield desired result:
data mc_1 mc_2 mc_3 mc_4 mc_5 mc_6 mc_7 mc_8; set have ; if ts_acn_opn_dt<’01jan2010’d then output mc_1; else if ’01jan2010’d>=ts_acn_opn_dt<’01jan2011’d then output mc_2; else if ‘01jan2011’d>=ts_acn_opn_dt<’01jan2012’d then output mc_3; else if ‘01jan2012’d>=ts_acn_opn_dt<’01jan2013’d then output mc_4; else if ‘01jan2013’d>=ts_acn_opn_dt<’01jan2014’d then output mc_5; else if ‘01jan2014’d>=ts_acn_opn_dt<’01jan2015’d then output mc_6; else if ‘01jan2015’d>=ts_acn_opn_dt<’01jan2016’d then output mc_7; else output mc_8;run;
I'm trying to understand reason for example B not yielding the results I want (Output mc_3 contains observations dated 02jan2010).
Thanks in advance
thanks Reeza!
Try either
else if ’01jan2010’d>=ts_acn_opn_dt >=’31Dec2010’d
in your specific case you could also use If year(ts_acn_opn_dt)=2010 then ...
thanks Ballardw for showing this helpful tip: If year(ts_acn_opn_dt)=2010 then
You moved your variable to the 'other' side of the comparison operator, >=, which means you need to flip the sign for the relationship to be true, to <=
Try changing your first set of operators from GE to LE.
You've changed your boundary values so you need to account for that.
thanks Reeza!
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
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.
Ready to level-up your skills? Choose your own adventure.