DATA Step, Macro, Functions and more

INFILE multiple files and file name into data set

Reply
Frequent Contributor
Posts: 133

INFILE multiple files and file name into data set

Hello SAS Experts,

 

Is there a way to read the file name acutally processed and put it in the first column of the dataset?

 

data Import_Import;
infile datalines;
length fil2read $40;
input fil2read $;
infile dummy filevar=fil2read end=done DSD
        DLM=';';
do while(not done);
INPUT
            A               : $CHAR11.
            B               : $CHAR11.
            C               : $CHAR11.
            D               : $CHAR100.
            E               : $CHAR96.
            F               : $CHAR1.
            G               : $CHAR6.
            H               : $CHAR2.
            I               : ?? COMMAX4.
            J              : $CHAR11.
            K              : $CHAR2.
            L              : ?? YYMMDD10.
            M              : $CHAR17.
            N              : $CHAR4.
            O              : $CHAR1.
            P              : $CHAR4.
            Q              : $CHAR1.
            R              : $CHAR1.
            S              : $CHAR4.
            T              : $CHAR1.
            U              : $CHAR1.
            V              : $CHAR1.
            W              : $CHAR1.
            X              : $CHAR4.
            Y              : $CHAR1.
            Z              : $CHAR1. ;
  output;
end;
datalines;
'C:\Users\x\test\H20154.csv'
'C:\Users\x\test\H20154.csv'
'C:\Users\x\test\B20154.csv'
'C:\Users\x\test\H20154.csv'
'C:\Users\x\test\N20154.csv'
;

Super User
Posts: 19,789

Re: INFILE multiple files and file name into data set

Use the FILENAME option in the infile statement. You should also make sure it's long enough to hold the path.

 

 

length filename $256.;

infile dummy filevar=fil2read filename=filename end=done DSD
        DLM=';';

source=filename;

 

This post may be of interest to you, especially if you have header rows in your files.

 

https://communities.sas.com/t5/SAS-Communities-Library/How-do-I-write-a-macro-to-import-multiple-tex...

Super User
Posts: 11,343

Re: INFILE multiple files and file name into data set

You were almost there. Since the FILEVAR is a temporary variable you need to assign it to a datastep variable.

data Import_Import;
   length ReadFile $ 40.;
   infile datalines;
   length fil2read $40;
   input fil2read $;
   infile dummy filevar=fil2read end=done DSD
        DLM=';';
   ReadFile = Fil2read;
   do while ..
Ask a Question
Discussion stats
  • 2 replies
  • 414 views
  • 0 likes
  • 3 in conversation