BookmarkSubscribeRSS Feed
gzr2mz39
Quartz | Level 8
How can I run these two lines of code 10 times where the name of input file and output file increment by 1 each time?

For example how would I combine these 6 lines into two lines of code:
filename in "C:\Documents and Settings\P0001.dta";
proc import datafile=in out=my_1 dbms = stata replace;
run;
filename in "C:\Documents and Settings\P0002.dta";
proc import datafile=in out=my_2 dbms = stata replace;
run;
filename in "C:\Documents and Settings\P0003.dta";
proc import datafile=in out=my_3 dbms = stata replace;
run;

Thank you.
2 REPLIES 2
Cynthia_sas
SAS Super FREQ
Hi:
When you want to make code generic or reusable, a good way to make that happen is through the use of the SAS Macro facility. For example, if you take the "basic" working program that you want to start with, the things that are the same are these lines...with "variable" information or "meta" information highlighted or underlined:
[pre]
filename in "C:\Documents and Settings\P0001.dta";
proc import datafile=in out=my_1 dbms = stata replace;
run;

[/pre]

A SAS Macro program could be written to use macro variables for some portion of the underlined pieces of code, which means that one possible way to solve your question would be to create a "hard-coded" macro variable called &CTR, which would stand in for some portion of the underlined code, as shown below:
[pre]
%let ctr = 1;

filename in "C:\Documents and Settings\P000&ctr..dta";
proc import datafile=in out=my_&ctr dbms = stata replace;
run;
filename in clear;

%let ctr = 2;

filename in "C:\Documents and Settings\P000&ctr..dta";
proc import datafile=in out=my_&ctr dbms = stata replace;
run;
filename in clear;

etc, etc.
[/pre]

But, in the above case, the macro variable &CTR isn't being automatically incremented. That's where moving to use of a Macro PROGRAM, you could then use a macro %DO loop to iterate &CTR from 1 to 10, so that each time through the Macro %DO loop, when the value of &CTR was substituted, you would be reading a different filename and creating a differently numbered data set. Consider this bunch of %PUT statements inside a Macro program and a %DO loop. You can see how every time through the loop, a different set of text strings is being written to the SAS log:
[pre]
**1) define macro program;
%macro impfile;
%do ctr = 1 %to 10;
%put **** ---- **** ---- **** ----;
%put show macro variable resolution in SAS log;
%put "C:\Documents and Settings\P000&ctr..dta";
%put my_&ctr ;
%end;
%mend impfile;

**2) invoke macro program;
%impfile;

[/pre]

Produces this in the SAS log:
[pre]
**** ---- **** ---- **** ----
show macro variable resolution in SAS log
"C:\Documents and Settings\P0001.dta"
my_1
**** ---- **** ---- **** ----
show macro variable resolution in SAS log
"C:\Documents and Settings\P0002.dta"
my_2
**** ---- **** ---- **** ----
show macro variable resolution in SAS log
"C:\Documents and Settings\P0003.dta"
my_3
**** ---- **** ---- **** ----
show macro variable resolution in SAS log
"C:\Documents and Settings\P0004.dta"
my_4
**** ---- **** ---- **** ----
show macro variable resolution in SAS log
"C:\Documents and Settings\P0005.dta"
my_5
**** ---- **** ---- **** ----
show macro variable resolution in SAS log
"C:\Documents and Settings\P0006.dta"
my_6
**** ---- **** ---- **** ----
show macro variable resolution in SAS log
"C:\Documents and Settings\P0007.dta"
my_7
**** ---- **** ---- **** ----
show macro variable resolution in SAS log
"C:\Documents and Settings\P0008.dta"
my_8
**** ---- **** ---- **** ----
show macro variable resolution in SAS log
"C:\Documents and Settings\P0009.dta"
my_9
**** ---- **** ---- **** ----
show macro variable resolution in SAS log
"C:\Documents and Settings\P00010.dta"
my_10

[/pre]

You need to understand some basic fundamentals of how macro processing works, how macro variables are resolved before you jump into your first Macro program. Basically, the SAS Macro facility is only "typing" code that goes to the compiler -- the SAS Macro facility does not actually EXECUTE code -- it only resolves macro variable and macro invocation references in code and then the code with the resolved references is sent on to the SAS compiler to be compiled and then executed. By the time the Macro facility is done with the code, all the macro variable references or macro invocations are gone.

A good introduction to the basics of SAS Macro processing is here:
http://www2.sas.com/proceedings/sugi28/056-28.pdf

...and a few other useful papers are here:
http://www2.sas.com/proceedings/sugi30/130-30.pdf (look at the %PRTMAC example on page 10)
http://www.nesug.org/proceedings/nesug03/bt/bt009.pdf
http://www2.sas.com/proceedings/sugi29/243-29.pdf

cynthia
monei011
Calcite | Level 5
The following is a largely macro free alternative:
/*
an alternative to SAS Macro is to use a program
that writes a program.
This example uses a datasetp to write SAS program
statements to a file, and then uses %include to
include the program file just created and run it.
You can use a "special" temporary SAS file if
you use the TEMP destination, if you don't want
the code to persist. (appropriate statement
included as a comment) The advantage of a permanent
file is you can debug what you have generated.
For many applications this approach is easier to
use and debug than SAS Macro, and you can see
code generated in a text file, not just as a
lot of disjointed text in the SAS Log
*/
filename tempcode "c:\notbackedup\testcode.sas";
/*filename tempcode TEMP;*/
data _null_;
file tempcode;
do i = 1 to 5;
put 'filename in "C:\Documents and Settings\P000' i 1. ".dta;";
put "proc import datafile=in out=my_" i 1. " dbms = stata replace;";
put "run;";
end;
run;
%include tempcode

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
  • 2 replies
  • 974 views
  • 0 likes
  • 3 in conversation