<?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 Comparing IDs across tables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Comparing-IDs-across-tables/m-p/169629#M32534</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have two datasets. One has many variables, one of which is a unique ID variable (1 row=1 unique person, no dupes). The second dataset is just a list of IDs. What I want to do is create a variable in Dataset 1 for whether a person's ID also appears in Dataset 2. I know how to do this using joins, but I'm hoping there's a simpler way that I don't know about or haven't thought of.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Any help is much appreciated.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 13 Nov 2014 15:43:22 GMT</pubDate>
    <dc:creator>Walternate</dc:creator>
    <dc:date>2014-11-13T15:43:22Z</dc:date>
    <item>
      <title>Comparing IDs across tables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Comparing-IDs-across-tables/m-p/169629#M32534</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have two datasets. One has many variables, one of which is a unique ID variable (1 row=1 unique person, no dupes). The second dataset is just a list of IDs. What I want to do is create a variable in Dataset 1 for whether a person's ID also appears in Dataset 2. I know how to do this using joins, but I'm hoping there's a simpler way that I don't know about or haven't thought of.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Any help is much appreciated.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 13 Nov 2014 15:43:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Comparing-IDs-across-tables/m-p/169629#M32534</guid>
      <dc:creator>Walternate</dc:creator>
      <dc:date>2014-11-13T15:43:22Z</dc:date>
    </item>
    <item>
      <title>Re: Comparing IDs across tables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Comparing-IDs-across-tables/m-p/169630#M32535</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;/* Note assumes you have a variable FLAG in your original dataset to hold the update */&lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; update table HAVE&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; set&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; FLAG="Y"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; where ID in (select distinct ID from DATASET2) ;&lt;/P&gt;&lt;P&gt;quit;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;You could also do it by a normal select:&lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; create table WANT as&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; select&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; A.*,&lt;/P&gt;&lt;P&gt;&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; case when exists(select THIS.ID from DATASET2 THIS where THIS.ID=A.ID) then "Y"&lt;/P&gt;&lt;P&gt;&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else "N" end as FLAG&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; from&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; DATASET1;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 13 Nov 2014 15:49:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Comparing-IDs-across-tables/m-p/169630#M32535</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2014-11-13T15:49:25Z</dc:date>
    </item>
    <item>
      <title>Re: Comparing IDs across tables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Comparing-IDs-across-tables/m-p/169631#M32536</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;A data step solution:&lt;/P&gt;&lt;P&gt;data A;&lt;/P&gt;&lt;P&gt;input ID X1 X2;&lt;/P&gt;&lt;P&gt;cards;&lt;/P&gt;&lt;P&gt;100&amp;nbsp; 10 20&lt;/P&gt;&lt;P&gt;200&amp;nbsp; 20 30&lt;/P&gt;&lt;P&gt;500&amp;nbsp; 50 70&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data B;&lt;/P&gt;&lt;P&gt;input ID;&lt;/P&gt;&lt;P&gt;cards;&lt;/P&gt;&lt;P&gt;100&lt;/P&gt;&lt;P&gt;150&lt;/P&gt;&lt;P&gt;200&lt;/P&gt;&lt;P&gt;100&lt;/P&gt;&lt;P&gt;200&lt;/P&gt;&lt;P&gt;300&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data want;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; if 0 then set B;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; declare hash h(dataset:'B');&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; h.definekey('ID');&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; h.definedone();&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; do until(last);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; set A end = last;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; flag = 'N';&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if h.find() = 0 then flag = 'Y'; &lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; output; &lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; end;&lt;/P&gt;&lt;P&gt;stop;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 13 Nov 2014 17:33:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Comparing-IDs-across-tables/m-p/169631#M32536</guid>
      <dc:creator>KachiM</dc:creator>
      <dc:date>2014-11-13T17:33:37Z</dc:date>
    </item>
    <item>
      <title>Re: Comparing IDs across tables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Comparing-IDs-across-tables/m-p/169632#M32537</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I think there might be a very &lt;SPAN style="text-decoration: underline;"&gt;simple&lt;/SPAN&gt; solution to this using the built in features of SAS. There is a temporary variable created when you merge data sets called 'in', which keeps track of where a data record comes from. So, you could do something like this:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;*sort the two data files;&lt;/P&gt;&lt;P&gt;proc sort data = work.first_file; by id; run;&lt;/P&gt;&lt;P&gt;proc sort data = work.second_file; by id; run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;*Merge the two files, keeping only the records in the first file, but create a variable that indicates if the ID was in the second file;&lt;/P&gt;&lt;P&gt;Data all;&lt;/P&gt;&lt;P&gt; merge work.first_file (in = A) &lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; work.second_file (in = B);&lt;/P&gt;&lt;P&gt; *create the variable;&lt;/P&gt;&lt;P&gt;&amp;nbsp; if B = 1 then in_second_file = 1;&lt;/P&gt;&lt;P&gt;*output only the original records;&lt;/P&gt;&lt;P&gt; if A = 1 then output;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Good luck!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 13 Nov 2014 21:54:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Comparing-IDs-across-tables/m-p/169632#M32537</guid>
      <dc:creator>sgnolek</dc:creator>
      <dc:date>2014-11-13T21:54:32Z</dc:date>
    </item>
  </channel>
</rss>

