« SAS flow control - the goto statement | Main | An Interesting Lesson Merging SAS Datasets »

SAS extract sentence when 2 string match conditions exist

The cleverness of the SAS Google Group shines through this past week with an interesting challenge.  Here was the problem presented:

...to scan rows of a text file for a "Primary Key" word (or symbol). When I find the "Primary Key", I want to concatenate the ENTIRE line the "Primary Key" word appeared on with all rows underneath it until I find another, "Secondary Key" word. Consider the snippet below. The "Primary Key" = TEST, the "Secondary Key" = !

data test;
input line $ 50.;
cards;
This is a TEST.
Can you help!
I want to write SAS code that
scans each line and picks out the word TEST!
The program should search each line for
the word TEST and then concatenates the
following lines until it reaches
the exclamation symbol!
;
proc print;
run;quit;

Solution 1:

 data test2 (keep=string);
   infile cards;
   input;
   length string $256;
   retain string ' ' start 0;
   if indexW(_infile_,'TEST',' !.') then do;
      start=1;
      call missing(string);
      end;
   if start then string = catx(' ',string,_infile_);
   if index(_infile_,'!') then do;
      if start ^= 0 then output;
      start = 0;
      end;
   cards;
This looks like homework!!!!!
This is a TEST.
Can you help!
I want to write SAS code that
scans each line and picks out the word TEST!
The program should search each line for
the word TEST and then concatenates the
following lines until it reaches
the exclamation symbol!
;
proc print noobs;
run;quit;

Solution 2:

 

 

/* This solution uses a paired DO UNTIL and DO WHILE.  Also concatenated the sequence of lines wanted into a single variable in a single observation.  This program assumes that no complete concatenation requires more than 256 bytes. */

data test3 (keep=complete_expression);
  length complete_expression $256;
  infile cards ;
   do until (index(_infile_,'TEST')^=0);
     input;
   end;
   complete_expression=_infile_;
   do while (index(_infile_,'!')=0);
     input;
     complete_expression=catx(' ',complete_expression,_infile_);
   end;
   output;
cards;
This is a TEST.
Can you help!
I want to write SAS code that
scans each line and picks out the word TEST!
The program should search each line for
the word TEST and then concatenates the
following lines until it reaches
the exclamation symbol!
;
run;

I am unsure if or when I would use this exact code, but I think the real value is it helps learn more about string matching and conditional programming. 

 

 

 

Posted on Sunday, May 25, 2008 by Registered CommenterJared in | CommentsPost a Comment

PrintView Printer Friendly Version

EmailEmail Article to Friend

Reader Comments

There are no comments for this journal entry. To create a new comment, use the form below.

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>