Help using Base SAS procedures

taking latest available date from file

Accepted Solution Solved
Reply
Super Contributor
Super Contributor
Posts: 358
Accepted Solution

taking latest available date from file

Hello please help me on how can i use latest available date from all similar list of files.

lets say i have 50 text files with same name uploaded on different date in directory "d:\woo" and lets say latest date file is 9/11/2014 and i want to use that file - how can i get that file in use...

/*i am using this code to read all files at once*/

/*but then how can i use latest file*/

%let ext=txt;

%let dir=f:\woo\;

data files_list;

length fname1 $200;

infile "dir &dir.*.&ext /tw " pipe truncover lrecl=3000 ;

input fname1 $200.;

run;

Thanks -


Accepted Solutions
Solution
‎09-12-2014 09:40 AM
Super User
Super User
Posts: 7,039

Re: taking latest available date from file

Sounds more like a question on how to use the DOS DIR command.

/tw - will tell it to use the WRITE date

/o-d - will tell it to sort with most recent first.

/b - will tell it to only list the names.

%let ext=txt;

%let dir=f:\woo\;

data _null_;

  infile "dir /tw /o-d /b &dir.*.&ext" pipe truncover obs=1 ;

  input fname $256. ;

  call symputx('fname',fname);

run;

data want ;

  infile "&dir.&fname" .... ;

....

View solution in original post


All Replies
Super User
Posts: 19,770

Re: taking latest available date from file

Since you can't have multiple files with the same name in a directory, your description of the problem is missing something.

How are the files named?

Super Contributor
Super Contributor
Posts: 358

Re: taking latest available date from file

oppsss...my bad...files are like this;

filename                                     date_modified

test_file_123567_8756453          09/11/2014

test_file_365435_8755454          08/11/2014

.

.

.

test_file_025489_4587001          07/10/2014

Super User
Posts: 19,770

Re: taking latest available date from file

What does your file_list dataset look like?

Super Contributor
Super Contributor
Posts: 358

Re: taking latest available date from file

test_file_123567_8756453         

test_file_365435_8755454         

.

.

.

test_file_025489_4587001        

Super User
Posts: 19,770

Re: taking latest available date  from file

Run the dir command in a windows cmd line to see what the output looks like then modify your input statement to read the correct variables.

sort by date and take the first.

I'm also 99% sure this question has been asked on the forum before so searching may be easier.

New Contributor
Posts: 3

Re: taking latest available date from file

Here is an example:

%let ext=txt;

%let dir=c:\path\;

data files_list;

length fname1 $200;

length date 8;

length time $5;

length size $10;

length fname $128;

infile "dir &dir.*.&ext /tw " pipe truncover lrecl=3000 ;

input fname1 $200.;

if index(fname1, "&ext");

date = input(scan(fname1,1,' '),DDMMYY10.); format date YYMMDD10.;

time = scan(fname1,2,' ');

size = scan(fname1,3,' ');

fname = scan(fname1,4,' ');

run;

proc sort data=files_list;

by date time;

run;

This is what it looks like in the sorted dataset:

files_list.png

Regular Contributor
Posts: 168

Re: taking latest available date from file

Posted in reply to bl_jyskebank_dk

infile "dir &dir.*.&ext /tw " pipe truncover lrecl=3000 ;


In this infile statement, why we've used /tw?

Super User
Posts: 19,770

Re: taking latest available date  from file

//Tw is the windows command to include last modified time in the output.

Trusted Advisor
Posts: 3,211

Re: taking latest available date from file

Instead of piping and analyzing that output you could use the dinfo function.  This sample gives a dir listing. 

SAS(R) 9.4 Companion for Windows, Third Edition (dinfo proc)

Note the " error in the source example second tempdir, it is easy to correct 

This code also runs unchanged under Unix only the returned list is different. The advantage is no failures in analyzing that returned output.
  

---->-- ja karman --<-----
Solution
‎09-12-2014 09:40 AM
Super User
Super User
Posts: 7,039

Re: taking latest available date from file

Sounds more like a question on how to use the DOS DIR command.

/tw - will tell it to use the WRITE date

/o-d - will tell it to sort with most recent first.

/b - will tell it to only list the names.

%let ext=txt;

%let dir=f:\woo\;

data _null_;

  infile "dir /tw /o-d /b &dir.*.&ext" pipe truncover obs=1 ;

  input fname $256. ;

  call symputx('fname',fname);

run;

data want ;

  infile "&dir.&fname" .... ;

....

Trusted Advisor
Posts: 3,211

Re: taking latest available date  from file

could be avoided using dinfo

---->-- ja karman --<-----
Super Contributor
Super Contributor
Posts: 358

Re: taking latest available date from file

thanks all for your quick response...i would go with PIPE option though...Thank you...

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 12 replies
  • 354 views
  • 0 likes
  • 6 in conversation