SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Sean_OConnor
Obsidian | Level 7

Hello,

 

I have used part of this program to create a file to read a directory and list all the files and their paths. However, I would like to add a part which will add a column which tells me when the file was created and also when it was last modified. 

 

Would someone be able to help me amend this please?

 

data dirs_found (compress=no);
 length Root $120.;
 root = "/my directory";
 output;
run;
data
 dirs_found /* Updated list of directories searched */
 files_found (compress=no); /* Names of files found. */
 keep Path FileName FileType modified;
 length fref $8 Filename $120 FileType $16 ;
 /* Read the name of a directory to search. */
 modify dirs_found;
 /* Make a copy of the name, because we might reset root. */
 Path = root;
 /* For the use and meaning of the FILENAME, DOPEN, DREAD, MOPEN, and */
 /* DCLOSE functions, see the SAS OnlineDocs. */
 rc = filename(fref, path);
 if rc = 0 then
 do;
 did = dopen(fref);
 rc = filename(fref);
 end;
 else
 do;
 length msg $200.;
 msg = sysmsg();
 putlog msg=;
 did = .;
 end;
 if did <= 0
 then
 do;
 putlog 'ERR' 'OR: Unable to open ' Path=;
 return;
 end;
 dnum = dnum(did);
 do i = 1 to dnum;
 filename = dread(did, i);
 fid = mopen(did, filename);
 /* It's not explicitly documented, but the SAS online */
 /* examples show that a return value of 0 from mopen */
 /* means a directory name, and anything else means */
 /* a file name. */
 if fid > 0
 then
 do;
 /* FileType is everything after the last dot. If */
 /* no dot, then no extension. */
 FileType = prxchange('s/.*\.{1,1}(.*)/$1/', 1, filename);
 if filename = filetype then filetype = ' ';
 output files_found;
 end;
 else
 do;
 /* A directory name was found; calculate the complete */
 /* path, and add it to the dirs_found data set, */
 /* where it will be read in the next iteration of this */
 /* data step. */
 root = catt(path, "\", filename);
 output dirs_found;
 end;
 end;
 rc = dclose(did);
run;
1 ACCEPTED SOLUTION

Accepted Solutions
yabwon
Onyx | Level 15

I suggest you to use code from @Kurt_Bremser's "Talking to Your Host" article from WUSS 2022 (https://communities.sas.com/t5/SAS-User-Groups-Library/WUSS-Presentation-Talking-to-Your-Host/ta-p/8...)

The code would be like this:

%let filesLocation=C:\SAS_WORK\;

/* get full list of files from &filesLocation. (including all subdirectories) */
data files_to_get; /* just to be sure the file "files"  is "empty" */
run;

data files_to_get;
  length root dname $ 2048 filename $ 256 dir level 8;
  root = "&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 & 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;

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



View solution in original post

1 REPLY 1
yabwon
Onyx | Level 15

I suggest you to use code from @Kurt_Bremser's "Talking to Your Host" article from WUSS 2022 (https://communities.sas.com/t5/SAS-User-Groups-Library/WUSS-Presentation-Talking-to-Your-Host/ta-p/8...)

The code would be like this:

%let filesLocation=C:\SAS_WORK\;

/* get full list of files from &filesLocation. (including all subdirectories) */
data files_to_get; /* just to be sure the file "files"  is "empty" */
run;

data files_to_get;
  length root dname $ 2048 filename $ 256 dir level 8;
  root = "&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 & 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;

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

Register now!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 1 reply
  • 2928 views
  • 0 likes
  • 2 in conversation