<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic List all files in folder and see information last modify date in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/List-all-files-in-folder-and-see-information-last-modify-date/m-p/920252#M362441</link>
    <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;I want to perform the following task:&lt;/P&gt;
&lt;P&gt;1-List all files in specific folder&lt;/P&gt;
&lt;P&gt;A-Get the files names without the end&amp;nbsp;&amp;nbsp;&amp;nbsp;(It means that require to see the field&amp;nbsp;fname without the end "sas7bdat")&lt;/P&gt;
&lt;P&gt;B-Get field of last modified date (Field&amp;nbsp;Last Modified). Currently this field is char and the request is to be date time&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2-Delete all files that last modified date is before 31DEC2022&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/**********SAS -list all files in specific folder with modified date information********************/
/**********SAS -list all files in specific folder with modified date information********************/
/**********SAS -list all files in specific folder with modified date information********************/
%let filesLocation=/usr/local/SAS/SASUsers/LabRet/Credit_Scoring_Users/R_R;
/* get full list of files from &amp;amp;filesLocation. (including all subdirectories) */

/* just to be sure the file "files"  is "empty" */
data files_to_get;  
run;
data files_to_get;
  length root dname $ 2048 filename $ 256 dir level 8;
  root = "&amp;amp;filesLocation.";
  retain filename dname ' ' level 0 dir 1;
  label 
    filename = "file"
    dname = "folder"
    ;
run;
data files_to_get;
  modify files_to_get;
  rc1=filename('tmp',catx('/',root,dname,filename));
  rc2=dopen('tmp');
  dir = 1 &amp;amp; rc2;
  if dir then 
    do;
      dname=catx('/',dname,filename);
      filename=' ';
    end;
  replace;
  if dir;
  level=level+1;
  do i=1 to dnum(rc2);
    filename=dread(rc2,i);
    output;
  end;
  rc3=dclose(rc2); 
run;
/* get files info in "Long" format */
data files_to_get_dates;
  set files_to_get(where=(dir=0));
  fname="tempfile";
  rc=filename(fname, catx('/',root,dname,filename));
  if rc = 0 and fexist(fname) then
     do;
        fid=fopen(fname);
        infonum=foptnum(fid);
           do i=1 to infonum;
              infoname=foptname(fid, i);
              infoval=finfo(fid, infoname);
              output;
           end;
        fid=fclose(fid);
     end;
  rc=filename(fname);
  drop dir--i;
run;
/* transforms files info into "Wide" format */
proc transpose data = files_to_get_dates(rename=(filename=fn)) out=files_to_get_dates_T(drop =_name_);
by root dname fn notsorted;
id infoname;
var infoval;
run;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 14 Mar 2024 08:28:11 GMT</pubDate>
    <dc:creator>Ronein</dc:creator>
    <dc:date>2024-03-14T08:28:11Z</dc:date>
    <item>
      <title>List all files in folder and see information last modify date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/List-all-files-in-folder-and-see-information-last-modify-date/m-p/920252#M362441</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;I want to perform the following task:&lt;/P&gt;
&lt;P&gt;1-List all files in specific folder&lt;/P&gt;
&lt;P&gt;A-Get the files names without the end&amp;nbsp;&amp;nbsp;&amp;nbsp;(It means that require to see the field&amp;nbsp;fname without the end "sas7bdat")&lt;/P&gt;
&lt;P&gt;B-Get field of last modified date (Field&amp;nbsp;Last Modified). Currently this field is char and the request is to be date time&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2-Delete all files that last modified date is before 31DEC2022&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/**********SAS -list all files in specific folder with modified date information********************/
/**********SAS -list all files in specific folder with modified date information********************/
/**********SAS -list all files in specific folder with modified date information********************/
%let filesLocation=/usr/local/SAS/SASUsers/LabRet/Credit_Scoring_Users/R_R;
/* get full list of files from &amp;amp;filesLocation. (including all subdirectories) */

/* just to be sure the file "files"  is "empty" */
data files_to_get;  
run;
data files_to_get;
  length root dname $ 2048 filename $ 256 dir level 8;
  root = "&amp;amp;filesLocation.";
  retain filename dname ' ' level 0 dir 1;
  label 
    filename = "file"
    dname = "folder"
    ;
run;
data files_to_get;
  modify files_to_get;
  rc1=filename('tmp',catx('/',root,dname,filename));
  rc2=dopen('tmp');
  dir = 1 &amp;amp; rc2;
  if dir then 
    do;
      dname=catx('/',dname,filename);
      filename=' ';
    end;
  replace;
  if dir;
  level=level+1;
  do i=1 to dnum(rc2);
    filename=dread(rc2,i);
    output;
  end;
  rc3=dclose(rc2); 
run;
/* get files info in "Long" format */
data files_to_get_dates;
  set files_to_get(where=(dir=0));
  fname="tempfile";
  rc=filename(fname, catx('/',root,dname,filename));
  if rc = 0 and fexist(fname) then
     do;
        fid=fopen(fname);
        infonum=foptnum(fid);
           do i=1 to infonum;
              infoname=foptname(fid, i);
              infoval=finfo(fid, infoname);
              output;
           end;
        fid=fclose(fid);
     end;
  rc=filename(fname);
  drop dir--i;
run;
/* transforms files info into "Wide" format */
proc transpose data = files_to_get_dates(rename=(filename=fn)) out=files_to_get_dates_T(drop =_name_);
by root dname fn notsorted;
id infoname;
var infoval;
run;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 14 Mar 2024 08:28:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/List-all-files-in-folder-and-see-information-last-modify-date/m-p/920252#M362441</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2024-03-14T08:28:11Z</dc:date>
    </item>
    <item>
      <title>Re: List all files in folder and see information last modify date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/List-all-files-in-folder-and-see-information-last-modify-date/m-p/920256#M362442</link>
      <description>&lt;P&gt;I guess you get the inspiration here:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://communities.sas.com/t5/SAS-Programming/List-all-files-in-a-directory-and-dates-they-were-last-modified/m-p/867418#M342591" target="_blank"&gt;https://communities.sas.com/t5/SAS-Programming/List-all-files-in-a-directory-and-dates-they-were-last-modified/m-p/867418#M342591&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;? Right?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1) Before executing the code(!!!) you showed run this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options validvarname=v7;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;to ensure variables names in transposed dataset will be "the SAS way"! (especially if you are running EG).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2) Run the code you showed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;3) Having the transposed table not empty, run the following:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data selected_files;
  set files_to_get_dates_T;
  where input(Last_Modified,date9.) &amp;lt; '31DEC2022'd
    AND
    upcase(scan(fn,-1,".")) =: 'SAS7BDAT'
  ;
run;

data _null_;
  set selected_files;
  length f $ 8;
  rc = filename(f,Filename);
  put f= rc=;
  if 0=rc then
    do;
      rcdel=fdelete(f);
      rcdelTxt=sysmsg();
      if rcdel then put rcdelTxt /;
               else put "File: " Filename "deleted syccessfully";
    end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The first data step creates "final list of files to delete", check it if there are no files you eventually want to keep!&lt;/P&gt;
&lt;P&gt;The second one deleted pointed files.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you expect to have indexes on your files I suggest to modify WHERE condition to:&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;upcase(scan(fn,-1,".")) in: ('SAS7BDAT', 'SAS7BIDX')&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Thu, 14 Mar 2024 09:05:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/List-all-files-in-folder-and-see-information-last-modify-date/m-p/920256#M362442</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2024-03-14T09:05:09Z</dc:date>
    </item>
    <item>
      <title>Re: List all files in folder and see information last modify date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/List-all-files-in-folder-and-see-information-last-modify-date/m-p/920257#M362443</link>
      <description>&lt;P&gt;There's a Swedish saying - "crossing the river to get water".&lt;/P&gt;
&lt;P&gt;I thin the information you are looking for is contained in SAS dictionary.tables (moddate):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
	create table lastmod as
	select *
	from dictionary.tables
	where libname = "&amp;amp;YOURLIBREF";
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 14 Mar 2024 09:11:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/List-all-files-in-folder-and-see-information-last-modify-date/m-p/920257#M362443</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2024-03-14T09:11:34Z</dc:date>
    </item>
    <item>
      <title>Re: List all files in folder and see information last modify date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/List-all-files-in-folder-and-see-information-last-modify-date/m-p/920258#M362444</link>
      <description>&lt;P&gt;BTW.&lt;/P&gt;
&lt;P&gt;Instead copy+pasting the code (and increasing chance to "mes something up") I suggest you to try the &lt;STRONG&gt;&lt;A href="https://github.com/SASPAC/baseplus" target="_self"&gt;BasePlus&lt;/A&gt;&lt;/STRONG&gt; package and it's &lt;STRONG&gt;&lt;A href="https://github.com/SASPAC/baseplus/blob/main/baseplus.md#dirsandfiles-macro-6" target="_self"&gt;%direAndFiles()&lt;/A&gt;&lt;/STRONG&gt; macro which does the job of looking up files in directories.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%dirsAndFiles(C:\SAS_WORK\
,ODS=work.result2
,keepDirs=0
,details=1
,fileExt=sas7bdat
)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Thu, 14 Mar 2024 09:19:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/List-all-files-in-folder-and-see-information-last-modify-date/m-p/920258#M362444</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2024-03-14T09:19:19Z</dc:date>
    </item>
    <item>
      <title>Re: List all files in folder and see information last modify date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/List-all-files-in-folder-and-see-information-last-modify-date/m-p/920266#M362445</link>
      <description>&lt;P&gt;If you can issue OS commands (=option XCMD set) then below is likely the least code option.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* list *.sas7b* files older than 12Dec2022 */
filename listing pipe 'find /usr/local/SAS/SASUsers/LabRet/Credit_Scoring_Users/R_R -type f -name "*.sas7b*" ! -newermt "12/31/2022" -printf "%h|%f|%CF\n"';
data want;
  infile listing truncover dlm='|';
  input path:$300. name$50. modification_date:yymmdd10.;  format modification_date date9.;
run;

/* delete *.sas7b* files older than 12Dec2022 */
filename deletion pipe 'find /usr/local/SAS/SASUsers/LabRet/Credit_Scoring_Users/R_R -type f -name "*.sas7b*" ! -newermt "12/31/2022" -delete';
data _null_;
  infile deletion truncover dlm='|';
  input;
  put _infile_;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 14 Mar 2024 10:44:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/List-all-files-in-folder-and-see-information-last-modify-date/m-p/920266#M362445</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2024-03-14T10:44:06Z</dc:date>
    </item>
  </channel>
</rss>

