BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
cosmid
Lapis Lazuli | Level 10

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

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

View solution in original post

5 REPLIES 5
Tom
Super User Tom
Super User

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
cosmid
Lapis Lazuli | Level 10
Thank you! That was a very thorough explanation!
ballardw
Super User

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.

Kurt_Bremser
Super User

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.
SAS-Nutzer
Fluorite | Level 6

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.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 5 replies
  • 1135 views
  • 6 likes
  • 5 in conversation