DATA Step, Macro, Functions and more

List All Macro Variables Created

Reply
Super Contributor
Posts: 358

List All Macro Variables Created

Hi All:

I tend to stay away from macro variables, but sometimes you just have to....

I create "x" number of macro varaibles with this loop:

DO I=1 TO months by 1;

  call symput("month"||trim(left(put(i,3.))),put(intnx(months',startdate,i-1,'E'),date9.));                                  
  END; 

Is there something simple that I can use to list the contents of all the variables created?  Usually

I just use a "%put varname", but that won't work in this case because I don't know how many

variables are created.

Thanks in advance.

Super Contributor
Posts: 394

Re: List All Macro Variables Created

Is %PUT _ALL_ what you're looking for?

SAS(R) 9.2 Macro Language: Reference

Super Contributor
Posts: 358

Re: List All Macro Variables Created

I'm actually using %put _global_ which gives me a shorter list, but I was looking for something that

would only list the macro variables created by the loop.

Super User
Super User
Posts: 6,497

Re: List All Macro Variables Created

To generalize you could save a copy of the dictionary table before the block of code you want to test and then pull the differences after the code block. 

%symdel x1 ;

%symdel x2 ;

proc sql noprint ;

create table save as select * from dictionary.macros ;

quit;

%let x1=x1;

%let x2=x2;

proc sql ;

select * from dictionary.macros

where scope='GLOBAL'

except select * from save

;

quit;

Super User
Posts: 5,071

Re: List All Macro Variables Created

A simple version that may do what you want:  %put _user_;

Super User
Posts: 10,466

Re: List All Macro Variables Created

proc sql;   select * from dictionary.macros; quit; Will show all of the macro variables. Add an appropriate where to look for Name starting with "month" should get you there.

Super Contributor
Posts: 307

Re: List All Macro Variables Created

Another option is to write the macro names to a dataset.

Super User
Posts: 5,071

Re: List All Macro Variables Created

OK, now here's the exact way.  Insert into your DO loop, just before the END:

call execute('%put month' || trim(left(put(i,3.))) || ' is &month' || trim(left(put(i,3.))) || ';');

Good luck.

Trusted Advisor
Posts: 1,300

Re: List All Macro Variables Created

data _null_;

do x=1 by 1 until(done);

set sashelp.class end=done;

call symputx( cats('name',x) , name );

end;

call symputx( 'name0' , x );

run;

%macro loopd;

%do x=1 %to &name0;

%put &&name&x;

%end;

%mend;

%loopd

Ask a Question
Discussion stats
  • 8 replies
  • 328 views
  • 3 likes
  • 7 in conversation