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;
The Boston Area SAS Users Group is hosting free webinars!
Next webinar will be in January 2025. Until then, check out our archives: https://www.basug.org/videos. And be sure to subscribe to our our email list.

SAS Innovate 2025: Register Now

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!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 897 views
  • 0 likes
  • 3 in conversation