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.

BASUG is hosting free webinars Next up: Don Henderson presenting on using hash functions (not hash tables!) to segment data on June 12. Register now at the Boston Area SAS Users Group event page: 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.

BASUG is hosting free webinars Next up: Don Henderson presenting on using hash functions (not hash tables!) to segment data on June 12. Register now at the Boston Area SAS Users Group event page: 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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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