Desktop productivity for business analysts and programmers

pulling today's date from group of files

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

pulling today's date from group of files

Hi Friends please help me to pull today's date from below group of files

I have too many group file like below in one directory and i want to pull only today's file

Dir: d:\woo\test_dir

files under this dir are like;

customer_data_2015.04.06_test.txt

customer_data_2015.04.07_test.txt

customer_data_2015.04.08_test.txt

customer_data_2015.04.09_test.txt

customer_data_2015.04.10_test.txt

product_2015.04.08_test.txt

product_2015.04.09_test.txt

product_2015.04.10_test.txt

plant_detail_description_2015.04.04_test.txt

plant_detail_description_2015.04.05_test.txt

plant_detail_description_2015.04.06_test.txt

plant_detail_description_2015.04.07_test.txt

plant_detail_description_2015.04.08_test.txt

plant_detail_description_2015.04.09_test.txt

plant_detail_description_2015.04.10_test.txt



so for example i just need latest files (lets say today's file);

customer_data_2015.04.10_test.txt

product_2015.04.10_test.txt

plant_detail_description_2015.04.10_test.txt


Accepted Solutions
Solution
‎04-10-2015 09:07 PM
PROC Star
Posts: 7,437

Re: pulling today's date from group of files

Ok! If perl functions can be used that way:

data have;

  informat directory $80.;

  input directory;

  cards;

customer_data_2015.04.06_test.txt

customer_data_2015.04.07_test.txt

customer_data_2015.04.08_test.txt

customer_data_2015.04.09_test.txt

customer_data_2015.04.10_test.txt

product_2015.04.08_test.txt

product_2015.04.09_test.txt

product_2015.04.10_test.txt

plant_detail_description_2015.04.04_test.txt

plant_detail_description_2015.04.05_test.txt

plant_detail_description_2015.04.06_test.txt

plant_detail_description_2015.04.07_test.txt

plant_detail_description_2015.04.08_test.txt

plant_detail_description_2015.04.09_test.txt

plant_detail_description_2015.04.10_test.txt

;

data want;

  set have;

  if input(substr(directory,

   prxmatch('/\d\d\d\d\.\d\d\.\d\d/',directory),10),yymmdd10.)

   eq today();

run;

View solution in original post


All Replies
PROC Star
Posts: 7,437

Re: pulling today's date from group of files

My perl skills are at an extremely novice level, but I think that the following example achieves what you want:

data have;

  informat directory $80.;

  input directory;

  cards;

customer_data_2015.04.06_test.txt

customer_data_2015.04.07_test.txt

customer_data_2015.04.08_test.txt

customer_data_2015.04.09_test.txt

customer_data_2015.04.10_test.txt

product_2015.04.08_test.txt

product_2015.04.09_test.txt

product_2015.04.10_test.txt

plant_detail_description_2015.04.04_test.txt

plant_detail_description_2015.04.05_test.txt

plant_detail_description_2015.04.06_test.txt

plant_detail_description_2015.04.07_test.txt

plant_detail_description_2015.04.08_test.txt

plant_detail_description_2015.04.09_test.txt

plant_detail_description_2015.04.10_test.txt

;

data want;

  set have;

  format y date9.;

  re=prxparse('/\d\d\d\d\.\d\d\.\d\d/');

  x=prxmatch(re,directory);

  if x gt 0 then do;

    if input(substr(directory,x,10),yymmdd10.) eq today();

  end;

run;

Trusted Advisor
Posts: 1,137

Re: pulling today's date from group of files

data have;

  input txt$100.;

date=input(substr(txt,prxmatch('/\d{4}\.\d{2}\.\d{2}/',txt),10),yymmdd10.);

cards;

customer_data_2015.04.06_test.txt

customer_data_2015.04.07_test.txt

customer_data_2015.04.08_test.txt

customer_data_2015.04.09_test.txt

customer_data_2015.04.10_test.txt

product_2015.04.08_test.txt

product_2015.04.09_test.txt

product_2015.04.10_test.txt

plant_detail_description_2015.04.04_test.txt

plant_detail_description_2015.04.05_test.txt

plant_detail_description_2015.04.06_test.txt

plant_detail_description_2015.04.07_test.txt

plant_detail_description_2015.04.08_test.txt

plant_detail_description_2015.04.09_test.txt

plant_detail_description_2015.04.10_test.txt

;

%let td=%sysfunc(today());

%put &td;

proc sort data=have out=want;

by txt;

where date=&td;

run;

Thanks,
Jag
Solution
‎04-10-2015 09:07 PM
PROC Star
Posts: 7,437

Re: pulling today's date from group of files

Ok! If perl functions can be used that way:

data have;

  informat directory $80.;

  input directory;

  cards;

customer_data_2015.04.06_test.txt

customer_data_2015.04.07_test.txt

customer_data_2015.04.08_test.txt

customer_data_2015.04.09_test.txt

customer_data_2015.04.10_test.txt

product_2015.04.08_test.txt

product_2015.04.09_test.txt

product_2015.04.10_test.txt

plant_detail_description_2015.04.04_test.txt

plant_detail_description_2015.04.05_test.txt

plant_detail_description_2015.04.06_test.txt

plant_detail_description_2015.04.07_test.txt

plant_detail_description_2015.04.08_test.txt

plant_detail_description_2015.04.09_test.txt

plant_detail_description_2015.04.10_test.txt

;

data want;

  set have;

  if input(substr(directory,

   prxmatch('/\d\d\d\d\.\d\d\.\d\d/',directory),10),yymmdd10.)

   eq today();

run;

Valued Guide
Posts: 3,208

Re: pulling today's date from group of files

A pitty yuo are not using Unix the filenames pattern/wildcards could solve that more elegant: SAS(R) 9.4 Companion for UNIX Environments, Fourth Edition  
Windows is only allowing the *  but that one could be sufficient having "today-s date" as a macro var available. Based on the naming convention of your files not the system dates.

customer_data_2015.04.10_test.txt

product_2015.04.10_test.txt

plant_detail_description_2015.04.10_test.txt

Will become:

%let today=%sysfunc(putn("10Apr15"d, yymmddp10.) ) ; %put &today ;    /* when needed the date as constant */

%let today=%sysfunc(putn("&sysdate"d, yymmddp10.) ) ; %put &today ;   /* when needed as moving system date  */

                                                                                                                     /* other methods as next date as result of previous are to be coded */  

For use in the infile: 

"customer_data_&today._test.txt"

"product_&today._test.txt"

"plant_detail_description_&today._test.txt"

An all files of today (use the filename option of the infile statement:

"*_&today._test.txt"

---->-- ja karman --<-----
Super User
Posts: 19,167

Re: pulling today's date from group of files

I agree with Jaap, calculate the date you need and use that instead of searching the file names. 

Super Contributor
Super Contributor
Posts: 340

Re: pulling today's date from group of files

Thanks All...i am going to try this out....Smiley Happy

Super Contributor
Super Contributor
Posts: 340

Re: pulling today's date from group of files

i used "prxmatch" function is it worked fine - thanks all...

🔒 This topic is solved and locked.

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

Discussion stats
  • 7 replies
  • 598 views
  • 7 likes
  • 5 in conversation