- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Is %PUT _ALL_ what you're looking for?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
A simple version that may do what you want: %put _user_;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Correct!
I was looking for the same and found it.
Thanks.
- Dr. Abhijeet Safai
Certified Base and Clinical SAS Programmer
Associate Data Analyst
Actu-Real
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Another option is to write the macro names to a dataset.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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