Issue 27 - June 2007 - SAS UK - In The Know - Enewsletter
Dynamically determine the creation date and last modified date for an external file
This example uses the DIR command and FILENAME PIPE statement to dynamically determine the creation and last modified dates of an external file. If you are running on UNIX then use the LS command with the FILENAME PIPE statement.
Sample 1 and Sample 2 use the DIR command to obtain information about an external file on Windows. You can modify this code to use other attributes as documented in the Windows HELP under the DIR command.
Sample 3 uses the LS command to get the last modified date of an external file on the UNIX operating system.
Sample 1: returns a file's creation date
/* /t:c - indicates you want a time field 'T', of type creation 'C' */
/* /a:-d - return information for files only, not directories */
%let file=c:\tracks\dennis\x1.txt;
filename foo pipe "dir &file /t:c /a:-d ";
data _null_;
infile foo firstobs=6;
/* The ?? format modifier for error reporting suppresses printing the messages */
/* and the input lines when SAS encounters invalid data values. The automatic */
/* variable _ERROR_ is not set to 1 for the invalid observation. */
/* The & format modifier enables you to read character values that contain */
/* embedded blanks with list input and to specify a character informat. SAS */
/* reads until it encounters multiple blanks. */
input cr_date ?? :mmddyy8. cr_time ?? & time8.;
if cr_date eq . then stop;
put cr_date= worddate. / cr_time= timeampm.;
run;
Sample 2: Returns a file's last modified date on Windows
/* /t:w indicates you want a time field 'T', of type Last Modified 'W' */
/* /a:-d return information for files only, not directories */
filename foo pipe "dir &file /t:w /a:-d";
data _null_;
infile foo firstobs=6;
input mod_date ?? : mmddyy8. mod_time ?? & time8.;
if mod_date eq . then stop;
put mod_date= worddate. / mod_time= timeampm.;
run;
Sample 3: Returns a file's last modified date on UNIX
/* ls is the UNIX equivalent of DIR */ /* -g specifies not to print the file's owner */ /* -o specifies not to print the file's group */ /* Note that if the time of last modification is greater than */ /* six months, the year is substituted for the hour and minute */ /* of the modification. */ filename foo pipe "ls -g -o ~/test.txt"; data _null_; infile foo firstobs=2; input @24 mod_date $12. ; if mod_date=" " then stop; put mod_date= ; run;
Click here to visit www.sas.com/uk