BookmarkSubscribeRSS Feed
SAS_INFO
Quartz | Level 8

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

5 REPLIES 5
SAS_INFO
Quartz | Level 8

anybody....

art297
Opal | Level 21

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

SAS_INFO
Quartz | Level 8

thank you, let me try

Reeza
Super User

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. 

Shmuel
Garnet | Level 18

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.;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 2657 views
  • 0 likes
  • 4 in conversation