I am finding this problem quite challenging. I want 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;
The result would be 3 lines:
1)This is a TEST. Can you help! 2)scans each line and picks out the word TEST! 3)the word TEST and then concatenates the following lines until it reaches the exclamation symbol!
data test; 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; start = 0; output; end; 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;
> I am finding this problem quite challenging. I want 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;
> The result would be 3 lines:
> 1)This is a TEST. Can you help! > 2)scans each line and picks out the word TEST! > 3)the word TEST and then concatenates the following lines until it reaches the exclamation symbol!
I've presumed to make a few changes to a generous solution to a cleverly-presented problem: data test (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;
On Behalf Of Lewis Jordan Sent: Sunday, May 11, 2008 3:28 PM To: SAS-L Subject: Challenging? Search Text File and Concatenate Rows
Generous SAS Users:
I am finding this problem quite challenging. I want 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;
The result would be 3 lines:
1)This is a TEST. Can you help! 2)scans each line and picks out the word TEST! 3)the word TEST and then concatenates the following lines until it reaches the exclamation symbol!
I think this is an excellent case for a paired DO UNTIL and DO WHILE as below. Also, since you said "concatenate", I literally concatenated the sequence of lines you wanted into a single variable in a single observation. Of course, my program assumes that no complete concatenation requires more than 256 bytes.
Regards, Mark
data test (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;
-----Original Message----- From: SAS(r) Discussion [mailto:SA...@LISTSERV.UGA.EDU] On Behalf Of
Lewis Jordan Sent: Sunday, May 11, 2008 3:28 PM To: SA...@LISTSERV.UGA.EDU Subject: Challenging? Search Text File and Concatenate Rows
Generous SAS Users:
I am finding this problem quite challenging. I want 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;
The result would be 3 lines:
1)This is a TEST. Can you help! 2)scans each line and picks out the word TEST! 3)the word TEST and then concatenates the following lines until it reaches the exclamation symbol!