08-05-2013 02:58 PM
The goal of this macro was to take a file directory and file extension as parameters, and add all the files in the directory with that extension to a SAS data set to be processed later.
I found this code segment online: 40934 - Retrieve file size, create time, and last modified date of an external file
But it is only for one file. The properties are retrieved no problem in the code below, but the next iteration of the loop changes all the existing values in the data set. I'm guessing this is because SAS is only storing a reference to the actual value, which then changes. Is there anyway to force it to copy the actual value.
I'd really like this to be a macro but is that impossible?
Code (messy) - displays info in log and prints data set. Data set info incorrect for reasons mentioned above:
%global tfilename Bytes;
%macro fileAttribs(ofilename, dir);
%local rc fid fidc;
%local CreateDT ModifyDT;
%let Bytes=%sysfunc(finfo(&fid,File Size (bytes)));
%let CreateDT=%sysfunc(finfo(&fid,Create Time));
%let ModifyDT=%sysfunc(finfo(&fid,Last Modified));
%put NOTE: File size of &ofilename is &Bytes bytes;
%put NOTE: Created &CreateDT;
%put NOTE: Last modified &ModifyDT;
/* Assigns the fileref of mydir to the directory and opens the directory */
/* Returns the number of members in the directory */
/* Loops through entire directory */
data filespec; /* Create data set to capture file name and size */
length ffname$ 30 ffsize$ 10;
%do i = 1 %to &memcnt;
/* Returns the extension from each file */
/* Checks to see if file contains an extension */
%if %qupcase(%qsysfunc(dread(&did,&i))) ne %qupcase(&name) %then
/* Checks to see if the extension matches the parameter value */
/* If condition is true prints the full name to the log */
%if (%superq(ext) ne and %qupcase(&name) = %qupcase(&ext)) or
(%superq(ext) = and %superq(name) ne) %then
run; /* Close local dataset filespec */
/* Closes the directory */
%drive( <DIRECTORY>, xls )
proc print data = filespec;
title 'Created data set filespec';
08-05-2013 03:36 PM
You'll need to get the list of files before you start retrieving their properties.
What operating system are you working on?
What command would you give that operating system to get a list of all files in a folder?
Are you running the program in batch mode or in some sort of interactive mode?
That should be enough to get things started.
08-05-2013 03:36 PM
Please explain why you do wat to have it as SAS-macro.
I am missing the reason for that. As it could be having learned Word-macros. Thinking SAS programming is macros?
You are describing the goal of a dataset having the filenames
The easiest way is using the filename option and wildcarding with the infile statement. SAS(R) 9.4 Companion for Windows (infile wildcars)
SAS(R) 9.4 Statements: Reference (infile filename). Having the filename you can use the datastepfunctions (no %sysfunc) delivering the additional fileinformation.
The datastep is able to read all files and creating the SAS-dataset and the SAS-dataset containing just the filenames in one pass.
08-05-2013 04:23 PM
You probably want to use
DOPEN(), DNUM() and DREAD() to get the list of filenames.
Then you could nest calls to FOPEN(), FOPTNUM() etc to get the attributes for each file.
Read the manual pages to get started.
08-14-2013 03:35 PM
I was able to find macro code to do exactly what I wanted. Here it is :
%macro getFileSizes (directory, extension, dataSet);
data &dataSet(drop=fid ff filrfb rc);
%if &memcount > 0 %then %do i=1 %to &memcount;
%if %scan(&&flname&i,-1,.) = &extension or &extension = all %then %do;
size=finfo(fid,'File Size (bytes)');
Addendum : I wanted to use macro code because I took the SAS course 'SAS Macro Language 2' three years ago and we did some very similar programming in that course. That course was very useful in enhancing my SAS programming skills.
Thanks to all who gave me suggestions.
08-14-2013 09:34 PM
You seem to have mixed macro and data step code.
All you need the macro to do is supply the values of the DIRECTORY, EXTENSION and DATASET parameters.
The rest of the logic will be clearer if you call dopen, dread, etc. functions directly in the data step code.
%macro getfilesizes(directory,extension,dataset) ;
data &dataSet ;
length file $256 dte 8 size 8 directory $256;
format dte datetime.;
keep file dte size directory ;
length filrf filrfb $8 ;
do i=1 to memcount;
file = dread(did,i);
if scan(file,-1,'.') = "&extension" or "&extension" = "all" then do;
size=input(finfo(fid,'File Size (bytes)'),32.);
proc print width=min;
03-09-2018 12:25 PM
I had the same issue and was able to resolve it by changing the \ to a / on the following line: