BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
SASGeek
Obsidian | Level 7

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

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

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.

View solution in original post

2 REPLIES 2
Kurt_Bremser
Super User

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.

SASGeek
Obsidian | Level 7
Thank you. I was worried that would be the answer but at least I know where to go now.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

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!

Register Now

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 1475 views
  • 1 like
  • 2 in conversation