<?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 Appending a column from one table to another in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Appending-a-column-from-one-table-to-another/m-p/551548#M153250</link>
    <description>&lt;P&gt;I am using Enterprise Guide 7.13 and SAS version 9.4.&amp;nbsp; I have two datasets. One has several field including a text field with sentences in it.&amp;nbsp; The other dataset I created made up of key words.&amp;nbsp; I want to search each of the fields in the first dataset to see if they contain one of the key words in the second dataset. If a row in the first dataset contains one of the key words, I want to append that word to the dataset for that row.&amp;nbsp; If there is no match the new column would be empty.&amp;nbsp;&amp;nbsp;The end goal is to count the number of matches for each keyword.&amp;nbsp; I have code that will do the matching and output those matches to a new dataset, but not append the key word as a separate variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have an excel sheet enclosed that illustrates what I am looking for.&amp;nbsp; The code below creates a dataset of the matches.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;data&lt;/STRONG&gt;&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;_null_&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;set&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;&amp;nbsp;dataset2 &lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;end&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;=last;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;if&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; _n_ = &lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;1&lt;/FONT&gt;&lt;/STRONG&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;then&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;call&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; execute(&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;'data dataset3; set dataset1; '&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;call&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; execute(cats(&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;'if findw(upcase(information),"'&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;,upcase(word),&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;'") &amp;gt; 0 then output;'&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;));&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;if&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; last &lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;then&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;call&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; execute(&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;'run;'&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;RUN&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;</description>
    <pubDate>Tue, 16 Apr 2019 22:49:52 GMT</pubDate>
    <dc:creator>pangea17</dc:creator>
    <dc:date>2019-04-16T22:49:52Z</dc:date>
    <item>
      <title>Appending a column from one table to another</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Appending-a-column-from-one-table-to-another/m-p/551548#M153250</link>
      <description>&lt;P&gt;I am using Enterprise Guide 7.13 and SAS version 9.4.&amp;nbsp; I have two datasets. One has several field including a text field with sentences in it.&amp;nbsp; The other dataset I created made up of key words.&amp;nbsp; I want to search each of the fields in the first dataset to see if they contain one of the key words in the second dataset. If a row in the first dataset contains one of the key words, I want to append that word to the dataset for that row.&amp;nbsp; If there is no match the new column would be empty.&amp;nbsp;&amp;nbsp;The end goal is to count the number of matches for each keyword.&amp;nbsp; I have code that will do the matching and output those matches to a new dataset, but not append the key word as a separate variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have an excel sheet enclosed that illustrates what I am looking for.&amp;nbsp; The code below creates a dataset of the matches.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;data&lt;/STRONG&gt;&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;_null_&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;set&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;&amp;nbsp;dataset2 &lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;end&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;=last;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;if&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; _n_ = &lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;1&lt;/FONT&gt;&lt;/STRONG&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;then&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;call&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; execute(&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;'data dataset3; set dataset1; '&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;call&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; execute(cats(&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;'if findw(upcase(information),"'&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;,upcase(word),&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;'") &amp;gt; 0 then output;'&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;));&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;if&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; last &lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;then&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;call&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; execute(&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;'run;'&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;RUN&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 16 Apr 2019 22:49:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Appending-a-column-from-one-table-to-another/m-p/551548#M153250</guid>
      <dc:creator>pangea17</dc:creator>
      <dc:date>2019-04-16T22:49:52Z</dc:date>
    </item>
    <item>
      <title>Re: Appending a column from one table to another</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Appending-a-column-from-one-table-to-another/m-p/551565#M153254</link>
      <description>&lt;P&gt;Can you post your datasets in a more usable form for us to work with?&lt;/P&gt;</description>
      <pubDate>Tue, 16 Apr 2019 21:51:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Appending-a-column-from-one-table-to-another/m-p/551565#M153254</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-04-16T21:51:59Z</dc:date>
    </item>
    <item>
      <title>Re: Appending a column from one table to another</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Appending-a-column-from-one-table-to-another/m-p/551570#M153258</link>
      <description>&lt;P&gt;What does the output look like if two or more of the words in dataset2 are found in the same sentence?&lt;/P&gt;
&lt;P&gt;Are the matches supposed to be case sensitive? i.e. does "Military" match "military"?&lt;/P&gt;
&lt;P&gt;What are the rules for a word that appears in a composite use such as "police" in "policeman".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Instructions here: &lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712" target="_blank"&gt;https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712&lt;/A&gt; will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the {i} icon or attached as text to show exactly what you have and that we can test code against.&lt;/P&gt;</description>
      <pubDate>Tue, 16 Apr 2019 22:10:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Appending-a-column-from-one-table-to-another/m-p/551570#M153258</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-04-16T22:10:04Z</dc:date>
    </item>
    <item>
      <title>Re: Appending a column from one table to another</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Appending-a-column-from-one-table-to-another/m-p/551579#M153264</link>
      <description>I believe SAS is case sensitive, so it is best to upcase everything so that you get a match. Just looking for an exact match.  In your example policeman would not be a match for police.  Trying to keep things simple for the time being.  I will settle for the first work matching to be the only result.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Tue, 16 Apr 2019 22:46:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Appending-a-column-from-one-table-to-another/m-p/551579#M153264</guid>
      <dc:creator>pangea17</dc:creator>
      <dc:date>2019-04-16T22:46:18Z</dc:date>
    </item>
    <item>
      <title>Re: Appending a column from one table to another</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Appending-a-column-from-one-table-to-another/m-p/551583#M153265</link>
      <description>&lt;P&gt;Based on this code you posted :&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  set dataset2 end=last;
  if _n_ = 1 then call execute('data dataset3; set dataset1; ');
  call execute(cats('if findw(upcase(information),"',upcase(word),'") &amp;gt; 0 then output;'));
  if last then call execute('run;');
RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You are running a data step like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data dataset3;
   set dataset1; 
   if findw(upcase(information),"NURSE") &amp;gt; 0 then output;
   if findw(upcase(information),"DOCTOR") &amp;gt; 0 then output;
   if findw(upcase(information),"POLICE") &amp;gt; 0 then output;
   ...
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Why not just join the two datasets directly?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table dataset3 as
  select a.information,b.word
  from dataset1 a
  right join dataset2 b
  on findw(upcase(a.information),upcase(strip(b.word)))
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or reduce the time and space and just store the counts.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table dataset3 as
  select b.word,count(*) as n_matches
  from dataset1 a
  right join dataset2 b
  on findw(upcase(a.information),upcase(strip(b.word)))
  group by 1
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 16 Apr 2019 23:09:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Appending-a-column-from-one-table-to-another/m-p/551583#M153265</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-04-16T23:09:29Z</dc:date>
    </item>
    <item>
      <title>Re: Appending a column from one table to another</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Appending-a-column-from-one-table-to-another/m-p/551809#M153359</link>
      <description>&lt;P&gt;That was perfect Tom. Thanks!&lt;/P&gt;</description>
      <pubDate>Wed, 17 Apr 2019 17:13:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Appending-a-column-from-one-table-to-another/m-p/551809#M153359</guid>
      <dc:creator>pangea17</dc:creator>
      <dc:date>2019-04-17T17:13:04Z</dc:date>
    </item>
  </channel>
</rss>

