<?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: Merging and keeping only common variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Merging-and-keeping-only-common-variables/m-p/519259#M140618</link>
    <description>&lt;P&gt;Are you looking to reorder the Variables in the final dataset ? If that is the only case then there are better options than merging.&amp;nbsp; Merging the two data sets does not make any sense in your example.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input B A;
cards;
4 1
5 2
6 3
;
run;

data want;
retain a b;
set have;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 06 Dec 2018 20:03:43 GMT</pubDate>
    <dc:creator>r_behata</dc:creator>
    <dc:date>2018-12-06T20:03:43Z</dc:date>
    <item>
      <title>Merging and keeping only common variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-and-keeping-only-common-variables/m-p/519254#M140613</link>
      <description>&lt;P&gt;Hi&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am looking to merge datasets together however I only want to keep the common variables between the 2 datasets. Is there a way to do this without manually writing out all the variables to be dropped? For example (below) I am trying to merge Dataset 1 and Dataset 2 to make Dataset 3. I am aware that Dataset 2's data will overwrite Dataset 1 and I am ok with this because Dataset 1 is acting like a template.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Dataset 1&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;A&amp;nbsp;&amp;nbsp;B&amp;nbsp; C&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;0&amp;nbsp; &amp;nbsp;0&amp;nbsp;&amp;nbsp; 0&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Dataset 2&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&amp;nbsp;B&amp;nbsp; A&lt;/STRONG&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;4&amp;nbsp;&amp;nbsp; 1&lt;/P&gt;&lt;P&gt;&amp;nbsp;5&amp;nbsp;&amp;nbsp; 2&lt;/P&gt;&lt;P&gt;&amp;nbsp;6&amp;nbsp;&amp;nbsp; 3&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Dataset 3&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&amp;nbsp;A&amp;nbsp; B&amp;nbsp;&amp;nbsp;&lt;/STRONG&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;1&amp;nbsp;&amp;nbsp; 4&lt;/P&gt;&lt;P&gt;&amp;nbsp;2&amp;nbsp;&amp;nbsp; 5&lt;/P&gt;&lt;P&gt;&amp;nbsp;3&amp;nbsp;&amp;nbsp; 6&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 06 Dec 2018 19:57:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-and-keeping-only-common-variables/m-p/519254#M140613</guid>
      <dc:creator>arielcslin</dc:creator>
      <dc:date>2018-12-06T19:57:17Z</dc:date>
    </item>
    <item>
      <title>Re: Merging and keeping only common variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-and-keeping-only-common-variables/m-p/519259#M140618</link>
      <description>&lt;P&gt;Are you looking to reorder the Variables in the final dataset ? If that is the only case then there are better options than merging.&amp;nbsp; Merging the two data sets does not make any sense in your example.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input B A;
cards;
4 1
5 2
6 3
;
run;

data want;
retain a b;
set have;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 06 Dec 2018 20:03:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-and-keeping-only-common-variables/m-p/519259#M140618</guid>
      <dc:creator>r_behata</dc:creator>
      <dc:date>2018-12-06T20:03:43Z</dc:date>
    </item>
    <item>
      <title>Re: Merging and keeping only common variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-and-keeping-only-common-variables/m-p/519262#M140621</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
 data one;
 input A  B  C;
 cards;
0   0   0
;

data two;
input B  A ;
cards;
4   1
5   2
6   3
;

proc sql;
create table want as
select * from one(obs=0)
union corr
select * from two;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 06 Dec 2018 20:06:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-and-keeping-only-common-variables/m-p/519262#M140621</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-12-06T20:06:39Z</dc:date>
    </item>
    <item>
      <title>Re: Merging and keeping only common variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-and-keeping-only-common-variables/m-p/519264#M140623</link>
      <description>&lt;P&gt;Yes the point is to order Dataset 2 however Dataset 2 may not have all the variables listed in the template(?) So I would like to put Dataset 2 in order but drop all variables that are not populated with any data&lt;/P&gt;</description>
      <pubDate>Thu, 06 Dec 2018 20:08:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-and-keeping-only-common-variables/m-p/519264#M140623</guid>
      <dc:creator>arielcslin</dc:creator>
      <dc:date>2018-12-06T20:08:18Z</dc:date>
    </item>
    <item>
      <title>Re: Merging and keeping only common variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-and-keeping-only-common-variables/m-p/519266#M140625</link>
      <description>&lt;P&gt;safer with&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;union corr all&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;

 data one;
 input A  B  C;
 cards;
0   0   0
;

data two;
input B  A ;
cards;
4   1
5   2
6   3
;

proc sql;
create table want as
select * from one(obs=0)
union corr all
select * from two;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 06 Dec 2018 20:10:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-and-keeping-only-common-variables/m-p/519266#M140625</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-12-06T20:10:50Z</dc:date>
    </item>
    <item>
      <title>Re: Merging and keeping only common variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-and-keeping-only-common-variables/m-p/519269#M140627</link>
      <description>Can you give a better example that reflect the scenario more accurately.</description>
      <pubDate>Thu, 06 Dec 2018 20:16:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-and-keeping-only-common-variables/m-p/519269#M140627</guid>
      <dc:creator>r_behata</dc:creator>
      <dc:date>2018-12-06T20:16:26Z</dc:date>
    </item>
    <item>
      <title>Re: Merging and keeping only common variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-and-keeping-only-common-variables/m-p/519307#M140649</link>
      <description>&lt;P&gt;If you know that the vars in one of the datasets constitute a proper subset of vars in the other, then you just need to drop all vars not in that "narrow" dataset.&amp;nbsp; In this case, you want to drop all vars not in datset2.&amp;nbsp; But you want to merge such that dataset2 VALUES prevail:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Dataset1;
  input A  B  C;
datalines;
0   0   0
run;

data Dataset2;
  input  B  A ;
datalines;
 4   1
 5   2
 6   3
run;

data dataset3;
  if 0 then set dataset2;      /*Start the PDV with the dataset2 vars*/
  retain _sentinel_between .;  /*Append this var to the PDV */
  merge dataset1 dataset2;     /*Append any vars exclusively in dataset1 to PDV*/
  retain _sentinel_after .;    /*Append a final var to the PDV*/
  drop _sentinel_between -- _sentinel_after;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note the PDV (program data vector) is the set of variables that the sas compiler establishes by parsing your sas code, prior to processing any data.&amp;nbsp; At each statement above the PDV is expanded when any new variables are encountered.&amp;nbsp; So by putting in _sentinel_between and _sentinel_after variables in strategic locations in the PDV, it becomes easy to specify a list of variables to drop.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The double dash in &lt;EM&gt;&lt;STRONG&gt;drop _sentinel_between -- _sentinel_after;&lt;/STRONG&gt;&lt;/EM&gt; tells sas to include all vars between the two sentinels, including the end points.&lt;/P&gt;</description>
      <pubDate>Thu, 06 Dec 2018 22:59:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-and-keeping-only-common-variables/m-p/519307#M140649</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2018-12-06T22:59:09Z</dc:date>
    </item>
    <item>
      <title>Re: Merging and keeping only common variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-and-keeping-only-common-variables/m-p/519328#M140652</link>
      <description>&lt;P&gt;The UNION CORR ALL method is probably the best.&amp;nbsp; (without the ALL keyword UNION will remove duplicate rows).&lt;/P&gt;
&lt;P&gt;If you try to do it using a data step then you can get metadata attributes like FORMAT settings overriding the attributes set in the template.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data template;
 input A B C;
cards;
;

data sample;
  input B  A ;
  format a 4.  ;
cards;
4   1
5   2
6   3
;

proc transpose data=sample (obs=0) out=names ; var _all_; run;
proc sql noprint ;
  select _name_ into :names separated by ' ' from names ;
quit;

data almost;
  set template(keep=&amp;amp;names) sample;
run;

proc sql noprint;
create table want as
  select * from template
  union corr all
  select * from sample
;
describe table almost ;
describe table want ;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;144  describe table almost;
NOTE: SQL table WORK.ALMOST was created like:

create table WORK.ALMOST( bufsize=65536 )
  (
   A num format=4.,
   B num
  );

145  describe table want ;
NOTE: SQL table WORK.WANT was created like:

create table WORK.WANT( bufsize=65536 )
  (
   A num,
   B num
  );&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 07 Dec 2018 00:32:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-and-keeping-only-common-variables/m-p/519328#M140652</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-12-07T00:32:53Z</dc:date>
    </item>
  </channel>
</rss>

