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!!
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?
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:
If you're importing multiple text files that are all the same, see this post as well:
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.
@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!!
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)
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.