BookmarkSubscribeRSS Feed
Ronein
Meteorite | Level 14

Hello

I want to create a data set with one column with names of files exists in a folder.

I run this code and get error.

NOTE: Argument 1 to function DNUM(0) at line 158 column 13 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.
fref=#LN00027 fname= did=0 i=1 _ERROR_=1 _N_=1

 

There is a problem with  do statement (dnum(did)).

Instead of dnum(did)  I wrote 20 (just for example) and then I recieve a data set with column fname with 20 rows but all of them with null values .

May anyone help please?

data filenames;
length fref $8 fname $200;
did = filename(fref,'\\path\');
/*creates a file reference for the directory */
did = dopen(fref);
/*opens the directory */
/*creates a handle -DOPEN*/
do i = 1 to dnum(did);
  fname = dread(did,i);
  output;
end;
/*loops over all (DNUM) entries in the directory*/
/*and  reads each (DREAD)*/
did = dclose(did);
did = filename(fref);
/*At the end, it deassigns the handle and the file reference*/
keep fname;
run;
2 REPLIES 2
singhsahab
Lapis Lazuli | Level 10

@Ronein here you can go just updating the path in macro variable.

 

 %LET PATH=;

 

%LET PATH=;

DATA filenames;
 RC=FILENAME("FILEREF","&PATH.");
 MYDIR=DOPEN("FILEREF");
 IF MYDIR > 0 THEN 
    NUM=DNUM(MYDIR);
	 DO I=1 TO NUM;
			IF I > 0 THEN DO
			   FILERE2=DREAD(MYDIR,i);
			   OUTPUT;
	END;
	END;
rc=dclose(mydir);
RUN;
Kurt_Bremser
Super User

did=0 means that the file cannot be found, or is not a directory.

I suspect that your SAS session does not have access to the same shared network resource that you use from your desktop.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 2 replies
  • 891 views
  • 2 likes
  • 3 in conversation