Hello all,
I have a Linux file location where we store our output files for processing. What I want to do is look into that Linux location and list ALL the .txt files in there and read them in as separate SAS datasets. The core files names are the same WEEKLY_SALES_yyyymmdd with the yyyymmdd varying with no patters (sometimes weekly, sometimes daily depending on the needs).
How can I list all those .txt files and import them into SAS? There could be as many as 150 .txt files in the folder.
After I assign the libref, then what?
libname sales '/sasdata/data/UX/sales/';
Thank you
Paula
Assigning a LIBNAME won't do you any good, as these are not SAS files which the BASE engine will recognize
Read the filenames into a dataset:
data filenames;
length dref $8 fname $200;
rc = filename(dref,'/sasdata/data/UX/sales');
did = dopen(dref);
if did then do i = 1 to dnum(did);
fname = dread(did,i);
if index(fname,'WEEKLY_SALES_') = 1 and scan(fname,-1,'.') = 'txt' then output;
end;
keep fname;
run;
But if all files have the same structure, you can read them in one data step:
data all;
length fname filename $200;
infile '/sasdata/data/UX/sales/WEEKLY_SALES_*.txt' filename=fname;
input
/* insert variables here */
;
filename = fname;
run;
You may need to add code for skipping header lines. The variable filename will contain the name of the file from which an observation originates.
Assigning a LIBNAME won't do you any good, as these are not SAS files which the BASE engine will recognize
Read the filenames into a dataset:
data filenames;
length dref $8 fname $200;
rc = filename(dref,'/sasdata/data/UX/sales');
did = dopen(dref);
if did then do i = 1 to dnum(did);
fname = dread(did,i);
if index(fname,'WEEKLY_SALES_') = 1 and scan(fname,-1,'.') = 'txt' then output;
end;
keep fname;
run;
But if all files have the same structure, you can read them in one data step:
data all;
length fname filename $200;
infile '/sasdata/data/UX/sales/WEEKLY_SALES_*.txt' filename=fname;
input
/* insert variables here */
;
filename = fname;
run;
You may need to add code for skipping header lines. The variable filename will contain the name of the file from which an observation originates.
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.