<?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: What is the fastest way to extract varying words into a new column? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/What-is-the-fastest-way-to-extract-varying-words-into-a-new/m-p/830680#M328229</link>
    <description>Yes it did, but my original question was looking for a simpler way, if you bothered to read it. What a useless response.</description>
    <pubDate>Fri, 26 Aug 2022 19:51:30 GMT</pubDate>
    <dc:creator>A-junamu_-</dc:creator>
    <dc:date>2022-08-26T19:51:30Z</dc:date>
    <item>
      <title>What is the fastest way to extract varying words into a new column?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/What-is-the-fastest-way-to-extract-varying-words-into-a-new/m-p/830610#M328198</link>
      <description>&lt;P&gt;For example, I have a data set, with column name school subjects students are interested in. The old column would look like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;U&gt;School Subjects:&lt;/U&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;Health science and Math&lt;/P&gt;&lt;P&gt;Biology&lt;/P&gt;&lt;P&gt;Biology and Geology&lt;/P&gt;&lt;P&gt;Arts and Computer Science&lt;/P&gt;&lt;P&gt;Geology and Health Science&lt;/P&gt;&lt;P&gt;Math&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My goal, is to extract the second school subject (ie Math or Geology) and put it in a new column to create the desired result:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;U&gt;School Subject 1&amp;nbsp;&lt;/U&gt; &lt;/STRONG&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;U&gt;&lt;STRONG&gt;School Subject 2&lt;/STRONG&gt;&lt;/U&gt;&lt;/P&gt;&lt;P&gt;Health Science&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Math&lt;/P&gt;&lt;P&gt;Biology&lt;/P&gt;&lt;P&gt;Biology&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Geology&lt;/P&gt;&lt;P&gt;Arts&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Computer Science&lt;/P&gt;&lt;P&gt;Geology&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Health Science&lt;/P&gt;&lt;P&gt;Math&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My solution was to create 2 conversion types, based on "and" and use if and then statement to separate the subjects:&lt;/P&gt;&lt;LI-CODE lang="sas"&gt;Conversiontype1 = scan(school_subjects, -2, " ");

Conversiontype2 = scan(school_subjects, 2, " ");

If conversiontype1 = "and" then school_subject1 = scan(school_subjects, -1, " ");

Else if conversiontype2 = "and" then school_subject2 = catx(" ", scan(school_subjects, -2), scan(school_subjects, -1));

else school_subject1 = school_subjects;

run;&amp;nbsp;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is there a simpler way of doing this?&lt;/P&gt;</description>
      <pubDate>Fri, 26 Aug 2022 14:33:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/What-is-the-fastest-way-to-extract-varying-words-into-a-new/m-p/830610#M328198</guid>
      <dc:creator>A-junamu_-</dc:creator>
      <dc:date>2022-08-26T14:33:18Z</dc:date>
    </item>
    <item>
      <title>Re: What is the fastest way to extract varying words into a new column?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/What-is-the-fastest-way-to-extract-varying-words-into-a-new/m-p/830619#M328204</link>
      <description>&lt;P&gt;One way. Note provided example data as a data step.&lt;/P&gt;
&lt;P&gt;The findw function returns the position a word is found, or 0 if not, so you can adjust position to select characters by offsetting from that value when found. NOTE: you do not want the FIND function which would find the "and" as part of other words like "hand". If the spelling is inconsistent , i.e. sometimes 'and' or sometimes 'And' you can add a parameter to the findw to ignore case: Findw(source,'and',,'i')&lt;/P&gt;
&lt;PRE&gt;data have;
  input Subject $40.;
datalines;
Health science and Math
Biology
Biology and Geology
Arts and Computer Science
Geology and Health Science
Math
;

data want;
   set have;
   length subject2 $ 40;
   if findw(subject,'and')&amp;gt;0 then do;
      subject2 = substr(subject,findw(subject,'and')+4);
      subject   = substr(subject,1,findw(subject,'and')-1);
      
   end;
run;&lt;/PRE&gt;</description>
      <pubDate>Fri, 26 Aug 2022 15:20:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/What-is-the-fastest-way-to-extract-varying-words-into-a-new/m-p/830619#M328204</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-08-26T15:20:48Z</dc:date>
    </item>
    <item>
      <title>Re: What is the fastest way to extract varying words into a new column?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/What-is-the-fastest-way-to-extract-varying-words-into-a-new/m-p/830668#M328224</link>
      <description>The problem is that it's a large data set, so I cannot use input. I need a code that can use "and" as a delimiter to separate the subjects.</description>
      <pubDate>Fri, 26 Aug 2022 19:10:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/What-is-the-fastest-way-to-extract-varying-words-into-a-new/m-p/830668#M328224</guid>
      <dc:creator>A-junamu_-</dc:creator>
      <dc:date>2022-08-26T19:10:38Z</dc:date>
    </item>
    <item>
      <title>Re: What is the fastest way to extract varying words into a new column?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/What-is-the-fastest-way-to-extract-varying-words-into-a-new/m-p/830675#M328227</link>
      <description>&lt;P&gt;The Input was to have some data that code can be tested with.&lt;/P&gt;
&lt;P&gt;You didn't even provide an actual workable variable name so I had to make something.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Did you run the code? Did it do what you requested with&amp;nbsp; those values?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Use YOUR data set on the SET statement. Use your variable name instead of "subject".&lt;/P&gt;
&lt;P&gt;Then see what happens.&lt;/P&gt;</description>
      <pubDate>Fri, 26 Aug 2022 19:24:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/What-is-the-fastest-way-to-extract-varying-words-into-a-new/m-p/830675#M328227</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-08-26T19:24:40Z</dc:date>
    </item>
    <item>
      <title>Re: What is the fastest way to extract varying words into a new column?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/What-is-the-fastest-way-to-extract-varying-words-into-a-new/m-p/830680#M328229</link>
      <description>Yes it did, but my original question was looking for a simpler way, if you bothered to read it. What a useless response.</description>
      <pubDate>Fri, 26 Aug 2022 19:51:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/What-is-the-fastest-way-to-extract-varying-words-into-a-new/m-p/830680#M328229</guid>
      <dc:creator>A-junamu_-</dc:creator>
      <dc:date>2022-08-26T19:51:30Z</dc:date>
    </item>
    <item>
      <title>Re: What is the fastest way to extract varying words into a new column?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/What-is-the-fastest-way-to-extract-varying-words-into-a-new/m-p/830685#M328231</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/411531"&gt;@A-junamu_-&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Yes it did, but my original question was looking for a simpler way, if you bothered to read it. What a useless response.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Define simpler.&lt;/P&gt;
&lt;P&gt;Your solution uses two additional variables.&lt;/P&gt;
&lt;P&gt;Mine uses no additional variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Yours &lt;STRONG&gt;fails&lt;/STRONG&gt; if the second subject following "and" is more than two words, mine doesn't.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your post title says "faster" not "simpler". I'm not going to bother to create data set large enough to get performance statistics for comparing "faster" or not. Because when I test your code snippet, against the data set I provide code for, your School_subject1 does not get assigned for the examples "Arts and Computer Science" and "Geology and Health Science". Your code:&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;Else if conversiontype2 = "and" then school_subject2 = catx(" ", scan(school_subjects, -2), scan(school_subjects, -1));

else school_subject1 = school_subjects;&lt;/LI-CODE&gt;
&lt;P&gt;fails to make an assignment to School_subject1 when the Conversiontype2= "and" is true. Just plain fails. So does not result in two variables each with one subject.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You did not provide full code for your data step so it may be that was accomplished elsewhere.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or a basic 2 line solution into 2 new variables for the example data set.&lt;/P&gt;
&lt;PRE&gt;data want;
   set have;
   sub1= scan(tranwrd(subject," and ",'+'),1,'+');
   sub2= scan(tranwrd(subject," and ",'+'),2,'+');
run; &lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please be &lt;STRONG&gt;very&lt;/STRONG&gt; cautious about calling responses "useless" on this forum.&lt;/P&gt;</description>
      <pubDate>Fri, 26 Aug 2022 20:49:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/What-is-the-fastest-way-to-extract-varying-words-into-a-new/m-p/830685#M328231</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-08-26T20:49:46Z</dc:date>
    </item>
    <item>
      <title>Re: What is the fastest way to extract varying words into a new column?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/What-is-the-fastest-way-to-extract-varying-words-into-a-new/m-p/830686#M328232</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/411531"&gt;@A-junamu_-&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Yes it did, but my original question was looking for a simpler way, if you bothered to read it. What a useless response.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;That's not a great response when your code does not work, you don't understand the basic example data post, and you're asking for free help from perfect strangers in a new forum.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 26 Aug 2022 20:48:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/What-is-the-fastest-way-to-extract-varying-words-into-a-new/m-p/830686#M328232</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-08-26T20:48:27Z</dc:date>
    </item>
    <item>
      <title>Re: What is the fastest way to extract varying words into a new column?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/What-is-the-fastest-way-to-extract-varying-words-into-a-new/m-p/830732#M328264</link>
      <description>&lt;P&gt;Why ?&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp; has already given you the excellent solution .&lt;/P&gt;
&lt;P&gt;If you really want take ' and ' a dlmstr,Here is the one :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data have;
  input Subject $40.;
datalines;
Health science and Math
Biology
Biology and Geology
Arts and Computer Science
Geology and Health Science
Math
;

filename x temp;
data _null_;
file x;
set have;
put subject;
;

data want;
infile x dlmstr=' and ' truncover;
input subject1 : $100. subject2 : $100. ;
run;&lt;/PRE&gt;</description>
      <pubDate>Sat, 27 Aug 2022 12:31:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/What-is-the-fastest-way-to-extract-varying-words-into-a-new/m-p/830732#M328264</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2022-08-27T12:31:41Z</dc:date>
    </item>
  </channel>
</rss>

