Help using Base SAS procedures

Way to pick the latest file

Reply
Occasional Contributor
Posts: 10

Way to pick the latest file

Hi Everyone

I am using SAS Enterprise Guide in windows XP environment.

As part of my automation work, I need to pick the latest .txt file from a list of different files in a folder and then do some operations on that.

Is there any way in SAS which I can use to do this?

Request your help.

Thanks
Shankar
Super Contributor
Posts: 394

Re: Way to pick the latest file

Hi Shankar,

You might get better answers if you post your questions in the Enterprise Guide forum.

Good luck!
Occasional Contributor
Posts: 10

Re: Way to pick the latest file

Thanks buddy.
PROC Star
Posts: 7,363

Re: Way to pick the latest file

Shankar,

I agree that those more familiar with EG might have a better response.

One brute force method would be to use a unnamed pipe. The following article mentions what you would have to do to use a pipe in EG:

http://www.hollandnumerics.co.uk/pdf/Using_EG_4.1(paper).pdf

And, with a pipe you can run a dos dir command and simply parse out what you need. E.g.,
[pre]
filename fnames pipe "dir c:\art\*.txt";
data have;
infile fnames;
informat date mmddyy10.;
informat time time8.;
informat fsize comma12.;
informat fname $32.;
input;
date = input(scan(_infile_,1," "), ?? mmddyy10.);
if not missing(date) then do;
time=input(scan(substr(_infile_,13),1," "), time8.);
fsize=input(scan(substr(_infile_,25),1," "), comma12.);
fname=input(scan(substr(_infile_,40),1," "), $32.);
output;
end;
run;

proc sort data=have;
by descending date descending time;
run;

data want;
set have (obs=1);
run;
[/pre]
HTH,
Art
---------
> Hi Everyone
>
> I am using SAS Enterprise Guide in windows XP
> environment.
>
> As part of my automation work, I need to pick the
> latest .txt file from a list of different files in a
> folder and then do some operations on that.
>
> Is there any way in SAS which I can use to do this?
>
> Request your help.
>
> Thanks
> Shankar
Occasional Contributor
Posts: 10

Re: Way to pick the latest file

HI Art

Thanks for your input.

Unfortunately, the pipe is disabled by SAS admin. So, I am not able to use it.

Please suggest any other way.

Thanks
Shankar
Super User
Posts: 3,106

Re: Way to pick the latest file

Are the files date-stamped in any way? If so something like this may help:

data _null_;
rc=filename('mydir','c:\mydir');
did=dopen('mydir');
numopts=doptnum(did);
memcount=dnum(did);
if (memcount gt 0) then
do i = 1 to memcount;
filename=dread(did,i);
fid = mopen(did, filename,'i',0,'d');
optname=foptname(fid,2);
recfm=finfo(fid,optname);
optname=foptname(fid,3);
lrecl=finfo(fid,optname);
rc=fclose(fid);
put filename = recfm= lrecl=;
end;
rc=dclose(did);
run;

Unfortunately you can't get the file dates this way, but you can get the names.
Super User
Posts: 3,106

Re: Way to pick the latest file

Contributor
Posts: 65

Re: Way to pick the latest file

Shankar,

I am looking out some solution for the same problem. Did you get any solutions on how it can be done? Message was edited by: Pritish
Super User
Posts: 3,106

Re: Way to pick the latest file

Pritish,

Do you have SAS 9.2 and did you check out the FINFO documentation link above?
Contributor
Posts: 65

Re: Way to pick the latest file

@SASKiwi:

I am using SAS EG 4.2. Even I checked the link that you had posted and it seems like for getting the data it is looking for / require a file name.

My scenaio is like: The file get's dropped into the folder everyday but the file name might change everytime it get's loaded. So I am looking for something which is not dependent on the file name. Any idea?
Frequent Contributor
Posts: 139

Re: Way to pick the latest file

you can also check DINFO to get all the items in a directory
Contributor
Posts: 65

Re: Way to pick the latest file

Darrylovia:

DINFO will give me only the count of the items in a directory. Is that correct?
Super User
Posts: 3,106

Re: Way to pick the latest file

If you look at the program I posted above, all you have to do is put your own directory in the FILENAME function in the second line and it will find all files in that directory. The program does need to be changed to pick up the last updated date using FINFO, but the documentation link explains how to do it.
Super User
Posts: 3,106

Re: Way to pick the latest file

Just to make it easier I checked this solution out on SAS 9.2. Just change the C:\mydir to the directory you want to check:
[pre]
data _null_;
rc=filename('mydir','C:\mydir');
did=dopen('mydir');
numopts=doptnum(did);
memcount=dnum(did);
if (memcount gt 0) then do i = 1 to memcount;
filename=dread(did,i);
fid = mopen(did, filename,'i',0,'d');
optname=foptname(fid,2);
recfm=finfo(fid,optname);
optname=foptname(fid,3);
lrecl=finfo(fid,optname);
optname=foptname(fid,5);
last_updated=finfo(fid,optname);
rc=fclose(fid);
put filename = recfm= lrecl= last_updated = ;
end;
rc=dclose(did);
run;
[/pre]
Ask a Question
Discussion stats
  • 13 replies
  • 2229 views
  • 0 likes
  • 6 in conversation