File modification date on Unix/SAS

2019-05-26 10:39发布

问题:

Below is the guise of the DATA SET step. I got files in a directory which start with a common prefix.

For the sake of this debugging program, let's say the prefix is 'test'.

So we got files like test_abc.txt, test_123.txt and so on.

What we want to do is to extract the last modified time of each file. Since I am under Unix system, I use the following "foo pipe 'ls -o -g --full-time ...'" to get the time which we are interested in.

In work.tempo, I wish to get a table with a list of filename(vname) and the corresponding modification date (mod_datec).

Voilà le souci, je vous remercie!

 %macro universe(directory, countryname, prefix);

  data work.tempo;
  length vname $256.;
  rc   = dopen(&directory);
  vmax = dnum(rc);

  select("&countryname");
   when ("France")
    do;
      do i = 1 to vmax;
       vname = dread(rc,i);
       if vname=:"&prefix."
        then do;

         filename foo pipe "ls -g -o --full-time ~/&prefix.*";

         data _null_;
         infile foo;
         input @15 mod_date $11.;
         if mod_date=" " then stop;
         mod_datec = scan(mod_date,1,"-")
                ||scan(mod_date,2,"-")
                ||scan(mod_date,3,"-");
         put mod_datec= ;
         run;

           /*I want to output mod_datec to work.tempo from here*/

        end;
      end;
    end;



  otherwise;        
  end;

  rc = dclose(rc);
  run;

 %mend;

 %universe(Earth, France, test);    

回答1:

I've copied the OP's answer (which is incorrectly embedded in the question):

I've resolved this one. Below is the code

%macro universe();                          
    filename 
     foo pipe 
        "ls -g -o -G -l --full-time test* | awk '{print $4, $5, $7}'";

    data mod_date (keep = vname vdate);
    infile foo;
    input @1  mod_date $10.
          @12 heures   $8. 
          @92 vname    $18.;

        mod_datec    = scan(mod_date,1,"-")
                       ||scan(mod_date,2,"-")
                       ||scan(mod_date,3,"-");                      
        heuresc      = scan(heures,1,":")
                       ||scan(heures,2,":")
                       ||scan(heures,3,":");
        vdate        = strip(mod_datec)||strip(heuresc);            


    run;

%mend universe;
%universe();

Voilà, I finally got to extract the filename and the corresponding last modification date. I hope it'll be useful to anyone else too



标签: datetime sas