BookmarkSubscribeRSS Feed
Ganeshk
Obsidian | Level 7

Hi Team,

 

I want to create dataset with list of all files names present in that data set and there format.

 

Ex: In my D-Drive under the folder called "Analytics" i have these files:

 

Path: "D:\Analytics"

 

Files Type
File1 .csv
File2 .sas
File3 .xls
File4 .sas
File5 .csv

 

How to create all the listed files with there format as a single data set?

 

Regards,

Ganesh K

9 REPLIES 9
ErikLund_Jensen
Rhodochrosite | Level 12

Hi ganeshk

 

You can use this macro. The informats ddmmyy10. and commax18. work on a danish windows installation and should be changed to read whatever t

%macro dirlist(dsout,folder,sub=0);
	%if &sub = 1 %then %let recursive = /s;
	%else %let recursive =;

	filename tmp pipe "dir ""&folder"" &recursive /A-D /t:w";

	data &dsout (drop=rec upddato updtime);  	
		length folder file $255 extension $30 updated bytes 8;
		retain folder;
		format updated datetime.;
		infile tmp truncover end=eof;
		input rec $char300.;
		if substr(rec,1,13) = ' Directory of' then folder = substr(rec,15);
		else if substr(rec,1,1) ne '' then do;
			file = substr(rec,37);
			upddato = input(scan(rec,1,' '),ddmmyy10.);
			updtime = input(scan(rec,2,' '),time5.);
			updated = dhms(upddato,hour(updtime),minute(updtime),0);
			bytes =  input(scan(rec,3,' '),commax18.0);
			extension = scan(trim(file),-1,'.');
			output;
		end;
	run;
	filename tmp clear;
%mend;
%dirlist(temp,C:\Users\erlu\AppData\Roaming\SAS,sub=1);

he windows DIR command returns in your environment. Use parm. sub=1 if you want to traverse subdirectories as well.

Ganeshk
Obsidian | Level 7

Hi Jensen,

 

I didn't get proper output, below is the output which i got.

 

image.png

 

Thanks,

Ganesh K

 

AMSAS
SAS Super FREQ

Here's a simple example, that will print a list of files to the log, and creates the dataset test 

 

filename myDir "<your directory>" ;

data test (keep=filename);
	did=dopen("myDir") ;
	filecount=dnum(did) ;
	do i=1 to filecount ;
		filename=dread(did,i) ;
		put filename= ;
		output ;
	end ;
	rc=dclose(did) ;
run ;
Ganeshk
Obsidian | Level 7

Hi Amsas,

 

Almost we got the output, Below is the screen shot:

 

image.png

 

But i want format to be listed separate column. And if the there is no format,in this case ex: Input then these are folder, which need to be mention as folder in format.

 

Thanks,

Ganesh K

 

AMSAS
SAS Super FREQ

Sorry, I assumed you would be able to manipulate the output to get what you needed. 
Here's a version with the code to extract the extension and filename - I'll leave you to figure out how to handle files without an extension

data test (keep=filename file ext);
	did=dopen("myDir") ;
	filecount=dnum(did) ;
	do i=1 to filecount ;
		filename=dread(did,i) ;
		put filename= ;
		ext=trim(scan(filename,-1,".","r")) ;
		lext=length(ext) ;
		lfil=length(filename) ;
		put _all_ ;
		file=substr(filename,1,lfil-lext-1) ;
		output ;

	end ;
	rc=dclose(did) ;
run ;

 

cannona3
Calcite | Level 5
Thanks, AMSAS! Exactly what I was trying to accomplish.
Ksharp
Super User

Actually it is OS command task.

I would issue the command like 'dir /s /b d:\analysis\*.*  > c:\temp\list.log ' ,

that command would save the resule into file list.log . And I will read list.log with SAS to get what you need.

ErikLund_Jensen
Rhodochrosite | Level 12

@Ganeshk 

 

What Ksharp suggests, is is principle what my macro is doing, except for an explitit named file,  so changing from pipe filename to a text file would make no difference. The SAS data step gets the same input.

 

The macro takes the following format without problems:

 

dircmd.gif

 

I guess your directory output is different somehow, as you do not get any content in the folder column. Try to write to the log what the DIR command returns, and copy that part of the log and post it to me. Add a put statement after the input statement:

 

		input rec $char300.;
		put rec;

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!

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
  • 9 replies
  • 20506 views
  • 7 likes
  • 5 in conversation