Using macro with text files

Reply
Frequent Learner
Posts: 1

Using macro with text files

 

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!!

 

 

 

 

Respected Advisor
Posts: 2,516

Re: Using macro with text files

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
Super User
Posts: 22,525

Re: Using macro with text files

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!!

 

 

 

 


 

 

Super User
Posts: 22,525

Re: Using macro with text files

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.
Super User
Posts: 6,362

Re: Using macro with text files

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)

Ask a Question
Discussion stats
  • 4 replies
  • 72 views
  • 3 likes
  • 4 in conversation