BookmarkSubscribeRSS Feed
mick_g
Calcite | Level 5

I have txt files called

2011.06.01_case.txt

2011.06.02_case.txt

2011.06.03_case.txt

2011.06.04_case.txt

2011.06.05_case.txt

ext.

I need to read in all the txt file for the past 30 days and attached the date on the txt file to the data inside the file

I started by createding a beginning case_date

Proc sql;

create table p_date as

select datepart(today()) as process_date;

quit;

Data case_date;

set p_date;

case_date = intnx('day',process_date,-30);

run;

I am having trouble with the loop;

do until &case_date = process_date;

Data &case_date_tracking;

Infile "site_mgt\&case_date_case.txt "

Firstobs = 2 dsd missover;

informat Case_number

             &Case_date;

input      Case_number

              &Case_date;

next &case_date;

run;

any help would be greatly appreciated

3 REPLIES 3
data_null__
Jade | Level 19

You can use a wildcard FILE reference and read all the files as one stream and create a variable to identify the source of input as your read.  The relevant INFILE statement options are EOV and FILENAME you may need other options too.

filename FT55F001 '*_case.txt';

data cases;

   length FILENAME FILE $256;

   retain EOV 1;

   infile FT55F001 EOV=EOV FILENAME=FILENAME;

   input .......;

   if eov then do;

      File=Filename;

      eov = 0;

      end;

   run;

DLing
Obsidian | Level 7

data_null_'s solution is great for processing large number of files.  Using wildcards to selectively generate the proper filename statement is flexible and powerful.

Here's a more programmatic approach:

%macro dayloop(days);

     %let now=%sysfunc(today());     * set to whatever end date;

     %do i = %eval(1-&days) %to 0;  * 7 becomes: -6 -5 -4 -3 -2 -1 0 ;

          %let date = %sysfunc(intnx(day,&now,&i),yymmdd10.);   * format according to need;

           %put &i &date;

          filename txt "&date.xxxxxxx";

          data txt&date;

               retain date xxxxxxxxxx;

               infile xxxxxxxx;

               input xxxxxxxxxx;

          run;

    

     %end;

%mend;

%dayloop( 30 )

Ksharp
Super User

How about:

%let path=c:\temp\ ;
filename txt pipe "dir &path*.txt /B";
data want;
 infile txt truncover ;
 input txt_name $50.;
 txt_name="&path"||strip(txt_name);
 length fname $ 50;
 infile dummy filevar=txt_name filename=fname end=last length=len;
 do until(last);
 input row $varying200. len;
 filename=scan(fname,-2,'.\');
 output;
 end;
run;

Ksharp

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1511 views
  • 0 likes
  • 4 in conversation