<?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 Creating subset of data by matching multiple variables to second data set using in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Creating-subset-of-data-by-matching-multiple-variables-to-second/m-p/333205#M75042</link>
    <description>&lt;P&gt;I have data A and B:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data A;
input ID  visit  accessor_ID SPID  score1 score2 score3 ;
datalines;
1 1 1 1 79 70 80
1 1 2 1 89 81 86
1 2 1 1 74 79 82
1 2 2 1 69 75 81
;
run;

data B;
input ID  visit  accessor_ID SPID  score1 score2 score3 ;
datalines;
1 1 1 1 73 75 70
1 1 1 2 81 82 90
1 1 2 1 89 81 86
1 1 2 2 79 84 88
1 2 1 1 74 79 82
1 2 1 2 86 76 77
1 2 2 1 69 75 81
1 2 2 2 78 75 83
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I want to subset data B, only keep obs which have the matching first four varibles in set A:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* want to subset data B, to only keep those with matching (ID visit accessor_ID spid), i.e.
1 1 1 1 73 75 70
1 1 2 1 89 81 86
1 2 1 1 74 79 82
1 2 2 1 69 75 81 
*/

proc sort data=A out=unique_set (keep=ID visit accessor_ID spid); by ID visit accessor_ID spid; run;
proc sort data=B; by ID visit accessor_ID spid; run;
data B_subset;
merge B (in=a) unique_set (in=b);
by ID visit accessor_ID spid; 
if A and B;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The above code works, but I wonder if there are other methods (like using proc sql etc). Thanks.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 15 Feb 2017 22:30:12 GMT</pubDate>
    <dc:creator>fengyuwuzu</dc:creator>
    <dc:date>2017-02-15T22:30:12Z</dc:date>
    <item>
      <title>Creating subset of data by matching multiple variables to second data set using</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-subset-of-data-by-matching-multiple-variables-to-second/m-p/333205#M75042</link>
      <description>&lt;P&gt;I have data A and B:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data A;
input ID  visit  accessor_ID SPID  score1 score2 score3 ;
datalines;
1 1 1 1 79 70 80
1 1 2 1 89 81 86
1 2 1 1 74 79 82
1 2 2 1 69 75 81
;
run;

data B;
input ID  visit  accessor_ID SPID  score1 score2 score3 ;
datalines;
1 1 1 1 73 75 70
1 1 1 2 81 82 90
1 1 2 1 89 81 86
1 1 2 2 79 84 88
1 2 1 1 74 79 82
1 2 1 2 86 76 77
1 2 2 1 69 75 81
1 2 2 2 78 75 83
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I want to subset data B, only keep obs which have the matching first four varibles in set A:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* want to subset data B, to only keep those with matching (ID visit accessor_ID spid), i.e.
1 1 1 1 73 75 70
1 1 2 1 89 81 86
1 2 1 1 74 79 82
1 2 2 1 69 75 81 
*/

proc sort data=A out=unique_set (keep=ID visit accessor_ID spid); by ID visit accessor_ID spid; run;
proc sort data=B; by ID visit accessor_ID spid; run;
data B_subset;
merge B (in=a) unique_set (in=b);
by ID visit accessor_ID spid; 
if A and B;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The above code works, but I wonder if there are other methods (like using proc sql etc). Thanks.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 15 Feb 2017 22:30:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-subset-of-data-by-matching-multiple-variables-to-second/m-p/333205#M75042</guid>
      <dc:creator>fengyuwuzu</dc:creator>
      <dc:date>2017-02-15T22:30:12Z</dc:date>
    </item>
    <item>
      <title>Re: Creating subset of data by matching multiple variables to second data set using</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-subset-of-data-by-matching-multiple-variables-to-second/m-p/333215#M75045</link>
      <description>&lt;P&gt;Matching multiple variables:&amp;nbsp; hash is probably the most robust method.&amp;nbsp; No sorting required.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want (drop=rc);&lt;/P&gt;
&lt;P&gt;&amp;nbsp; if _n_=1then do;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if 0 then set a (keep=&lt;SPAN class="token keyword"&gt;ID&lt;/SPAN&gt;&amp;nbsp; visit&amp;nbsp; accessor_ID SPID&amp;nbsp;&amp;nbsp;);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; declare hash lookupa (dataset:'A (keep=id visit accessor_id spid)');&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;lookupa.definekey(all:'Y');&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;lookupa.definedone():&lt;/P&gt;
&lt;P&gt;&amp;nbsp; end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; set b;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; rc=lookupa.find();&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;if rc=0;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;</description>
      <pubDate>Wed, 15 Feb 2017 23:05:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-subset-of-data-by-matching-multiple-variables-to-second/m-p/333215#M75045</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2017-02-15T23:05:31Z</dc:date>
    </item>
    <item>
      <title>Re: Creating subset of data by matching multiple variables to second data set using</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-subset-of-data-by-matching-multiple-variables-to-second/m-p/333217#M75047</link>
      <description>&lt;P&gt;One way that MAY work if your two comparison sets do not have as many matches for variables ID visit accessor_ID SPID score1 would be:&lt;/P&gt;
&lt;PRE&gt;proc sql ;
   create table b_subset as
   select b.*
   from b natural join a;
quit; &lt;/PRE&gt;
&lt;P&gt;Natural join actually looks at your data and guesses how to link records. In your example data case you have many matches including the SCORE1 value so that gets used and only returns the 3 records that match on 5 varibles. No sort is needed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Another option would be to specify the variables that must match:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;proc sql ;
   create table b_subset as
   select b.*
   from a  join b on
     a.id=b.id and a.visit=b.visit and a.accessor_ID=b.accessor_ID and a.spid=b.spid;
quit; &lt;/PRE&gt;</description>
      <pubDate>Wed, 15 Feb 2017 23:10:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-subset-of-data-by-matching-multiple-variables-to-second/m-p/333217#M75047</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-02-15T23:10:53Z</dc:date>
    </item>
  </channel>
</rss>

