<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Extracting line from free text that contains a keyword in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Extracting-line-from-free-text-that-contains-a-keyword/m-p/911567#M359451</link>
    <description>&lt;P&gt;I assume that what you have are SAS text variables that contain carriage returns and/or linefeeds. To extract the relevant lines from that, you could try something like this (assuming your initial variable is called TEXT, and the found lines will be in the variable LINE):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  prxid=prxparse('/[^\r\l]*carbapenem[^\r\l]*/i');
  pos=1;
  do until(0);
    call prxsubstr(prxid,text,pos,len);
    if pos=0 then leave;
    line=substr(text,pos,len);
    output;
    pos+length;
    end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 15 Jan 2024 14:42:35 GMT</pubDate>
    <dc:creator>s_lassen</dc:creator>
    <dc:date>2024-01-15T14:42:35Z</dc:date>
    <item>
      <title>Extracting line from free text that contains a keyword</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extracting-line-from-free-text-that-contains-a-keyword/m-p/911328#M359371</link>
      <description>&lt;P&gt;Hello all,&lt;/P&gt;
&lt;P&gt;I've been working with free text - medical notes - that have been uploaded into a SAS dataset. Below are 2 examples that I've copied and pasted as well as a screenshot of the text within Notepad ++ that shows the Carriage Returns/Line Feeds. The keyword is 'carbapenem' and I've tried, without any luck, to extract only the line in the text that contains the keyword using some Regex code. Ideally, I'd like to pull out only the 1st line in example #1 and the 2nd line in example #2.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm looking for any tips or advice on how to pull out the keyword line using Regex or non-Regex code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;"Organism is a confirmed carbapenem. &lt;BR /&gt;If infection is present, preferred treatment &lt;BR /&gt;is with a fluoroquinolone. "&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;"COLONY TYPE 2 &lt;BR /&gt;CARBAPENEM PRODUCER: &lt;BR /&gt;RECOMMEND A TREATMENT FOR COMPLICATED UTI OR SYSTEMIC INFECTION"&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="BrianB4233_1-1705019338784.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/92464i08773934FA151161/image-size/medium?v=v2&amp;amp;px=400" role="button" title="BrianB4233_1-1705019338784.png" alt="BrianB4233_1-1705019338784.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 12 Jan 2024 00:33:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extracting-line-from-free-text-that-contains-a-keyword/m-p/911328#M359371</guid>
      <dc:creator>BrianB4233</dc:creator>
      <dc:date>2024-01-12T00:33:39Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting line from free text that contains a keyword</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extracting-line-from-free-text-that-contains-a-keyword/m-p/911330#M359372</link>
      <description>&lt;P&gt;can you post the data in datalines?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 12 Jan 2024 00:45:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extracting-line-from-free-text-that-contains-a-keyword/m-p/911330#M359372</guid>
      <dc:creator>tarheel13</dc:creator>
      <dc:date>2024-01-12T00:45:35Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting line from free text that contains a keyword</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extracting-line-from-free-text-that-contains-a-keyword/m-p/911333#M359374</link>
      <description>&lt;P&gt;Below code should work&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  infile "c:\temp\source_text.txt" truncover termstr=crlf;
  input line $1000.;
  if find(upcase(line),'CARBAPENEM') then output;
run;

proc print data=want;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1705025292963.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/92465iED1B133C1D34DD18/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_0-1705025292963.png" alt="Patrick_0-1705025292963.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 12 Jan 2024 02:10:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extracting-line-from-free-text-that-contains-a-keyword/m-p/911333#M359374</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2024-01-12T02:10:04Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting line from free text that contains a keyword</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extracting-line-from-free-text-that-contains-a-keyword/m-p/911567#M359451</link>
      <description>&lt;P&gt;I assume that what you have are SAS text variables that contain carriage returns and/or linefeeds. To extract the relevant lines from that, you could try something like this (assuming your initial variable is called TEXT, and the found lines will be in the variable LINE):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  prxid=prxparse('/[^\r\l]*carbapenem[^\r\l]*/i');
  pos=1;
  do until(0);
    call prxsubstr(prxid,text,pos,len);
    if pos=0 then leave;
    line=substr(text,pos,len);
    output;
    pos+length;
    end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 15 Jan 2024 14:42:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extracting-line-from-free-text-that-contains-a-keyword/m-p/911567#M359451</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2024-01-15T14:42:35Z</dc:date>
    </item>
  </channel>
</rss>

