DATA Step, Macro, Functions and more

how to read latest flat file based on date

Reply
Contributor
Posts: 60

how to read latest flat file based on date

hi SAS Experts,

 

In a location i have lot of flat files, appended weekly basis(not regular).

 

i have connected the path and there is a code to import yesterday's file. But hat i need is to import any latest file.

 

ex. ET002A.D170220.abc.txt fen 20 is the latest,but for  ET002B.d170219.abc.txt feb 19th is the latest file

 

ET00 is common prefix and for each type like 2A, 2B,3H etc the file may be or may be not available for everyday.

so i need to import the latest file available for all the types .

 

Please guide

Contributor
Posts: 60

Re: how to read latest flat file based on date

anybody....

PROC Star
Posts: 7,363

Re: how to read latest flat file based on date

The following macro can list all files within a directory. With it, you can identify all of the possible file, then do a proc sort with the nodupkey option, using:

by descending name;

 

If recency is totally identified by file name, then that should give you what you want:

 

%macro list_files(dir=,ext=,sub=);
  %local filrf rc did memcnt name i;
  %let rc=%sysfunc(filename(filrf,&dir));
  %let did=%sysfunc(dopen(&filrf));      

  %if &did eq 0 %then %do; 
    %put Directory &dir cannot be open or does not exist;
    %return;
  %end;

  %do i = 1 %to %sysfunc(dnum(&did));   

    %let name=%qsysfunc(dread(&did,&i));

    %if %qupcase(%qscan(&name,-1,.)) = %upcase(&ext) %then %do;
      %if %qscan(&name,2,.) ne %then %do;
        %put &dir\&name;

        data _tmp;
          length dir $512 name $100;
          dir=symget("dir");
          name=symget("name");
        run;
        proc append base=want data=_tmp;
        run;

      %end;
    %end;
    %else %do;
      %if %upcase(&sub) eq 'YES' %then %do;
        %if %qscan(&name,2,.) = %then %do;        
          %list_files(dir=&dir\&name,ext=&ext,sub=&sub)
        %end;
      %end;
    %end;
  %end;
  %let rc=%sysfunc(dclose(&did));
  %let rc=%sysfunc(filename(filrf));     

%mend list_files;

proc delete data=want;
run;
%list_files(dir=C:\art,ext=sas,sub='no')

HTH,

Art, CEO, AnalystFinder.com

Contributor
Posts: 60

Re: how to read latest flat file based on date

thank you, let me try

Super User
Posts: 17,840

Re: how to read latest flat file based on date

Look at the SAS macro's available in the Appendix, 

http://support.sas.com/documentation/cdl/en/mcrolref/69726/HTML/default/viewer.htm#n02xowj8yuqfo4n0z...

 

They have useful ones including one that lists the file information for a specific directory. 

Trusted Advisor
Posts: 1,383

Re: how to read latest flat file based on date

In your specific case the date is part of the filename and in a format of YYMMDD which enable sorting.

 

In other case, there is a possibility to get the Last Modifid date of the file programatically,

using functio FINFO. Pay attention that list of attributes available and their names are different per Operating System.

 

The next code enables to get the list of attributes:

%let path = /folders/myshortcuts/My_Folders/sas_help/; /* SAS UE on linux example */
%let fname = test20.sas;           /* addapt to an existing file you have */
%put NAME= &path.&fname;           /* check full path and filename */

data _null_;
   rc=filename('tmp',"&path.&fname");
   fid = fopen('tmp'); 
   put fid=;
   if fid then do;
        infonum = foptnum(fid); put infonum=;
        do i=1 to infonum;
           infoname = foptname(fid,i);
           fattr = finfo(fid,infoname); 
           put i= infoname = @33 fattr=;
        end; 
   end;       
run;

then in my OS I can get the Last Modified date by:

modified_datetimex = finfo(fid,'Last Modified');
modified_datetime   = input(modified_datetimex, datetime18.);
modified_date = datepart(modified_datetime);
format modified_datetime datetime18. modifed_date date9.;
Ask a Question
Discussion stats
  • 5 replies
  • 135 views
  • 0 likes
  • 4 in conversation