BookmarkSubscribeRSS Feed
nickspencer
Obsidian | Level 7
Hi,

I have this macro which I need to repeat multiple times so want to loop it instead.


Sample 1:

%macro check_list;
%global list1;
%if list1= % then %do;
%let list1=“myname”;

....

%end;
%mend check_list;
%check_list;

Data test1;
Set list1;
Run;


I need to repeat the same code for list2, list3 and list4 and also create data1, data2 , data3, data4.

I tried to wrap the sample1 Code using.
%macro loop;
%do i = 1 %to 4;

Sample1(replaced list1 with list& and data1 with data&i)

Error: Attempt to %global a name(list2) which exists in a local environment.

Any suggestions ?
5 REPLIES 5
PaigeMiller
Diamond | Level 26

@nickspencer wrote:

Any suggestions ?

Yes, show us the actual code and SASLOG that you are using. Run the code again with OPTIONS MPRINT; as the first line and then paste the SASLOG into the {i} window; and paste the actual code into the running man window.

--
Paige Miller
error_prone
Barite | Level 11
Yes, use the search facility. It has been explained so many times that another repetition is useless. Remember that before you start creating a macro it is wise to have working code.
art297
Opal | Level 21

You don't need a global macro variable.

 

If your code is as basic as in your example then just wrap it in a macro with a %do loop. e.g.:

%macro check_list;
  %do i=1 %to 4;
    Data test&i;
      Set list&i.;
    Run;
  %end;
%mend;


Art, CEO, AnalystFinder.com

 

Astounding
PROC Star

Somehow, your code creates a local macro variable named LIST2. 

 

But nobody can help beyond that.  It's in your macro code somewhere, and only you can see that code.  If you were to share it, you might get a more specific answer.

ballardw
Super User

Do you really need list1 to be global?

Also you can create the macro such that a parameter that you do not explicitly assign has a default value:

Instead of : %macro check_list;

 

use: %macro(List1=myname);

the Default value of LIST1 inside the macro then becomes myname.

 

Be extremely careful of quotes as part of macro variables. Often they make coding much harder. The actual code and how LIST1 is used outside of the macro would help.

 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 5 replies
  • 701 views
  • 0 likes
  • 6 in conversation