Searching for in between dates -comparing two methods

Solved
Frequent Contributor
Posts: 123

Searching for in between dates -comparing two methods

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

Accepted Solutions
Solution
‎08-03-2016 10:28 AM
Frequent Contributor
Posts: 123

thanks Reeza!

All Replies
Super User
Posts: 13,523

Re: Searching for in between dates -comparing two methods

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

Frequent Contributor
Posts: 123

Re: Searching for in between dates -comparing two methods

thanks Ballardw for showing this helpful tip: If year(ts_acn_opn_dt)=2010 then

Super User
Posts: 23,700

Re: Searching for in between dates -comparing two methods

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.

Solution
‎08-03-2016 10:28 AM
Frequent Contributor
Posts: 123

Re: Searching for in between dates -comparing two methods

thanks Reeza!

☑ This topic is solved.