BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
brulard
Pyrite | Level 9

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

1 ACCEPTED SOLUTION

Accepted Solutions
4 REPLIES 4
ballardw
Super User

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

brulard
Pyrite | Level 9

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

Reeza
Super User

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. 

brulard
Pyrite | Level 9

thanks Reeza!

SAS Innovate 2025: Register Now

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!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 859 views
  • 0 likes
  • 3 in conversation