BookmarkSubscribeRSS Feed
ZRick
Obsidian | Level 7

Can macro variables created in select into be local?

  • SELECT … INTO :mac_var1


will mac_var1 be local?

3 REPLIES 3
Tom
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 ;

quit;

%put _user_;

%mend inner;

%macro outer ;

  %local var2;

  %inner;

%mend outer;

%outer;



Cynthia_sas
SAS Super FREQ


Hi:

  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.

  cynthia

proc sql;
  select age into :howold
  from sashelp.class
  where name="Philip";
quit;
%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";
quit;
     
%global gmvar;
%local lmvar;
data _null_;
  call symputx('gmvar','This one is GLOBAL',G);
  call symputx('lmvar','This one is LOCAL',L);
run;
      
%put _user_;
%mend findage;
  
options mlogic;
%findage(want=Philip)
  
%findage(want=Alfred)
options nomlogic;

  
%put after running macro;
%put _user_;

Peter_C
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 ;

%caller
%put _user_;

macro variables mac_age and mac_height are not global

but mac_age is not local to %do_it

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 3 replies
  • 9867 views
  • 2 likes
  • 4 in conversation