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.
Editor's Note: Lots of helpful and useful suggestions in this thread. Thanks to those who commented. This one leverages the GLOBAL macro information and then shows how to specifically locate the subset of variables of interest. Thanks, @paulrichmay .
Hi,
This code worked for me. Just pulls the data set where SAS stores the macro variables. Restricting to 'GLOBAL' drops the 'ATUOMATIC' vars created by SAS. Same for the if statements. Those can be dropped depending on what you've named your variables.
Hope this helps.
data macro_vars;
set SASHELP.VMACRO;
where scope = 'GLOBAL';
if substr(name, 1, 1) = '_' then delete;
if substr(name, 1, 3) in ('SAS', 'SQL', 'SYS') then delete;
drop scope offset;
run;
Thanks,
Paul
Is %PUT _ALL_ what you're looking for?
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.
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;
A simple version that may do what you want: %put _user_;
Correct!
I was looking for the same and found it.
Thanks.
- Dr. Abhijeet Safai
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.
Another option is to write the macro names to a dataset.
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.
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
Editor's Note: Lots of helpful and useful suggestions in this thread. Thanks to those who commented. This one leverages the GLOBAL macro information and then shows how to specifically locate the subset of variables of interest. Thanks, @paulrichmay .
Hi,
This code worked for me. Just pulls the data set where SAS stores the macro variables. Restricting to 'GLOBAL' drops the 'ATUOMATIC' vars created by SAS. Same for the if statements. Those can be dropped depending on what you've named your variables.
Hope this helps.
data macro_vars;
set SASHELP.VMACRO;
where scope = 'GLOBAL';
if substr(name, 1, 1) = '_' then delete;
if substr(name, 1, 3) in ('SAS', 'SQL', 'SYS') then delete;
drop scope offset;
run;
Thanks,
Paul
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.