BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Aexor
Lapis Lazuli | Level 10

Hi All,

 

Help needed in below question:

 

A unix directory contains  30 csv files . i want to read the 27th file csv.

 

Please help.

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

You will have to read all filenames into a dataset, and sort that by name, then you can get the 27th with a POINT= option:

data fnames;
length dref $8 fname $200;
rc = filename(dref,"/folders/myfolders");
did = dopen(dref);
if did
then do i = 1 to min(dnum(did),27);
  fname = dread(did,i);
  output;
end;
if i le 27 then putlog "Not enough files!";
keep fname;
run;

proc sort data=fnames;
by fname;
run;

data _null_;
pt = 27;
set fnames point=pt;
call symputx('myfile',fname);
stop;
run;

%put &myfile.;

View solution in original post

4 REPLIES 4
Kurt_Bremser
Super User

In a data step, open the directory (assign a file reference with the FILENAME function, then use that in a DOPEN call). 

Now you can loop over the contents of the directory (DREAD, up to the value returned by the DNUM function), and assign the returned filename to a macro variable (CALL SYMPUTX) when the counter hits 27.

Tom
Super User Tom
Super User

@Aexor wrote:

Hi All,

 

Help needed in below question:

 

A unix directory contains  30 csv files . i want to read the 27th file csv.

 

Please help.

 

 


You need to provide a lot more detail about what you are doing. 

 

Do you just want to find the name of the 27th file?  How are you ordering the files to determine which is the 27th?

Do you want to actually read the file once you have found its name?  Do you know the fields that are supposed to be in the CSV file?  Or will you have to use a tool such as PROC IMPORT to guess how to read the file into data?

Aexor
Lapis Lazuli | Level 10
Hi Tom,

Thanks for checking.
Yes the files are in sequential order a.txt, b.txt and so on .so I want to read the 27th file and filelds are same in all files e.g name,class,number
Kurt_Bremser
Super User

You will have to read all filenames into a dataset, and sort that by name, then you can get the 27th with a POINT= option:

data fnames;
length dref $8 fname $200;
rc = filename(dref,"/folders/myfolders");
did = dopen(dref);
if did
then do i = 1 to min(dnum(did),27);
  fname = dread(did,i);
  output;
end;
if i le 27 then putlog "Not enough files!";
keep fname;
run;

proc sort data=fnames;
by fname;
run;

data _null_;
pt = 27;
set fnames point=pt;
call symputx('myfile',fname);
stop;
run;

%put &myfile.;

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 4 replies
  • 1020 views
  • 4 likes
  • 3 in conversation