BookmarkSubscribeRSS Feed
sasheadache
Obsidian | Level 7

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?

 

"ERROR: Invalid DO loop control information, either the INITIAL or TO expression is missing or the BY expression is missing, zero, or invalid."

 

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/

 

5 REPLIES 5
yabwon
Onyx | Level 15

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

 

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



sasheadache
Obsidian | Level 7

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.

ballardw
Super User

@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_=1

 In 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)

Tom
Super User Tom
Super User

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

 

 

Kurt_Bremser
Super User

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.

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!

SAS Enterprise Guide vs. SAS Studio

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.

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
  • 5 replies
  • 268 views
  • 0 likes
  • 5 in conversation