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 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.;
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.
@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?
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 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.