BookmarkSubscribeRSS Feed
Ronein
Onyx | Level 15

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.

CFC_SAS_Communities_400x225.jpg

Call for content now open!

It's your turn to help shape SAS Innovate 2027. Share your expertise and inspire the SAS community.

Submit your proposal →

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 2678 views
  • 2 likes
  • 3 in conversation