<?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: Efficient way to extract words from text and place in new variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Efficient-way-to-extract-words-from-text-and-place-in-new/m-p/629640#M186245</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/86015"&gt;@Caetreviop543&lt;/a&gt;&amp;nbsp; For hard coded char constants, you could consider a fast approach which is merely tweak of your original. See if this helps&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input TXT &amp;amp; :$10. ;
cards;

Some text

More text

List text

Read text
text red
;

data want;
 set have;
 array t(5) $32 _temporary_ ('Some' 'More' 'List' 'Read' 'red') ;
 length extract $32;
 do _n_=1 to dim(t) until(not missing(extract));
  if index(txt,strip(t(_n_))) then extract=t(_n_);
 end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 04 Mar 2020 22:48:11 GMT</pubDate>
    <dc:creator>novinosrin</dc:creator>
    <dc:date>2020-03-04T22:48:11Z</dc:date>
    <item>
      <title>Efficient way to extract words from text and place in new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Efficient-way-to-extract-words-from-text-and-place-in-new/m-p/629610#M186225</link>
      <description>&lt;P&gt;I'm trying to figure out the best, most succinct way to extract words from a text variable, and place them in a new variable. I have a text variable:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;TXT&lt;/P&gt;&lt;P&gt;Some text&lt;/P&gt;&lt;P&gt;More text&lt;/P&gt;&lt;P&gt;List text&lt;/P&gt;&lt;P&gt;Read text&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And I want to extract certain words from TXT and create a new variable:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;TXT&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Extract&lt;/P&gt;&lt;P&gt;text Some&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Some&lt;/P&gt;&lt;P&gt;More text&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;More&lt;/P&gt;&lt;P&gt;text word List&amp;nbsp; &amp;nbsp; &amp;nbsp;List&lt;/P&gt;&lt;P&gt;Read text&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Read&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So far this is the only code I came up with:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data new;
set old;

if find(TXT, "Some")&amp;gt;0 then Extract = "Some";
else if find(TXT, "More")&amp;gt;0 then Extract = "More";
else if find(TXT, "List")&amp;gt;0 then Extract = "List";
else if find(TXT, "Read")&amp;gt;0 the Extract = "Read";

run;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It works, but is pretty clunky. Does anyone know&amp;nbsp; a better way?&lt;/P&gt;</description>
      <pubDate>Wed, 04 Mar 2020 21:34:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Efficient-way-to-extract-words-from-text-and-place-in-new/m-p/629610#M186225</guid>
      <dc:creator>Caetreviop543</dc:creator>
      <dc:date>2020-03-04T21:34:35Z</dc:date>
    </item>
    <item>
      <title>Re: Efficient way to extract words from text and place in new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Efficient-way-to-extract-words-from-text-and-place-in-new/m-p/629612#M186227</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Extract=scan(txt,1,' ');&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 04 Mar 2020 21:32:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Efficient-way-to-extract-words-from-text-and-place-in-new/m-p/629612#M186227</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-03-04T21:32:48Z</dc:date>
    </item>
    <item>
      <title>Re: Efficient way to extract words from text and place in new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Efficient-way-to-extract-words-from-text-and-place-in-new/m-p/629613#M186228</link>
      <description>&lt;P&gt;I made the example too simple; the word I want to extract isn't always the first.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 04 Mar 2020 21:35:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Efficient-way-to-extract-words-from-text-and-place-in-new/m-p/629613#M186228</guid>
      <dc:creator>Caetreviop543</dc:creator>
      <dc:date>2020-03-04T21:35:05Z</dc:date>
    </item>
    <item>
      <title>Re: Efficient way to extract words from text and place in new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Efficient-way-to-extract-words-from-text-and-place-in-new/m-p/629614#M186229</link>
      <description>&lt;P&gt;Oops sorry I overlooked completely. my apologies&lt;/P&gt;</description>
      <pubDate>Wed, 04 Mar 2020 21:38:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Efficient-way-to-extract-words-from-text-and-place-in-new/m-p/629614#M186229</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-03-04T21:38:28Z</dc:date>
    </item>
    <item>
      <title>Re: Efficient way to extract words from text and place in new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Efficient-way-to-extract-words-from-text-and-place-in-new/m-p/629615#M186230</link>
      <description>&lt;P&gt;That's ok! I should have made the example better.&lt;/P&gt;</description>
      <pubDate>Wed, 04 Mar 2020 21:45:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Efficient-way-to-extract-words-from-text-and-place-in-new/m-p/629615#M186230</guid>
      <dc:creator>Caetreviop543</dc:creator>
      <dc:date>2020-03-04T21:45:19Z</dc:date>
    </item>
    <item>
      <title>Re: Efficient way to extract words from text and place in new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Efficient-way-to-extract-words-from-text-and-place-in-new/m-p/629617#M186231</link>
      <description>&lt;P&gt;HI&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/86015"&gt;@Caetreviop543&lt;/a&gt;&amp;nbsp; Would this come close?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Basically, eliminating the word 'text' in the string&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input TXT &amp;amp; :$10. ;
cards;

Some text

More text

List text

Read text
text red
;

data want;
 set have;
 temp=strip(tranwrd(txt,'text',' '));
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 04 Mar 2020 21:55:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Efficient-way-to-extract-words-from-text-and-place-in-new/m-p/629617#M186231</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-03-04T21:55:08Z</dc:date>
    </item>
    <item>
      <title>Re: Efficient way to extract words from text and place in new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Efficient-way-to-extract-words-from-text-and-place-in-new/m-p/629619#M186232</link>
      <description>&lt;P&gt;Thanks for your response. Is there a way to extract just the word if the text isn't all the same, i.e., the actual word text doesn't appear throughout? I just used text to symbolize a string of words.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 04 Mar 2020 21:59:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Efficient-way-to-extract-words-from-text-and-place-in-new/m-p/629619#M186232</guid>
      <dc:creator>Caetreviop543</dc:creator>
      <dc:date>2020-03-04T21:59:47Z</dc:date>
    </item>
    <item>
      <title>Re: Efficient way to extract words from text and place in new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Efficient-way-to-extract-words-from-text-and-place-in-new/m-p/629620#M186233</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is there a way&lt;STRONG&gt; to extract just the word&lt;/STRONG&gt; if the text isn't all the same,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;By the above, do you mean you only want to extract a set of &lt;STRONG&gt;known&lt;/STRONG&gt; words? Like as though you have a list of words to extract from the string???Please clarify&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/86015"&gt;@Caetreviop543&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thanks for your response. Is there a way to extract just the word if the text isn't all the same, i.e., the actual word text doesn't appear throughout? I just used text to symbolize a string of words.&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 04 Mar 2020 22:03:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Efficient-way-to-extract-words-from-text-and-place-in-new/m-p/629620#M186233</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-03-04T22:03:48Z</dc:date>
    </item>
    <item>
      <title>Re: Efficient way to extract words from text and place in new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Efficient-way-to-extract-words-from-text-and-place-in-new/m-p/629621#M186234</link>
      <description>&lt;P&gt;You can write this in a more compact manner, but it is less legible.&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;EXTRACT = prxchange('s/.*(Some|More|List|Read).*/\1/',-1,TXT);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;This means: find anything then one of the strings then anything else and replace with the string found.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 04 Mar 2020 22:05:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Efficient-way-to-extract-words-from-text-and-place-in-new/m-p/629621#M186234</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2020-03-04T22:05:10Z</dc:date>
    </item>
    <item>
      <title>Re: Efficient way to extract words from text and place in new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Efficient-way-to-extract-words-from-text-and-place-in-new/m-p/629623#M186235</link>
      <description>&lt;P&gt;It told me the prxchange argument doesn't have enough arguments. How does it know which variable to change/extract words from?&lt;/P&gt;</description>
      <pubDate>Wed, 04 Mar 2020 22:09:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Efficient-way-to-extract-words-from-text-and-place-in-new/m-p/629623#M186235</guid>
      <dc:creator>Caetreviop543</dc:creator>
      <dc:date>2020-03-04T22:09:23Z</dc:date>
    </item>
    <item>
      <title>Re: Efficient way to extract words from text and place in new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Efficient-way-to-extract-words-from-text-and-place-in-new/m-p/629624#M186236</link>
      <description>&lt;P&gt;Try again, I corrected within seconds . I clicked too fast and you are too fast. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 04 Mar 2020 22:11:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Efficient-way-to-extract-words-from-text-and-place-in-new/m-p/629624#M186236</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2020-03-04T22:11:06Z</dc:date>
    </item>
    <item>
      <title>Re: Efficient way to extract words from text and place in new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Efficient-way-to-extract-words-from-text-and-place-in-new/m-p/629627#M186237</link>
      <description>&lt;P&gt;1 is probably fine rather than -1 actually. Sorry I can't test.&lt;/P&gt;</description>
      <pubDate>Wed, 04 Mar 2020 22:15:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Efficient-way-to-extract-words-from-text-and-place-in-new/m-p/629627#M186237</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2020-03-04T22:15:23Z</dc:date>
    </item>
    <item>
      <title>Re: Efficient way to extract words from text and place in new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Efficient-way-to-extract-words-from-text-and-place-in-new/m-p/629628#M186238</link>
      <description>&lt;P&gt;Thanks, but that didn't work. It returned a much larger list of lots of text.&lt;/P&gt;</description>
      <pubDate>Wed, 04 Mar 2020 22:16:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Efficient-way-to-extract-words-from-text-and-place-in-new/m-p/629628#M186238</guid>
      <dc:creator>Caetreviop543</dc:creator>
      <dc:date>2020-03-04T22:16:31Z</dc:date>
    </item>
    <item>
      <title>Re: Efficient way to extract words from text and place in new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Efficient-way-to-extract-words-from-text-and-place-in-new/m-p/629639#M186244</link>
      <description>&lt;P&gt;Mmm that's odd. I probably forgot something obvious. I can't test sadly.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 04 Mar 2020 22:44:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Efficient-way-to-extract-words-from-text-and-place-in-new/m-p/629639#M186244</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2020-03-04T22:44:36Z</dc:date>
    </item>
    <item>
      <title>Re: Efficient way to extract words from text and place in new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Efficient-way-to-extract-words-from-text-and-place-in-new/m-p/629640#M186245</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/86015"&gt;@Caetreviop543&lt;/a&gt;&amp;nbsp; For hard coded char constants, you could consider a fast approach which is merely tweak of your original. See if this helps&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input TXT &amp;amp; :$10. ;
cards;

Some text

More text

List text

Read text
text red
;

data want;
 set have;
 array t(5) $32 _temporary_ ('Some' 'More' 'List' 'Read' 'red') ;
 length extract $32;
 do _n_=1 to dim(t) until(not missing(extract));
  if index(txt,strip(t(_n_))) then extract=t(_n_);
 end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 04 Mar 2020 22:48:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Efficient-way-to-extract-words-from-text-and-place-in-new/m-p/629640#M186245</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-03-04T22:48:11Z</dc:date>
    </item>
    <item>
      <title>Re: Efficient way to extract words from text and place in new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Efficient-way-to-extract-words-from-text-and-place-in-new/m-p/629647#M186252</link>
      <description>&lt;P&gt;Thanks that worked!!&lt;/P&gt;</description>
      <pubDate>Wed, 04 Mar 2020 23:09:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Efficient-way-to-extract-words-from-text-and-place-in-new/m-p/629647#M186252</guid>
      <dc:creator>Caetreviop543</dc:creator>
      <dc:date>2020-03-04T23:09:00Z</dc:date>
    </item>
  </channel>
</rss>

