I am trying to run down a list of files in a directory on SAS Studio to find the size and last modified date of each file. The code below was taken from another site and simply adapted to my folder locations but I cannot get it to work. (Original solution linked at the bottom)
I am getting the below error, which I believe is because it isn't evaluating the 'dirid' and 'numfields' correctly before the DO statement. But then I don't know why... Can anyone help?
data info (keep=filename bytes crdate moddate); /* Assign the fileref 'files' to the directory location*/ filename files"/USER/01_folder"; /* Opens up the directory, dirid is now the directory id*/ dirid=dopen('files'); /* Counts the number of files in the directory*/ numfiles=dnum(dirid); /*Loop each file */ do i = 1 to numfiles; /* Identify the file as filename */ filename=dread(dirid,i); /* Find the file in the location */ file="/USER/01_folder/"||Filename; /* Assign the fileref 'fnames' to this file */ sysrc=filename('fnames',file); /* Open the File*/ fid=fopen('fileref'); /*If the file is opened */ if fid ^= 0 then do; bytes=finfo(fid,'File Size (bytes)'); moddate=finfo(fid,'Last Modified'); output; /*Close the file */ sysrc=fclose(fid); end; end; /*Close the directory */ rc=dclose(dirid); run; https://katalyzedata.com/tips-tricks/getting-the-finfo-from-your-files/
1) since the DO-Loop starts at 1, I'm guessing the "numfiles" is the problem.
Add:
put numfiles=;
after dnum() function.
Also put the filename statement outside data step.
filename files "/USER/01_folder";
data ...
...
Also check if "dirid" is not 0.
2) I can recommend you BasePlus package and the %dirsAndFiles() from the package to do the job for you. It searches recursively, it gets OS data, it has a bunch of options to set output dataset. and is OS independent.
All the best
Bart
Hi Bart, That's my main problem, neither the dirid or numfiles are being populated but I just don't understand why. (refer additional log snippet below). I also already tried moving the filename statement outside of the datastep, changed the folder path from single to double quotes etc but didn't have any luck.
NOTE: Argument 1 to function DNUM(0) at line 94 column 11 is invalid. ERROR: Invalid DO loop control information, either the INITIAL or TO expression is missing or the BY expression is missing, zero, or invalid. dirid=0 numfiles=. i=0 filename= file= sysrc=. fid=. bytes= moddate= crdate= rc=. _ERROR_=1 _N_=1
In terms of the package I dont think we have it, but I have asked our SAS team if they can check.
@sasheadache wrote:
Hi Bart, That's my main problem, neither the dirid or numfiles are being populated but I just don't understand why. (refer additional log snippet below). I also already tried moving the filename statement outside of the datastep, changed the folder path from single to double quotes etc but didn't have any luck.
NOTE: Argument 1 to function DNUM(0) at line 94 column 11 is invalid. ERROR: Invalid DO loop control information, either the INITIAL or TO expression is missing or the BY expression is missing, zero, or invalid. dirid=0 numfiles=. i=0 filename= file= sysrc=. fid=. bytes= moddate= crdate= rc=. _ERROR_=1 _N_=1In terms of the package I dont think we have it, but I have asked our SAS team if they can check.
Does the path to the directory start at the root of a drive or disc mount? If not make it so otherwise the path is going to be assumed to be relative to where SAS is executing and that almost never is where your directory starts (and likely shouldn't)
First either move the FILENAME statement before the DATA statement (or convert it to a call to the FILENAME() function instead) so that the order of the lines of code will match the order that the code actually executes.
Second you need to test that each function worked before proceeding to the next step.
Main ones for this code being :
Were you able to define the fileref?
Were you able to open the directory?
Note that If the goal is just to get the list of files in a directory tree then you could just use this macro: https://github.com/sasutils/macros/blob/master/dirtree.sas
Output dataset structure --NAME-- Len Format Description FILENAME $256 Name of file in directory TYPE $1 File or Directory? (F/D) SIZE 8 COMMA20. Filesize in bytes DATE 4 YYMMDD10. Date file last modified TIME 4 TOD8. Time of day file last modified DEPTH 3 Tree depth PATH $256 Directory name
Check if
/USER/01_folder
is the correct spelling. Is it really USER, not User or user?
All-uppercase for a directory is very rare in UNIX systems.
Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.