BookmarkSubscribeRSS Feed
mbas
Calcite | Level 5

 

Hi!

I have the files 123.txt, 456.txt and 789.txt and a program like this:

 

*******************************************************

...

%let n= 123.txt;

...

*******************************************************

And I need to replace with 456.txt and 789.txt, and run again. 

 

 

I'm trying this:

 

*****************************************************

%macro mb(stg);

 

...

%let n= &stg..txt;

...

 

%mend;

 

%let gball="123 456 789";

 

%do %while (&k<=3);

%let gb=scan(&gball,&k);

%mb(&gb);

%let k=k+1;

 

%end;

 

*****************************************************

 

You can note that I'm not very expert with macro, so any comment is very usefull.

 

 

Thanks for yours answers!!

 

 

 

 

4 REPLIES 4
PaigeMiller
Diamond | Level 26

Your %do loop cannot be in open code, it can only work inside a macro.

 

You haven't stated exactly what the problem is with this code, so ... what is the problem? 

--
Paige Miller
Reeza
Super User

What exactly is your question? It's not clear what you need help with here.

 

Example 1, 3, 4 may be helpful to you here:

https://communities.sas.com/t5/SAS-Communities-Library/SAS-9-4-Macro-Language-Reference-Has-a-New-Ap...

 

If you're importing multiple text files that are all the same, see this post as well:

https://communities.sas.com/t5/SAS-Communities-Library/How-do-I-write-a-macro-to-import-multiple-tex...

 

Last but not least, look at CALL EXECUTE for a simple way to call a macro multiple times. 

I usually use a pipe to get the list of files into a data set, then use CALL EXECUTE to call the macro once for each file. 

 

Spoiler

@mbas wrote:

 

Hi!

I have the files 123.txt, 456.txt and 789.txt and a program like this:

 

*******************************************************

...

%let n= 123.txt;

...

*******************************************************

And I need to replace with 456.txt and 789.txt, and run again. 

 

 

I'm trying this:

 

*****************************************************

%macro mb(stg);

 

...

%let n= &stg..txt;

...

 

%mend;

 

%let gball="123 456 789";

 

%do %while (&k<=3);

%let gb=scan(&gball,&k);

%mb(&gb);

%let k=k+1;

 

%end;

 

*****************************************************

 

You can note that I'm not very expert with macro, so any comment is very usefull.

 

 

Thanks for yours answers!!

 

 

 

 


 

 

Reeza
Super User
And assuming you are trying to write a macro, first get your base case working, that is, what does the code look like for a single file. Once you have that working it's relatively trivial to convert it to a macro.
Astounding
PROC Star

Besides needing a macro to use %DO, you also need to get rid of the quotes, and you need to use the macro function (%scan) instead of the DATA step function (scan).  Macro language does not use quotes to identify strings of characters.  In combination, the new macro might look like this:

 

%macro loop (gball=);

   %local k;

   %do k=1 %to %sysfunc(countw(&gball));

      %mb (%scan(&gball, &k))

   %end;

%mend loop;

 

Then call it with (for example):

 

%loop (gball=123 456 789)

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
  • 4 replies
  • 342 views
  • 3 likes
  • 4 in conversation