DATA Step, Macro, Functions and more

Reading in .dat file with extraneous data

Reply
Occasional Contributor
Posts: 5

Reading in .dat file with extraneous data

Hello,

I am trying to pull data from a .dat file based on pattern matching criteria. This file has extra info about locations, conditions, instruments, etc. at the start of the file, but the data I want is further down. I am able to using matching criteria to obtain the first line of data, but then it stops after that line because the match no longer applies.
Ex:
** Today's data file
Location = river brook
Three staff present
Lamprey tracking project

Date Time Location Sample Total
03/08/10 08:10 001 034 56
03/08/10 08:15 001 035 56


data counts;
infile 'C:\mydata\file.dat';
input @'Location Sample Total ' date time $8. location sample total;
run;

Is there a way to match to read the first line and then continue reading until the end? Or some other way to pull only certain info form this file? Thank you for any suggestions! Message was edited by: Tiffany
Super Contributor
Super Contributor
Posts: 3,174

Re: Reading in .dat file with extraneous data

From the data-sample you provided, there will be no match - where is the string "Events " in your input?

Get your INPUT stmt @'' sorted out and then use the END=EOF parameter on the INFILE statement, and code a DO / END loop logic after you get the record pointer to the start of your data. You will likely have at least two INPUT statements, one outside the DO loop (ahead to get to the desired input location) and another (one or more as needed) to perform the data record INPUT processing.

For self-checking, suggest adding one or more PUTLOG commands in your code at various points (use "nnn" to identify each uniquely):

PUTLOG '>DIAG-nnn>' / _all_;

..and/or...

PUTLOG '>DIAG-nnn>' / _infile_;

Scott Barry
SBBWorks, Inc.
Occasional Contributor
Posts: 5

Re: Reading in .dat file with extraneous data

Thank you Scott!

This is what I have now, but I am still missing something critical:

data counts;
infile 'C:\mydata\file.dat' end=eof;
input @'Location Sample Total ' date time $8. location sample total;
do until (eof);
input date time $8. location sample total;
end;
run;

any thoughts?
Respected Advisor
Posts: 3,799

Re: Reading in .dat file with extraneous data

[pre]
data counts;
infile 'C:\mydata\file.dat' end=eof;
input @'Location Sample Total ' date time $8. location sample total;
do until (eof);
input date time $8. location sample total;
output;
end;
stop;
run;
[/pre]
Occasional Contributor
Posts: 5

Re: Reading in .dat file with extraneous data

Posted in reply to data_null__
perfect. thank you!
Ask a Question
Discussion stats
  • 4 replies
  • 745 views
  • 0 likes
  • 3 in conversation