Hello,
I am trying to create a sas macro variable but I get a warning
WARNING: Apparent symbolic reference MAXDATE not resolved
I see that the process didn't work well and I dont know why
Data ttt;
format Pdate date9.;
Input Pdate :date9.;
Cards;
13FEB2020
18Mar2020
21Jun2020
19May2021
;
Run;
%macro exist;
%if %sysfunc(exist(ttt)) %then %do;
proc sql noprint;
select max(Pdate)
into :MaxDate trimmed
from ttt
;
quit;
%end;
%Else %do;
%let MaxDate=22159;
/*01SEP2020*/
%End;
%mend;
%exist
%put &MaxDate;
/*WARNING: Apparent symbolic reference MAXDATE not resolved.*/
If you want a global macro-variable, you have to use %global-statement.
If you want a global macro-variable, you have to use %global-statement.
Great!
May you please explain why do we need to use global macro variable in this example
Data ttt;
format Pdate date9.;
Input Pdate :date9.;
Cards;
13FEB2020
18Mar2020
21Jun2020
19May2021
;
Run;
%global MaxDate;
%macro exist;
%if %sysfunc(exist(ttt)) %then %do;
proc sql noprint;
select max(Pdate)
into :MaxDate trimmed
from ttt
;
quit;
%end;
%Else %do;
%let MaxDate=22159;
/*01SEP2020*/
%End;
%mend;
%exist
%put &MaxDate;
@Ronein wrote:
Great!
May you please explain why do we need to use global macro variable in this example
Data ttt; format Pdate date9.; Input Pdate :date9.; Cards; 13FEB2020 18Mar2020 21Jun2020 19May2021 ; Run; %global MaxDate; %macro exist; %if %sysfunc(exist(ttt)) %then %do; proc sql noprint; select max(Pdate) into :MaxDate trimmed from ttt ; quit; %end; %Else %do; %let MaxDate=22159; /*01SEP2020*/ %End; %mend; %exist %put &MaxDate;
You have a %put referencing the variable Maxdate AFTER the %exist has executed. That is why you need Maxdate to be global in this case. ANY time you use a macro variable outside of the scope of a running macro you are going to need that macro variable to be global.
I'd like to point out that, in most applications, the following two statements are equivalent, but the second is much easier to read and understand:
%let MaxDate=22159; /* 01SEP2020 */
%let MaxDate='01SEP2020'd;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.