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;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.
Ready to level-up your skills? Choose your own adventure.