BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
OS2Rules
Obsidian | Level 7

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.

1 ACCEPTED SOLUTION

Accepted Solutions
paulrichmay
Calcite | Level 5

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

View solution in original post

10 REPLIES 10
Tim_SAS
Barite | Level 11

Is %PUT _ALL_ what you're looking for?

SAS(R) 9.2 Macro Language: Reference

OS2Rules
Obsidian | Level 7

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.

Tom
Super User Tom
Super User

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;

Astounding
PROC Star

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

DrAbhijeetSafai
Pyrite | Level 9

Correct!

 

I was looking for the same and found it.

 

Thanks.

 

- Dr. Abhijeet Safai

Dr. Abhijeet Safai
Associate Data Analyst
Actu-Real
ballardw
Super User

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.

Fugue
Quartz | Level 8

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

Astounding
PROC Star

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.

FriedEgg
SAS Employee

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

paulrichmay
Calcite | Level 5

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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
  • 10 replies
  • 28152 views
  • 8 likes
  • 9 in conversation