BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Ronein
Onyx | Level 15

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.*/
1 ACCEPTED SOLUTION

Accepted Solutions
andreas_lds
Jade | Level 19

If you want a global macro-variable, you have to use %global-statement.

View solution in original post

4 REPLIES 4
andreas_lds
Jade | Level 19

If you want a global macro-variable, you have to use %global-statement.

Ronein
Onyx | Level 15

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;
 
ballardw
Super User

@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.

Rick_SAS
SAS Super FREQ

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;

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
  • 4 replies
  • 1025 views
  • 5 likes
  • 4 in conversation