Question

I have a list of 17 flat files that I'm trying to import into different data sets. All of the files have the same data step, so I'm trying to write a do while loop to import all the files.

I've been trying to adapt some code from here without success: http://www.sas.com/offices/europe/uk/support/sas-hints-tips/tips-enterprise-csv.html

http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/viewer.htm#a000543785.htm

I'm getting an error that says the %do statement is not valid in open code. Here is my code:

% let path1 = 'c:\path1'
% let path2 = 'c:\path2'
...
% let pathN = 'c:\pathN' 

%let n=1; 

%do %while (&n <= 17);
%let pathin = &path&n;

  data retention&n;
    infile &pathin;
    <data step-->
  run;

%let n=%eval(&n+1); 
%end;

I've tested the data step outside of the do-while loop and it works fine for 1 file at a time using the %let pathin = &path&n code. The code still writes the datafile for the 1st data set; but, I need to be able to loop through all the files and can't figure out how. Sorry if this is a novice question; I'm just learning SAS.

Thanks, -Alex

Was it helpful?

Solution

Welcome to SAS programming! The error message you got is a clue. "Open code" refers to statements that are executed directly by the SAS system. A %do statment is part of the SAS Macro Language, not "normal" SAS. A %let statement can be executed in open code and is use to create a macro variable (distinct from a compiled macro).

Compiled SAS macros are created by code that appears between the %macro and %mend statements. For example, using your code:

%macro run_me;
%let n=1; 

%do %while (&n <= 17);
%let pathin = &path&n;

  data retention&n;
    infile &pathin;
    <data step-->
  run;

%let n=%eval(&n+1); 
%end;
%mend;

But all that does is define/compile the macro. To execute it, you must issue the statement %run_me;. Note that the name run_me was just a name I made up.

For more info, please consult the SAS Macro Reference, especially the introductory section.

OTHER TIPS

To convert your progma to macro, turn your macro variables declared by LET statement into macro arguments:

%macro readfile(n, pathin);

  data retention&n;
    infile &pathin;
    <data step-->
  run;

%mend;

A data step to repetitively call your macro. Here the data included in CARDS statement, but also can be read from some table via SET statement. The macro call is performed via call execute routine.

data _null_;
length path $200 stmt $250;
input path;
stmt = catt('%readfile(', putn(_N_, 3. -L), path, ')');
call execute(stmt);

cards;
c:\file1.txt
c:\file2.txt
c:\file3.txt
;
run;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top