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;

SAS Innovate 2025: Call for Content

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!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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