<?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: Merge or concatenate the table in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Merge-or-concatenate-the-table/m-p/904822#M357459</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/416124"&gt;@Gick&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;In fact when there are more than 10 tables, the code does not take into account non-empty ones.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Assuming the variables that make up the business key are {modalite, variable} - or {mod,var} in your sample data - then I guess in your 10 table case there was no combination of values for {modalit, variable} that existed in all tables (except for when modalite is missings). Because the sample code I've posted uses an INNER JOIN (similar logic for the data steps) only rows where the key values exist in all source tables will get selected.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Questions you need to answer:&lt;/P&gt;
&lt;P&gt;1. Is {modalite, var} the business key in all your source tables meaning each value combination exists only once within a table?&lt;/P&gt;
&lt;P&gt;2. What should happen if a key combination doesn't exist in all tables? Should the contributing variables from this table (not the key ones) just become missing?&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; - means that you would never drop a row from any table to create the result table.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 28 Nov 2023 12:50:24 GMT</pubDate>
    <dc:creator>Patrick</dc:creator>
    <dc:date>2023-11-28T12:50:24Z</dc:date>
    <item>
      <title>Merge or concatenate the table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-or-concatenate-the-table/m-p/904780#M357440</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="fusion_Matab.PNG" style="width: 472px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/90283i9F9F2EC7EE06A536/image-size/large?v=v2&amp;amp;px=999" role="button" title="fusion_Matab.PNG" alt="fusion_Matab.PNG" /&gt;&lt;/span&gt;&lt;BR /&gt;data T1;
   input mod $ var $ moy1 ;
   datalines;
   . AA 23 
   as AA 45
   sc AA 68
   . CC  0
   ic CC 1
   vi CC 35
   cm CC 235
   . DD 4
   fi DD 36
   dh DD 35
   sg DD 10
;
run; 

data T2;
   input mod $ var $ moy2;
   datalines;
   . AA 0
   as AA 10
   sc AA 23
   . CC 10
   ic CC 12
   vi CC 23
   cm CC 9
   . DD 3.3
   fi DD 21
   dh DD 16
   sg DD 13
;
run; 

data T3;
   input mod $ var $ moy3 ;
   datalines;
   . AA 12
   as AA 4
   sc AA 0
   . CC 11
   ic CC 35
   vi CC 6
   cm CC 8
   . DD 12
   fi DD 4
   dh DD 14
   sg DD 15
;
run; 



/* get the Matab table */
data MaTab;
   input mod $ var $ moy1 moy2 moy3 ;
   datalines;
   . AA 23 0 12
   as AA 45 10 4
   sc AA 68 23 0
   . CC  0 10 11
   ic CC 1 12 35
   vi CC 35 23 6
   cm CC 235 9 8
   . DD 4 3.3 12
   fi DD 36 21 4
   dh DD 35 16 14
   sg DD 10 13 15
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;I have several sas tables (T1,T2,T3,...,Tn). I am providing you with an extract (T1, T2 and T3). I want to create a code which allows me to have the Matab table whose structure is as follows: (see the table).&lt;/P&gt;
&lt;P&gt;I tried to get it by concatenating or merging (merge with data step or sql with proc sql) without success.&amp;nbsp;The result in screenshot is not good and this is what I get even though I want to have the Matab table.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can anyone suggest me a code or an approach that can get the Matab table please.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Best regards,&lt;/P&gt;
&lt;P&gt;Gick&lt;/P&gt;</description>
      <pubDate>Tue, 28 Nov 2023 05:02:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-or-concatenate-the-table/m-p/904780#M357440</guid>
      <dc:creator>Gick</dc:creator>
      <dc:date>2023-11-28T05:02:17Z</dc:date>
    </item>
    <item>
      <title>Re: Merge or concatenate the table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-or-concatenate-the-table/m-p/904785#M357441</link>
      <description>&lt;P&gt;Below code works for your sample data.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For your real data: Do you want a result set with observations that have matching keys {mod,var} in all tables (=inner join) or something else.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  create table want as
  select 
    t1.*,
    t2.moy2,
    t3.moy3
  from t1 

  inner join t2
    on t1.mod=t2.mod and t1.var=t2.var

  inner join t3
    on t1.mod=t3.mod and t1.var=t3.var

  order by mod, var;
  ;
quit;

proc sort data=MaTab;
  by mod var;
run;

proc compare base=MaTab comp=want;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1701148718676.png" style="width: 619px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/90285i72A77E39DE4F7273/image-dimensions/619x271?v=v2" width="619" height="271" role="button" title="Patrick_0-1701148718676.png" alt="Patrick_0-1701148718676.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or just for fun here two alternative data step options&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  if _n_=1 then
    do;
      if 0 then set t2(keep=moy2) t3(keep=moy3);
      dcl hash h2(dataset:'t2');
      h2.defineKey('mod','var');
      h2.defineData('moy2');
      h2.defineDone();
      dcl hash h3(dataset:'t3');
      h3.defineKey('mod','var');
      h3.defineData('moy3');
      h3.defineDone();
    end;
    call missing(of _all_);
    set t3;
    if h2.find()=0 and h3.find()=0;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=t1 out=t_sorted1;
  by mod var;
run;
proc sort data=t2 out=t_sorted2;
  by mod var;
run;
proc sort data=t3 out=t_sorted3;
  by mod var;
run;

data want;
  merge t_sorted1(in=t1) t_sorted2(in=t2) t_sorted3(in=t3);
  by mod var;
  if t1 and t2 and t3;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 28 Nov 2023 05:32:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-or-concatenate-the-table/m-p/904785#M357441</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2023-11-28T05:32:33Z</dc:date>
    </item>
    <item>
      <title>Re: Merge or concatenate the table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-or-concatenate-the-table/m-p/904792#M357445</link>
      <description>&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="fusion_Matab.PNG" style="width: 489px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/90286i755B6F2D1BE473DF/image-size/large?v=v2&amp;amp;px=999" role="button" title="fusion_Matab.PNG" alt="fusion_Matab.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 28 Nov 2023 06:53:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-or-concatenate-the-table/m-p/904792#M357445</guid>
      <dc:creator>Gick</dc:creator>
      <dc:date>2023-11-28T06:53:40Z</dc:date>
    </item>
    <item>
      <title>Re: Merge or concatenate the table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-or-concatenate-the-table/m-p/904793#M357446</link>
      <description>It works for this extracted data. But, when I apply it in my tables, the Mod mode is empty. The mods entered are not displayed and I don't know why?&lt;BR /&gt;&lt;BR /&gt;I'll take a screenshot for you.&lt;BR /&gt;&lt;BR /&gt;THANKS.</description>
      <pubDate>Tue, 28 Nov 2023 06:53:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-or-concatenate-the-table/m-p/904793#M357446</guid>
      <dc:creator>Gick</dc:creator>
      <dc:date>2023-11-28T06:53:56Z</dc:date>
    </item>
    <item>
      <title>Re: Merge or concatenate the table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-or-concatenate-the-table/m-p/904807#M357454</link>
      <description>In fact when there are more than 10 tables, the code does not take into account non-empty ones.</description>
      <pubDate>Tue, 28 Nov 2023 10:08:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-or-concatenate-the-table/m-p/904807#M357454</guid>
      <dc:creator>Gick</dc:creator>
      <dc:date>2023-11-28T10:08:55Z</dc:date>
    </item>
    <item>
      <title>Re: Merge or concatenate the table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-or-concatenate-the-table/m-p/904811#M357456</link>
      <description>&lt;P&gt;How does this screenshot relate to the example data you posted?&lt;/P&gt;
&lt;P&gt;What are the key variables, and what does COUNT mean?&lt;/P&gt;
&lt;P&gt;Keep in mind that missing values (like the blanks in modalite) will always appear first. You should consider changing the order of variables in the BY statements.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 28 Nov 2023 11:00:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-or-concatenate-the-table/m-p/904811#M357456</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-11-28T11:00:10Z</dc:date>
    </item>
    <item>
      <title>Re: Merge or concatenate the table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-or-concatenate-the-table/m-p/904822#M357459</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/416124"&gt;@Gick&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;In fact when there are more than 10 tables, the code does not take into account non-empty ones.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Assuming the variables that make up the business key are {modalite, variable} - or {mod,var} in your sample data - then I guess in your 10 table case there was no combination of values for {modalit, variable} that existed in all tables (except for when modalite is missings). Because the sample code I've posted uses an INNER JOIN (similar logic for the data steps) only rows where the key values exist in all source tables will get selected.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Questions you need to answer:&lt;/P&gt;
&lt;P&gt;1. Is {modalite, var} the business key in all your source tables meaning each value combination exists only once within a table?&lt;/P&gt;
&lt;P&gt;2. What should happen if a key combination doesn't exist in all tables? Should the contributing variables from this table (not the key ones) just become missing?&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; - means that you would never drop a row from any table to create the result table.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 28 Nov 2023 12:50:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-or-concatenate-the-table/m-p/904822#M357459</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2023-11-28T12:50:24Z</dc:date>
    </item>
    <item>
      <title>Re: Merge or concatenate the table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-or-concatenate-the-table/m-p/904826#M357460</link>
      <description>It's good. Just use "left join" instead of "Inner join".&lt;BR /&gt;&lt;BR /&gt;Thank you so much</description>
      <pubDate>Tue, 28 Nov 2023 12:57:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-or-concatenate-the-table/m-p/904826#M357460</guid>
      <dc:creator>Gick</dc:creator>
      <dc:date>2023-11-28T12:57:51Z</dc:date>
    </item>
    <item>
      <title>Re: Merge or concatenate the table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-or-concatenate-the-table/m-p/904831#M357461</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/416124"&gt;@Gick&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;It's good. Just use "left join" instead of "Inner join".&lt;BR /&gt;&lt;BR /&gt;Thank you so much&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;ONLY if your first table is the master and you don't need any rows from other tables that don't have a match to this first table.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here the documentation for the joins you likely need:&amp;nbsp;&lt;A href="https://go.documentation.sas.com/doc/en/pgmsascdc/9.4_3.3/casfedsql/n0gb1qlach920vn1tweau4cns8k4.htm" target="_self"&gt;Example of a Full Outer Qualified Join&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 28 Nov 2023 14:01:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-or-concatenate-the-table/m-p/904831#M357461</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2023-11-28T14:01:33Z</dc:date>
    </item>
    <item>
      <title>Re: Merge or concatenate the table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-or-concatenate-the-table/m-p/905089#M357503</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/416124"&gt;@Gick&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;In fact when there are more than 10 tables, the code does not take into account non-empty ones.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/416124"&gt;@Gick&lt;/a&gt;&amp;nbsp;That's not the solution and what you observe has NOTHING to do with the number of tables.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 29 Nov 2023 06:03:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-or-concatenate-the-table/m-p/905089#M357503</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2023-11-29T06:03:39Z</dc:date>
    </item>
  </channel>
</rss>

