BookmarkSubscribeRSS Feed
znhnm
Quartz | Level 8

Hi,

 

I am trying to link the two conditions on date1 and date2 with an OR statement in the middle. I am getting a syntax error when I try below. What is the rght way to do it? 

 

 

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


Thanks!

4 REPLIES 4
Sajid01
Meteorite | Level 14

You have date and not datetime. So use d and not dt then it will work.

The sample code is given below.

data example;
informat date1 date2 date9.;
format date1 date2 date9.;
input date1 date2;
datalines;
15AUG2022 14AUG2022
;
run;
data example2;
set example;
if (((date1 > '01JAN2022'd AND date1 < "31DEC2022"d) OR (missing(date1)))
OR ((date2 > '01JAN2022'd AND date2 < "31DEC2022"d) OR (missing(date2))));
output;
run;
znhnm
Quartz | Level 8
Hi,

Thanks for the answer. I actually have the dt value in my original dataset. I stil have the same problem.

The exact way of the query I run should be like this:

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;



ballardw
Super User

What do you mean "link"? The code you show is more what I would call a selection.

 

Note: Best practice on this forum when asking about syntax errors is to copy the entire data step or procedure code from the LOG will all the notes. Then on the forum open a text box using the </> icon that appears above the message window and paste ALL of the text. The text box maintains formatting of text as the message windows will reformat text and make the diagnostic characters SAS often provides less useful.

 

You may want to provide Proc contents of your data so we can clarify what type of variables you actually have. When I see one improper use such as '01JAN2022'dt then I question all variables as to type and properties.

 

If you want to test for membership in a year it is easier to use the Year function: If year(datevariable) = 2022

If the variable is actually a date time then : If year(datepart(datetimevariable))=2022

Or use the interval  : lowervalue le variable le uppervalue

'01JAN2022'd < date1 < "31DEC2022"d

Note, the above makes it very easy to see that if you wanted the first and last day of the year included your comparison is logically incorrect as you may want LE to include the end values. If you actually intend to exclude the end dates 1 Jan and 31 Dec I would say you have an odd requirement.

znhnm
Quartz | Level 8
Hi, thanks for the reply. I tried to state it better here: https://communities.sas.com/t5/SAS-Programming/Multiple-conditions-IF/m-p/847862#M335198

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
  • 595 views
  • 2 likes
  • 3 in conversation