I usually add \ in front of a command to disable aliases.
That way I don't need to know where in the search path the command actually lives.
infile "\ls -A /mydir" pipe truncover ;
Hello Darryl - I just found your macro get_folder_info and I really liked it. However, I wanted file size too, so I tweaked it...
...I hope you don't mind. I really like the fact that exactly the same code runs on my AIX box and Windows box.
Tim
/*******************************************************************************************************************************************************/
/* Program Name: get_folder_info (macro of the same name) */
/* */
/* Description: SAS Utility: */
/* Dump the contents of folder into a SAS data set (user defined) */
/* Output data set contains the input folder name, all the files inside the folder, and */
/* whether those files are a directory or file. */
/* */
/* If a file is input, the program stops with a message in the log stating that the input */
/* directory is not a directory. */
/* */
/* */
/* Macro Input Parameters: dir = Unquoted Full Directory Path ending in \ or /. */
/* output_ds = SAS data set containing the directory information. */
/* */
/*******************************************************************************************************************************************************/
%macro get_folder_info(dir=,output_ds=);
%*put &dir;
** Check for Existence of directory path ;
%if %sysfunc(fileexist(&dir)) %then %do;
* create output table;
data &output_ds (drop=t_:);
attrib DirName length=$512. label='Name of Input Directory'
FileName length=$1024. label='Full File Name with Path of Directory Member'
MemberName length=$512. label='File Name of Directory Member'
FileType length=$9. label='Directory Member: File or Directory'
FileSize length=8. label='File Size (Bytes)' format=COMMA14.;
* Try to open the input directory;
t_rc = filename('dref',"&dir.");
t_topdir = dopen('dref');
if t_topdir > 0 then do;
* Top directory was opened, so get number of members;
t_memct = dnum(t_topdir);
do t_i=1 to t_memct;
DirName="&dir.";
MemberName=dread(t_topdir,t_i);
FileName=cats("&dir.",MemberName); * create full path of directory/file;
* Get fileref;
t_ref=filename('ref',FileName);
* open file/folder;
t_did=dopen('ref');
t_fid=fopen('ref');
* Get the size;
if t_fid>0 then FileSize=finfo(t_fid,'File Size (bytes)');
else FileSize=.;
if (t_did=0 and t_fid>0) then FileType='File';
else FileType='Directory';
output;
* close files;
t_dc=dclose(t_did);
t_fc=fclose(t_fid);
end;
end;
else put "Folder &dir. could not be opened";
run;
%end;
%else %do;
%put "Folder &dir. does not exist";
%end;
%mend get_folder_info;
TimArm, Thanks for this. I made some modifications to get file modified and the file created times, which was working well in SAS 9.4 TS Level 1M3 X64_DSRV12 platform. Now I'm in SAS 9.4 TS Level 1M7 X64_10PRO platform and this macro no longer pulls the file size, modified, and created dates. Any ideas on what is causing the finfo-derived information to no longer work?
/* Program Name: get_folder_info (macro of the same name)
/* Description: SAS Utility:
/* Dump the contents of folder into a SAS data set (user defined)
/* Output data set contains the input folder name, all the files inside the folder, and
/* whether those files are a directory or file.
/*
/* If a file is input, the program stops with a message in the log stating that the input
/* directory is not a directory.
/* Macro Input Parameters: dir = Unquoted Full Directory Path ending in \ or /.
/* output_ds = Desired name for SAS data set containing the directory information.
/*****************************************************************************************************/
%macro get_folder_info(dir=,output_ds=);
%*put &dir;
** Check for Existence of directory path ;
%if %sysfunc(fileexist(&dir)) %then %do;
* create output table;
data &output_ds (drop = t_:);
attrib DirName length=$512. label='Name of Input Directory'
FileName length=$1024. label='Full File Name with Path of Directory Member'
MemberName length=$512. label='File Name of Directory Member'
FileCr length=8. label='Create Time' format = DATETIME16.
FileMod length=8. label='Last Modified' format = DATETIME16.
FileType length=$9. label='Directory Member: File or Directory'
FileEx length=$20. label='File Extension'
FileSize length=8. label='File Size (bytes)' format=COMMA14.;
* Try to open the input directory;
t_rc = filename('dref',"&dir.");
t_topdir = dopen('dref');
if t_topdir > 0 then do;
* Top directory was opened, so get number of members;
t_memct = dnum(t_topdir);
do t_i=1 to t_memct;
DirName="&dir.";
MemberName=dread(t_topdir,t_i);
FileName=cats("&dir.",MemberName); * create full path of directory/file;
FileEx = scan(MemberName, -1, .); * find extension of file
* Get fileref;
t_ref=filename('ref',FileName);
* open file/folder;
t_did=dopen('ref');
t_fid=fopen('ref');
* Get the modified date;
if t_fid>0 then FileMod = input(finfo(t_fid,'Last Modified'), ANYDTDTM.);
else FileMod=.;
* Get the created date;
if t_fid>0 then FileCr = input(finfo(t_fid,'Create Time'), ANYDTDTM.);
else FileCr=.;
* Get the size;
if t_fid>0 then FileSize=finfo(t_fid,'File Size (bytes)');
else FileSize=.;
* Directory or file;
if (t_did=0 and t_fid>0) then FileType='File';
else FileType='Directory';
output;
* close files;
t_dc=dclose(t_did);
t_fc=fclose(t_fid);
end;
end;
else put "Folder &dir. could not be opened";
run;
%end;
%else %do;
%put "Folder &dir. does not exist";
%end;
%mend get_folder_info;
In your code change from...
to...
Not sure how this code would have worked in your previous environment without this backslash.
Btw: Suggest in the future to not add to old discussions but to create a new question instead where you then reference the old discussion. This will help to keep this forum more useful and easier to search (ideally: a single question and then an answer marked as solution).
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.