useR
useR

Reputation: 3082

filevar infile statement to read in multiple txt file

from this link i learnt how to read multiple txt file.

Problem: is it possible to create a macro variable to input all txt file in a folders. say C:\Users\Desktop\ (given all files are in txt format with name datasetyyyymmdd.)

I have dataset20150101.txt - dataset20150806.txt and i do not want to manually input all those linkage in the datalines.

data whole2;
    infile datalines;
    length fil2read $256;
    input fil2read $;
    infile dummy filevar=fil2read end=done dsd;
    do while (not done);
        input name$ value1 value2;
        output;
    end;
    datalines;
C:\Users\Desktop\dataset20150501.txt
C:\Users\Desktop\dataset20150502.txt
run;

Upvotes: 1

Views: 969

Answers (1)

Dirk Horsten
Dirk Horsten

Reputation: 3845

Ask the operating system which files are present:

filename DataIn pipe "dir C:\Users\Desktop\dataset*.txt /S /B";   

data whole2;
    infile DataIn truncover;
    length fil2read $256;
    input fil2read $;
    infile dummy filevar=fil2read end=done dsd;
    do while (not done);
        input name$ value1 value2;
        output;
    end;
run;

The Bare option /B removes unneeded information like last access date.

I added the Sub-folder option /S because then the dir statement returns full path names. This way it also reads dataset*.txt files in subfolder of C:\Users\Desktop\. If that does not suite you, remove the /S and use

    path2Read = "dir C:\Users\Desktop\"||fil2read;

Upvotes: 3

Related Questions