I have several hundreds of tab delimited text files and the data I want to import starts in different rows for each text file.
The data starts at the row after the word "Sleep Stage" .
Can I somehow search for this string in my infile statement? In this example code the data starts at row 205 , but it can vary.
%macro import_file(path, file_name, dataset_name);
data &dataset_name.;
infile "&path.\&file_name." DSD DLM="09"x firstobs=205;
input Sleep $ Position :$10. Time :$8. event :$20. Duration :8.;
run;
%mend;
Searching with the infile-statement is not possible. I would use two input statement (untested code):
%macro import_file(path, file_name, dataset_name);
data &dataset_name.;
length _dataFound 8;
retain _dataFound 0;
drop _dataFound;
infile "&path.\&file_name." DSD DLM="09"x;
if not _dataFound then do;
input @;
_dataFound = find(_infile_, 'Sleep Stage') > 0;
end;
if _dataFound then do;
input Sleep $ Position :$10. Time :$8. event :$20. Duration :8.;
output;
end;
run;
%mend;
EDIT: remove firstobs from infile
Searching with the infile-statement is not possible. I would use two input statement (untested code):
%macro import_file(path, file_name, dataset_name);
data &dataset_name.;
length _dataFound 8;
retain _dataFound 0;
drop _dataFound;
infile "&path.\&file_name." DSD DLM="09"x;
if not _dataFound then do;
input @;
_dataFound = find(_infile_, 'Sleep Stage') > 0;
end;
if _dataFound then do;
input Sleep $ Position :$10. Time :$8. event :$20. Duration :8.;
output;
end;
run;
%mend;
EDIT: remove firstobs from infile
Hi @TobbeNord To address
'It should start import the row below "sleep stage" .'
Add an ELSE as shown below
if not _dataFound then do;
input @;
_dataFound = find(_infile_, 'Sleep Stage') > 0;
end;
else
if _dataFound then do;
input Sleep $ Position :$10. Time :$8. event :$20. Duration :8.;
output;
end;
The data starts at the row after the word "Sleep Stage" .
So just read lines until you find it.
data &dataset_name.;
infile "&path.\&file_name." DSD DLM="09"x ;
if _n_=1 then do until (upcase(_infile_) contains 'SLEEP STAGE');
input;
end;
input Sleep $ Position :$10. Time :$8. event :$20. Duration :8.;
run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.