DATA Step, Macro, Functions and more

importing many files

Reply
Regular Contributor
Posts: 196

importing many files

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.
SAS Super FREQ
Posts: 8,743

Re: importing many files

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
Occasional Contributor
Posts: 12

Re: importing many files

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
Ask a Question
Discussion stats
  • 2 replies
  • 221 views
  • 0 likes
  • 3 in conversation