BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
SASdevAnneMarie
Barite | Level 11

Hello Experts,

 

I'm wondering why this data comparing doesn't work (I have also the date> 01/04/2024 in base finale)

data base_finale;
	set base2;
	if   month("Date de réception"n)<="01APR2024"d;
run;

The format of coulmn is "Date de réception"n

SASdevAnneMarie_0-1712322593042.png

Thank you !

1 ACCEPTED SOLUTION

Accepted Solutions
Quentin
Super User

When you say "doesn't work" I assume you mean the code runs without errors or warnings in the log, but you're surprised by the result?

 

What is your intent for this code?  

 

If you want to select records with date on or before the April 1, 2024, don't use the MONTH function, try:

if   "Date de réception"n <= "01APR2024"d ;

The MONTH() function will return 1-12, which is probably not what you want.  When you use the MONTH function, this comparison will always be true.

The Boston Area SAS Users Group is hosting free webinars!
Next webinar will be in January 2025. Until then, check out our archives: https://www.basug.org/videos. And be sure to subscribe to our our email list.

View solution in original post

4 REPLIES 4
Quentin
Super User

When you say "doesn't work" I assume you mean the code runs without errors or warnings in the log, but you're surprised by the result?

 

What is your intent for this code?  

 

If you want to select records with date on or before the April 1, 2024, don't use the MONTH function, try:

if   "Date de réception"n <= "01APR2024"d ;

The MONTH() function will return 1-12, which is probably not what you want.  When you use the MONTH function, this comparison will always be true.

The Boston Area SAS Users Group is hosting free webinars!
Next webinar will be in January 2025. Until then, check out our archives: https://www.basug.org/videos. And be sure to subscribe to our our email list.
SASdevAnneMarie
Barite | Level 11
Thank you Quentin.
ballardw
Super User

The MONTH function will return a value of 1 to 12 when given a numeric (hopefully date) value or missing if the argument is missing. So you get a return of at most 12.  The date 01APR2024 has a numeric value of 23467.

So any result from the Month function will be much less than 23467.

 

If you haven't encountered it before, missing is always less than any non-missing value.

Patrick
Opal | Level 21

Are you looking for any row where the comparison date is from the same or previous month, or should the two dates only be max one month apart? Below code should give you an idea of the options.

data base2;
  format "Date de réception"n ddmmyy10.;
  "Date de réception"n='20mar2024'd; output;
  "Date de réception"n='20may2024'd; output;
  "Date de réception"n='12jan2023'd; output;
run;

run;
data base_finale;
	set base2;
  /* if intck('month',"Date de réception"n,"01APR2024"d) in (0,1); */
  interval_1=intck('month',"Date de réception"n,"01APR2024"d);
  interval_2=intck('month',"Date de réception"n,"01APR2024"d,'c');
run;

proc print data=base_finale;
run;

Patrick_0-1712329218231.png

 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 927 views
  • 3 likes
  • 4 in conversation