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

Hello,

 

I am getting an error when calling a macro date.  Any suggestions appreciated!!

 

 

/****Setting up the date range I need across all programs****/

 

*Numeric;
%let reportdtst=mdy(03,24,2022);
%let reportdten=mdy(03,23,2023);

*Character;
data _null_;
call symput("reportdtstc",strip(put(&reportdtst,date9.)));
call symput("reportdtenc",strip(put(&reportdten,date9.)));
run;

/***************************/

 

...then when I try to call it in program, I am getting error.

 

 

options symbolgen;
%put &reportdtstc &reportdtenc;
data adae1;
set adam.adaedsur (where=(treatfl='Y')); 

if AESER='Y' and RELGR1N=2 and &reportdtstc <= astdt <= &reportdtenc;

drop AOCC04FL AOCC05FL AOCC06FL;
run;

 

 

jenim514_0-1677606714588.png

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

Macro variables containing dates should NOT be formatted when you want to use them in logical or arithmetic operations.

 

%let reportdtst=mdy(03,24,2022);
%let reportdten=mdy(03,23,2023);

*Character;
data _null_;
call symput("reportdtstc",&reportdtst);
call symput("reportdtenc",&reportdten);
run;

options symbolgen;
%put &reportdtstc &reportdtenc;
data adae1;
set adam.adaedsur (where=(treatfl='Y')); 
if AESER='Y' and RELGR1N=2 and &reportdtstc <= astdt <= &reportdtenc;
drop AOCC04FL AOCC05FL AOCC06FL;
run;

 

 

Now, why does this work? Because macro variables are replaced by their values when you run code. So when you execute this code

 

if AESER='Y' and RELGR1N=2 and &reportdtstc <= astdt <= &reportdtenc;

 

this is what SAS actually runs, if we use your original code

 

if AESER='Y' and RELGR1N=2 and 24MAR2022 <= astdt <= 23MAR2023;

 

But this is not legal SAS code. When using macro variables, the result must be legal valid working SAS code. I assign you a homework assignment ... explain why this is not legal SAS code.

 

When the macro variable is unformatted, then comparing ASTDT to an integer works fine, because SAS dates are integers expressing number of days since 01JAN1960. So with my code, this is actually what runs:

 

if AESER='Y' and RELGR1N=2 and 22728<= astdt <= 23092;

 

which is legal SAS code, and compares ASTDT to the integer value of 22728 (the number of days since 01JAN1960), which is indeed 24MAR2022, and so on.

--
Paige Miller

View solution in original post

3 REPLIES 3
PaigeMiller
Diamond | Level 26

Macro variables containing dates should NOT be formatted when you want to use them in logical or arithmetic operations.

 

%let reportdtst=mdy(03,24,2022);
%let reportdten=mdy(03,23,2023);

*Character;
data _null_;
call symput("reportdtstc",&reportdtst);
call symput("reportdtenc",&reportdten);
run;

options symbolgen;
%put &reportdtstc &reportdtenc;
data adae1;
set adam.adaedsur (where=(treatfl='Y')); 
if AESER='Y' and RELGR1N=2 and &reportdtstc <= astdt <= &reportdtenc;
drop AOCC04FL AOCC05FL AOCC06FL;
run;

 

 

Now, why does this work? Because macro variables are replaced by their values when you run code. So when you execute this code

 

if AESER='Y' and RELGR1N=2 and &reportdtstc <= astdt <= &reportdtenc;

 

this is what SAS actually runs, if we use your original code

 

if AESER='Y' and RELGR1N=2 and 24MAR2022 <= astdt <= 23MAR2023;

 

But this is not legal SAS code. When using macro variables, the result must be legal valid working SAS code. I assign you a homework assignment ... explain why this is not legal SAS code.

 

When the macro variable is unformatted, then comparing ASTDT to an integer works fine, because SAS dates are integers expressing number of days since 01JAN1960. So with my code, this is actually what runs:

 

if AESER='Y' and RELGR1N=2 and 22728<= astdt <= 23092;

 

which is legal SAS code, and compares ASTDT to the integer value of 22728 (the number of days since 01JAN1960), which is indeed 24MAR2022, and so on.

--
Paige Miller
jenim514
Pyrite | Level 9
add the "&reportdtstc"d and sas then recognizes as a date!
Quentin
Super User

Keep in mind that macros are about text substitution.

 

Your code:

data adae1;
set adam.adaedsur (where=(treatfl='Y')); 
if AESER='Y' and RELGR1N=2 and &reportdtstc <= astdt <= &reportdtenc;
drop AOCC04FL AOCC05FL AOCC06FL;
run;

Will resolve to:

data adae1;
set adam.adaedsur (where=(treatfl='Y')); 
if AESER='Y' and RELGR1N=2 and 24MAR2022 <= astdt <= 23MAR2022;
drop AOCC04FL AOCC05FL AOCC06FL;
run;

which is not valid code.  I suspect you want to generate:

data adae1;
set adam.adaedsur (where=(treatfl='Y')); 
if AESER='Y' and RELGR1N=2 and "24MAR2022"d <= astdt <= "23MAR2022"d;
drop AOCC04FL AOCC05FL AOCC06FL;
run;

You could do that with:

data adae1;
set adam.adaedsur (where=(treatfl='Y')); 
if AESER='Y' and RELGR1N=2 and "&reportdtstc"d <= astdt <= "&reportdtenc"d;
drop AOCC04FL AOCC05FL AOCC06FL;
run;

Note that you don't actually need the macro variables with the pretty values.  You could also do it with:

data adae1;
set adam.adaedsur (where=(treatfl='Y')); 
if AESER='Y' and RELGR1N=2 and &reportdtst <= astdt <= &reportdten;
drop AOCC04FL AOCC05FL AOCC06FL;
run;

which would resolve to:

data adae1;
set adam.adaedsur (where=(treatfl='Y')); 
if AESER='Y' and RELGR1N=2 and mdy(03,24,2022) <= astdt <= mdy(03,23,2023);
drop AOCC04FL AOCC05FL AOCC06FL;
run;

Perhaps the easiest would be to just use a date literal when you create the macro variable:

%let reportdtst="24Mar2022"d;
%let reportdten="23Mar2023"d;;

data adae1;
set adam.adaedsur (where=(treatfl='Y')); 
if AESER='Y' and RELGR1N=2 and &reportdtst <= astdt <= &reportdten;
drop AOCC04FL AOCC05FL AOCC06FL;
run;
BASUG is hosting free webinars Next up: Mark Keintz presenting History Carried Forward, Future Carried Back: Mixing Time Series of Differing Frequencies on May 8. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.

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
  • 3 replies
  • 480 views
  • 0 likes
  • 3 in conversation