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;
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.
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.
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;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.