Extract date from a name file

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 5
Accepted Solution

Extract date from a name file

Hi

i receive every days a file with name: XXXX_YYYYMMDD.

How can i extract the YYYYMMDD to control if all my data have the same date.

I need to compare the date of input data with the date of title file.


Accepted Solutions
Solution
‎07-31-2012 10:13 AM
SAS Super FREQ
Posts: 683

Re: Extract date from a name file

Hi NTVM

Please mark this discussion as answered, maybe also provide the approach you did take.

This will help others when searching for this.

thanks

Bruno

View solution in original post


All Replies
Contributor
Posts: 70

Re: Extract date from a name file

Can you please provide some examples. so that we can suggest you ....Smiley Happy

Occasional Contributor
Posts: 5

Re: Extract date from a name file

I have a folder with several files:

Garantees_20120730.txt

Financiero_20120730.txt

I need the date (20120730) to a new variable.

Super Contributor
Posts: 349

Re: Extract date from a name file

Hi,

Try this..once you get your file name in the data set then you can compare with whatever values you want .hope it helps...

filename dir "F:\TD\test";

data work.dir;

             length name $100;

             d=dopen("dir");

             num=dnum(d);

             do i=1 to num;

                name=dread(d,i);

r=index(name,".txt");

dt=input(compress(name,,'kd'),yymmdd8.);

                    output;

                 end;

  format dt date9.;

  keep name dt;

  run;

Thanks,

Shiva

SAS Super FREQ
Posts: 683

Re: Extract date from a name file

Hi

when reading from a text file one can use the FILENAME= option of the INFILE statement to get the name of the file being read. This can be very helpfull if you are reading several files with the same DATA Step.


See below for any example. The first DATA Step simply creates a test file.

filename myinput "!TEMP\Garantees_20120730.txt";

data _null_;
 
file myinput dlm=",";
 
do i = 1 to 10;
   
put i "20100730";
 
end;
run;


data someData;
  length
    _myFilename $
2048
    myFilename $
256
  ;
  infile myinput dlm="," filename=_myFilename;
  input
    line
    myDate :
anydtdte.
  ;
  myFilename = scan(_myFilename, -1, "\");

 
format
    myDate
date9.
  ;
run;
Occasional Contributor
Posts: 5

Re: Extract date from a name file

it´s working...

thanks all.

Solution
‎07-31-2012 10:13 AM
SAS Super FREQ
Posts: 683

Re: Extract date from a name file

Hi NTVM

Please mark this discussion as answered, maybe also provide the approach you did take.

This will help others when searching for this.

thanks

Bruno

Respected Advisor
Posts: 3,124

Re: Extract date from a name file

Here is another approach using 'pipe':

filename fname pipe  'dir c:\test\*.txt /b/s';

data test;

infile fname;

input filename :&$100.;

new_var=substr(filename,prxmatch('/_\d{8}\./', filename)+1,8);

output;

run;

Haikuo

☑ This topic is SOLVED.

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

Discussion stats
  • 7 replies
  • 725 views
  • 0 likes
  • 5 in conversation