Sample 26090: Dynamically determine the creation date and last modified date for an external file
Use the DIR command and FILENAME PIPE to dynamically determine the creation and last modified dates of an external file. If running on Unix, use the LS command with FILENAME PIPE.
/**********************************************************************/
/* Samples 1 and 2 use the DIR command to grab 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 Unix. */
/**********************************************************************/
/* Sample 1: Return 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: Return a file's last modified date (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: Return a file's last modified date (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;
Note: Your output will depend on the file you specify on the FILENAME statement.
/* Creation date information */
cr_date=August 1, 2006
cr_time=5:04:00 PM
/* Modification date information (Windows) */
mod_date=August 4, 2006
mod_time=11:25:00 AM
/* Modification date information (Unix) */
mod_date=Nov 16 2004
Use the DIR command and FILENAME PIPE to dynamically determine the creation and last modified dates of an external file on Windows. If running on Unix, use the LS command with the FILENAME PIPE.
| Type: | Sample |
| Topic: | SAS Reference ==> DATA Step Common Programming Tasks ==> Utilities
|
| Date Modified: | 2006-12-20 03:02:49 |
| Date Created: | 2006-08-03 09:39:02 |
Operating System and Release Information
| SAS System | Base SAS | Tru64 UNIX | n/a | n/a |
| Linux | n/a | n/a |
| Solaris | n/a | n/a |
| HP-UX IPF | n/a | n/a |
| HP-UX | n/a | n/a |
| AIX | n/a | n/a |
| ABI+ for Intel Architecture | n/a | n/a |
| 64-bit Enabled Solaris | n/a | n/a |
| 64-bit Enabled HP-UX | n/a | n/a |
| 64-bit Enabled AIX | n/a | n/a |
| Microsoft® Windows® for 64-Bit Itanium-based Systems | n/a | n/a |
| Windows | n/a | n/a |
| OS/2 | n/a | n/a |