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

Hi,

 

I have a dataset called test_dataset and want to only keep accounts where the c_EFF_DT variable is in March 2024 (so in example below, only the account with 02MAR2024 would remain). My code below isn't filtering correctly, so can someone help me to correct it please?

 

data test_output;
   set test_dataset;
   where "01Mar2024"d <= c_EFF_DT <= "31Mar2024"d;
run;

 

Justin9_0-1714408008865.png

Justin9_1-1714408032148.png

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

Your code would work if c_EFF_DT was an actual SAS date value. But it is not a SAS date, it is a SAS date/time value. How do I know? Because it has been assigned a date/time format.

 

So to do the filtering, you have to use date/time values

 

where "01Mar2024:00:00:00"dt <= c_EFF_DT <= "31Mar2024:00:00:00"dt;

 

 

or even simpler, convert c_eff_dt to a date value

 

 where "01Mar2024"d <= datepart(c_EFF_DT) <= "31Mar2024"d;

 

 

or if you are going to be doing this repeatedly for future months, and you don't want to have to figure out what the last day of each month is and then type that into the program

 

where month(datepart(c_eff_dt))=3 and year(datepart(c_eff_dt))=2024;

 

or

 

where '01MAR2024'd <= datepart(c_eff_dt) < '01APR2024'd

Note the change from <= to < 

I like the last one best, less typing, no need to mentally figure out what the last day of the month is

--
Paige Miller

View solution in original post

1 REPLY 1
PaigeMiller
Diamond | Level 26

Your code would work if c_EFF_DT was an actual SAS date value. But it is not a SAS date, it is a SAS date/time value. How do I know? Because it has been assigned a date/time format.

 

So to do the filtering, you have to use date/time values

 

where "01Mar2024:00:00:00"dt <= c_EFF_DT <= "31Mar2024:00:00:00"dt;

 

 

or even simpler, convert c_eff_dt to a date value

 

 where "01Mar2024"d <= datepart(c_EFF_DT) <= "31Mar2024"d;

 

 

or if you are going to be doing this repeatedly for future months, and you don't want to have to figure out what the last day of each month is and then type that into the program

 

where month(datepart(c_eff_dt))=3 and year(datepart(c_eff_dt))=2024;

 

or

 

where '01MAR2024'd <= datepart(c_eff_dt) < '01APR2024'd

Note the change from <= to < 

I like the last one best, less typing, no need to mentally figure out what the last day of the month is

--
Paige Miller

SAS Innovate 2025: Register Today!

 

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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
  • 1 reply
  • 536 views
  • 0 likes
  • 2 in conversation