BookmarkSubscribeRSS Feed
znhnm
Quartz | Level 8

Hi, 

 

I am trying to apply one filter on date1 and another filter on date2 and creating an OR selection between them. The below syntax is not working properly. The code runs but the filter on date2 is not applied to the data. What would be the right syntax?

 

data example2;
set example;
if (((date1 > '01JAN2022:00:00:00.000000'dt AND date1 < "31DEC2022:00:00:00.000000"dt) OR (missing(date1)))
OR ((date2 > '01JAN2022:00:00:00.000000'dt AND date2 < "31DEC2022:00:00:00.000000"dt) OR (missing(date2))));
run;

 

3 REPLIES 3
PaigeMiller
Diamond | Level 26

Are variables DATE1 and DATE2 both date/time values? What format is applied to them? If you remove the format, what is a typical value of DATE2?

 

If they are both date/time values, we would need to see a portion of your data set to determine what the problem is, and we would need to know what the desired outcome is. Please provide data as working SAS data step code, which you can type in yourself or follow these instructions. Attachments and screen captures are not acceptable ways to share data.

--
Paige Miller
Astounding
PROC Star

As you saw in writing your code, it's clumsy to refer to datetime values.  You can further see this by looking at the conditions you selected, since dates that fall on December 31, 2022 are (probably) being deleted improperly.  I would suggest simplifying and getting rid of the datetimes:

 

data example2;
   set example;
   if missing(date1) or missing(date2) or year(datepart(date1))=2022 or year(datepart(date2))=2022;
run;
Sajid01
Meteorite | Level 14

@znhnm Everything works for me. See the example code below.
Please share your sample data code and log to get help.

data example;
informat date1 date2 datetime26.;
format date1 date2 datetime26.;
input date1 date2;
datalines;
15AUG2022:00:00:00.000000 14AUG2022:00:00:00.000000
;
run;

data example2;
set example;
if (((date1 > '01JAN2022:00:00:00.000000'dt AND date1 < "31DEC2022:00:00:00.000000"dt) OR (missing(date1)))
OR ((date2 > '01JAN2022:00:00:00.000000'dt AND date2 < "31DEC2022:00:00:00.000000"dt) OR (missing(date2))));
run;
proc print data=example2;
run;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 3 replies
  • 467 views
  • 1 like
  • 4 in conversation