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!
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.