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