I know with a character variable in SAS, you want to define the length first otherwise it'll have the length of the first value. I wonder if this is something that'll affect macro vars as well? I'm just talking about normal variables like a counter in a do loop. I'm not talking about the dynamic macro vars which is too advanced for me.
Thanks
Macro variable do not have a type (everything is text to the macro processor) and are not fixed length. So there is no need to "define" them in the sense you mean.
But they do have scope. You will discovery the impact of this when you work with actual macro definition and not just macro variables. If you create a macro variable inside a running macro that has not been defined before then will be created local to that macro, which means it will disappear when the macro finishes running. (Like WORK dataset disappear when your SAS session ends).
But you might want to initialize a macro variable to a default value in case the code you are using to calculate and assign a value does not run.
The place I normally see this being needed is when using SQL to create the macro variables and the query does not return any results.
proc sql noprint;
%let over20=NONE;
select name into :over20 separated by ' '
from sashelp.class
where age > 20
;
quit;
%put &=over20;
Results
833 proc sql noprint; 834 %let over20=NONE; 835 select name into :over20 separated by ' ' 836 from sashelp.class 837 where age > 20 838 ; NOTE: No rows were selected. 839 quit; NOTE: PROCEDURE SQL used (Total process time): real time 0.04 seconds cpu time 0.00 seconds 840 %put &=over20; OVER20=NONE
Macro variable do not have a type (everything is text to the macro processor) and are not fixed length. So there is no need to "define" them in the sense you mean.
But they do have scope. You will discovery the impact of this when you work with actual macro definition and not just macro variables. If you create a macro variable inside a running macro that has not been defined before then will be created local to that macro, which means it will disappear when the macro finishes running. (Like WORK dataset disappear when your SAS session ends).
But you might want to initialize a macro variable to a default value in case the code you are using to calculate and assign a value does not run.
The place I normally see this being needed is when using SQL to create the macro variables and the query does not return any results.
proc sql noprint;
%let over20=NONE;
select name into :over20 separated by ' '
from sashelp.class
where age > 20
;
quit;
%put &=over20;
Results
833 proc sql noprint; 834 %let over20=NONE; 835 select name into :over20 separated by ' ' 836 from sashelp.class 837 where age > 20 838 ; NOTE: No rows were selected. 839 quit; NOTE: PROCEDURE SQL used (Total process time): real time 0.04 seconds cpu time 0.00 seconds 840 %put &=over20; OVER20=NONE
Macro variable length is set in system options.
MVARSIZE sets the maximum size for a macro variable. The default is 65534 characters. You can set the option greater but if you need to you probably should reconsider what the heck you are stuffing into macro variables.
This interacts with the option MSYMTABMAX for keeping macro variables in memory. If the length of macro variables exceeds this setting then the values start getting written/read to disk and can affect performance.
You want to check the documentation for your operating system as each has peculiarites.
According to the documentation of MVARSIZE=, the maximum value is still 65534, although units of M and G are syntactically allowed.
Also see this:
73 options mvarsize=65535; ________ 18 ERROR 18-12: Optionswert für SAS-Option MVARSIZE muss zwischen 0 und 65534 sein.
Adding to what Tom has said, you only have to first initialize the macro variable if you want to make it global (with the %global statement):
%GLOBAL myvar; %let myvar = xxx;
Otherwise, you don't have to.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.