BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
NTVM
Calcite | Level 5

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.

1 ACCEPTED SOLUTION

Accepted Solutions
BrunoMueller
SAS Super FREQ

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

7 REPLIES 7
yaswanthj
Calcite | Level 5

Can you please provide some examples. so that we can suggest you ....:)

NTVM
Calcite | Level 5

I have a folder with several files:

Garantees_20120730.txt

Financiero_20120730.txt

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

shivas
Pyrite | Level 9

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

BrunoMueller
SAS Super FREQ
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;
NTVM
Calcite | Level 5

it´s working...

thanks all.

BrunoMueller
SAS Super FREQ

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

Haikuo
Onyx | Level 15

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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 7 replies
  • 4294 views
  • 0 likes
  • 5 in conversation