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!
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;
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.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.