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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 794 views
  • 4 likes
  • 3 in conversation