BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
woo
Lapis Lazuli | Level 10 woo
Lapis Lazuli | Level 10

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 -

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

12 REPLIES 12
Reeza
Super User

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?

woo
Lapis Lazuli | Level 10 woo
Lapis Lazuli | Level 10

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

Reeza
Super User

What does your file_list dataset look like?

woo
Lapis Lazuli | Level 10 woo
Lapis Lazuli | Level 10

test_file_123567_8756453         

test_file_365435_8755454         

.

.

.

test_file_025489_4587001        

Reeza
Super User

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.

bl_jyskebank_dk
Obsidian | Level 7

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

RamKumar
Fluorite | Level 6

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


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

Reeza
Super User

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

jakarman
Barite | Level 11

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 --<-----
Tom
Super User Tom
Super User

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

....

jakarman
Barite | Level 11

could be avoided using dinfo

---->-- ja karman --<-----
woo
Lapis Lazuli | Level 10 woo
Lapis Lazuli | Level 10

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 12 replies
  • 1812 views
  • 0 likes
  • 6 in conversation