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 (BASUG) is hosting our in person SAS Blowout on Oct 18!
This full-day event in Cambridge, Mass features four presenters from SAS, presenting on a range of SAS 9 programming topics. Pre-registration by Oct 15 is required.
Full details and registration info at https://www.basug.org/events.

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 (BASUG) is hosting our in person SAS Blowout on Oct 18!
This full-day event in Cambridge, Mass features four presenters from SAS, presenting on a range of SAS 9 programming topics. Pre-registration by Oct 15 is required.
Full details and registration info at https://www.basug.org/events.
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: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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