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

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;

1 ACCEPTED SOLUTION

Accepted Solutions
andreas_lds
Jade | Level 19

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

View solution in original post

4 REPLIES 4
andreas_lds
Jade | Level 19

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

TobbeNord
Obsidian | Level 7
Thank you!
It almost worked as wanted.
The only little "bug" is that it also import the row with "sleep stage" on it.
It should start import the row below "sleep stage" .
novinosrin
Tourmaline | Level 20

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;
Tom
Super User Tom
Super User

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;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 4 replies
  • 1657 views
  • 4 likes
  • 4 in conversation