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. 

sas-innovate-2024.png

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.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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