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)
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.
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.