<?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: Joining two tables with a variable that has non-distinct values in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Joining-two-tables-with-a-variable-that-has-non-distinct-values/m-p/843589#M36613</link>
    <description>&lt;P&gt;I could swear I tried that...but apparently not as this works!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you so much!&lt;/P&gt;</description>
    <pubDate>Thu, 10 Nov 2022 14:20:05 GMT</pubDate>
    <dc:creator>sayno2s</dc:creator>
    <dc:date>2022-11-10T14:20:05Z</dc:date>
    <item>
      <title>Joining two tables with a variable that has non-distinct values</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Joining-two-tables-with-a-variable-that-has-non-distinct-values/m-p/843511#M36602</link>
      <description>&lt;P&gt;Hi guys,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to join two tables but I'm having problems with the "lack of detail" in one variable which results in either too many or too few records in the output.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The issue is "VISIT" in the first data set. There are four "U" visits in "Prot1" and two in "Prot2". Joining the tables, I'm getting, e.g., six or two "U" visits in each protocol, depending on the code version (see below).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've been trying things with in/excluding variables, different types of joins, with/out "distinct", "Group By", pulling out my hair, but nothing works.&lt;BR /&gt;&lt;BR /&gt;I'd be very grateful for your help and thank you in advance!&lt;BR /&gt;&lt;BR /&gt;This is how the two tables and the desired output look like:&lt;BR /&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Q-Tables.PNG" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/77145i06164F015E655AC8/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Q-Tables.PNG" alt="Q-Tables.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Here's the code example:&lt;/P&gt;&lt;PRE&gt;/* This is just an example for one "Num"; there are several hundreds in the tables */&lt;BR /&gt;data dset1;
input Num VISIT $ Type $ Prot $ Result $;
datalines;
100 M4 Type2 Prot1 Outcome1
100 M4 Type1 Prot1 Outcome2
100 W12 Type2 Prot2 Outcome5
100 W12 Type1 Prot2 Outcome5
100 W6 Type2 Prot2 Outcome6
100 W6 Type1 Prot2 Outcome5
100 U Type2 Prot1 Outcome3
100 U Type1 Prot1 Outcome3
100 U Type2 Prot1 Outcome3
100 U Type1 Prot1 Outcome3
100 U Type2 Prot2 Outcome5
100 U Type1 Prot2 Outcome4
;
run

data dset2;
input Num VisitName $ Type $ Reader $ OrigVisitName $ InternalID $;
datalines;
100 M4 Type1 PersonA Month4 ID1
100 M4 Type2 PersonB Month4 ID1
100 W12 Type1 PersonA Week12 ID2
100 W12 Type2 PersonB Week12 ID2
100 W6 Type1 PersonA Week6 ID3
100 W6 Type2 PersonB Week6 ID3
100 U Type1 PersonA U1 ID4
100 U Type1 PersonA U2 ID6
100 U Type1 PersonA U3 ID5
100 U Type2 PersonB U1 ID4
100 U Type2 PersonB U2 ID6
100 U Type2 PersonB U3 ID5
;
run

proc sql;
   create table work.WANT as
   select distinct t1.*, 
     t2.VisitName,  t2.Reader /*, t2.OrigVisitName, t2.InternalID */
      from work.dset1 t1
           full join work.dset2 t2 on (t1.Num = t2.Num) and (t1.VISIT = t2.VisitName) and (t1.Type = t2.Type);
quit;&lt;/PRE&gt;</description>
      <pubDate>Thu, 10 Nov 2022 03:06:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Joining-two-tables-with-a-variable-that-has-non-distinct-values/m-p/843511#M36602</guid>
      <dc:creator>sayno2s</dc:creator>
      <dc:date>2022-11-10T03:06:58Z</dc:date>
    </item>
    <item>
      <title>Re: Joining two tables with a variable that has non-distinct values</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Joining-two-tables-with-a-variable-that-has-non-distinct-values/m-p/843517#M36603</link>
      <description>&lt;P&gt;Rules are helpful.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your example data steps do not look like the pictures, the order is different. So without some pretty explicit rules I do not quite know what you are actually expecting.&lt;/P&gt;</description>
      <pubDate>Thu, 10 Nov 2022 04:58:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Joining-two-tables-with-a-variable-that-has-non-distinct-values/m-p/843517#M36603</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-11-10T04:58:21Z</dc:date>
    </item>
    <item>
      <title>Re: Joining two tables with a variable that has non-distinct values</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Joining-two-tables-with-a-variable-that-has-non-distinct-values/m-p/843526#M36604</link>
      <description>&lt;P&gt;Thank you for taking a look!&lt;BR /&gt;&lt;BR /&gt;Sorry, I had sorted the excel data somewhere in the meantime. The datalines below are now corresponding to the screenshot.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I hope this next screenshot also helps. The issue is that the "U" visits in dset1 are not distinct (this information is available in "OrigVisitName" in dset2). Now I want join the tables but I can't get it to work. It's either too few U visits because of incorporating fewer variables (Wrong Table 2) or there are too many because everyone matches with everyone (Wrong Table 2).&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Q-Tables2.PNG" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/77152i632568BA641C7D67/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Q-Tables2.PNG" alt="Q-Tables2.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data dset1;
input Num VISIT $ Type $ Prot $ Result $;
datalines;
Num VISIT Type Prot Result
100 M4 Type1 Prot1 Outcome2
100 M4 Type2 Prot1 Outcome1
100 U Type1 Prot1 Outcome3
100 U Type1 Prot1 Outcome3
100 U Type1 Prot2 Outcome4
100 U Type2 Prot1 Outcome3
100 U Type2 Prot1 Outcome3
100 U Type2 Prot2 Outcome5
100 W12 Type1 Prot2 Outcome5
100 W12 Type2 Prot2 Outcome5
100 W6 Type1 Prot2 Outcome5
100 W6 Type2 Prot2 Outcome6
;
run

data dset2;
input Num VisitName $ Type $ Reader $ OrigVisitName $ InternalID $;
datalines;
100 M4 Type1 PersonA Month4 ID1
100 M4 Type2 PersonB Month4 ID1
100 U Type1 PersonA U1 ID4
100 U Type1 PersonA U2 ID6
100 U Type1 PersonA U3 ID5
100 U Type2 PersonB U1 ID4
100 U Type2 PersonB U2 ID6
100 U Type2 PersonB U3 ID5
100 W12 Type1 PersonA Week12 ID2
100 W12 Type2 PersonB Week12 ID2
100 W6 Type1 PersonA Week6 ID3
100 W6 Type2 PersonB Week6 ID3
;
run &lt;/PRE&gt;</description>
      <pubDate>Thu, 10 Nov 2022 05:39:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Joining-two-tables-with-a-variable-that-has-non-distinct-values/m-p/843526#M36604</guid>
      <dc:creator>sayno2s</dc:creator>
      <dc:date>2022-11-10T05:39:11Z</dc:date>
    </item>
    <item>
      <title>Re: Joining two tables with a variable that has non-distinct values</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Joining-two-tables-with-a-variable-that-has-non-distinct-values/m-p/843531#M36605</link>
      <description>&lt;P&gt;Try next code. My result may be in different order then yours.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data dset1;
input Num VISIT $ Type $ Prot $ Result $;
datalines;
100 M4 Type2 Prot1 Outcome1
100 M4 Type1 Prot1 Outcome2
100 W12 Type2 Prot2 Outcome5
100 W12 Type1 Prot2 Outcome5
100 W6 Type2 Prot2 Outcome6
100 W6 Type1 Prot2 Outcome5
100 U Type2 Prot1 Outcome3
100 U Type1 Prot1 Outcome3
100 U Type2 Prot1 Outcome3
100 U Type1 Prot1 Outcome3
100 U Type2 Prot2 Outcome5
100 U Type1 Prot2 Outcome4
;
run;

data dset2;
input Num VisitName $ Type $ Reader $ OrigVisitName $ InternalID $;
datalines;
100 M4 Type1 PersonA Month4 ID1
100 M4 Type2 PersonB Month4 ID1
100 W12 Type1 PersonA Week12 ID2
100 W12 Type2 PersonB Week12 ID2
100 W6 Type1 PersonA Week6 ID3
100 W6 Type2 PersonB Week6 ID3
100 U Type1 PersonA U1 ID4
100 U Type1 PersonA U2 ID6
100 U Type1 PersonA U3 ID5
100 U Type2 PersonB U1 ID4
100 U Type2 PersonB U2 ID6
100 U Type2 PersonB U3 ID5
;
run;

proc sort data=dset1; by num visit type; run;
proc sort data=dset2; by num visitname type; run;

data temp;
 merge dset1 
       dset2(rename=(visitname=visit));
  by num visit type;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 10 Nov 2022 05:54:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Joining-two-tables-with-a-variable-that-has-non-distinct-values/m-p/843531#M36605</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2022-11-10T05:54:03Z</dc:date>
    </item>
    <item>
      <title>Re: Joining two tables with a variable that has non-distinct values</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Joining-two-tables-with-a-variable-that-has-non-distinct-values/m-p/843589#M36613</link>
      <description>&lt;P&gt;I could swear I tried that...but apparently not as this works!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you so much!&lt;/P&gt;</description>
      <pubDate>Thu, 10 Nov 2022 14:20:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Joining-two-tables-with-a-variable-that-has-non-distinct-values/m-p/843589#M36613</guid>
      <dc:creator>sayno2s</dc:creator>
      <dc:date>2022-11-10T14:20:05Z</dc:date>
    </item>
  </channel>
</rss>

