BookmarkSubscribeRSS Feed
Obsidian | Level 7

Can macro variables created in select into be local?

  • SELECT … INTO :mac_var1

will mac_var1 be local?

Super User Tom
Super User

If you are inside of a macro AND if the macro variables have not defined before then they will be local.  But if the macro variables already existing (either global or local) then the existing macro variable will get the value.

In this example VAR1 will be GLOBAL, VAR2 will be local with scope of macro OUTER. The other macro variables will be local with scope of the macro INNER.

%let var1=global;

%macro inner;

proc sql noprint ;

  select name into :var1-:var100 from sashelp.class ;


%put _user_;

%mend inner;

%macro outer ;

  %local var2;


%mend outer;




  It sort of depends. You can run the code below and review what you see in the SAS log. The macro variable HOWOLD created out in "open code" is global in scope by default. A "local" symbol table only exists for the duration of a macro program. For example, if you are just running PROC SQL and no macro program is involved, then there is no local table, only the global table. So you couldn't put a macro variable into the local table if it doesn't exist. On the other hand, if you create a macro variable INSIDE a macro program, unless you explicitly put the macro variable into the global table, it will be local in scope. The rules of global and local scope are outlined very nicely in the macro documentation and the %GLOBAL and %LOCAL statements exist to help you declare scope if you explicitly need to. In addition, CALL SYMPUTX allows you to specify the global or local placement of a macro variable.


proc sql;
  select age into :howold
  from sashelp.class
  where name="Philip";
%put ---- INTO in open code, not in macro program;
%put _user_;
%macro findage(want=);
proc sql;
  select age into :theage
  from sashelp.class
  where name="&want";
%global gmvar;
%local lmvar;
data _null_;
  call symputx('gmvar','This one is GLOBAL',G);
  call symputx('lmvar','This one is LOCAL',L);
%put _user_;
%mend findage;
options mlogic;
options nomlogic;

%put after running macro;
%put _user_;

Rhodochrosite | Level 12

not sure why I feel I have to add this , but ... since no-one has pointed it out

If your macro invokes SELECT  ....... INTO :mac_var1

then it might not be as _local_ as you expected

%macro do_it( variable, data= sashelp.class ) ;
proc sql noprint ;
select max(&variable) into :mac_&variable  from &data ;
quit ;
%put _user_ ;
%mend  do_it ;

%macro caller ;
%let mac_age = 1 ;
%do_it( age ) ;
%do_it( height ) ;
%mend  caller ;

option mprint ;

%put _user_;

macro variables mac_age and mac_height are not global

but mac_age is not local to %do_it

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

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 4 in conversation