<?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: How to merge many tables and replace variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-merge-many-tables-and-replace-variables/m-p/494375#M130260</link>
    <description>&lt;P&gt;If you prefer SQL:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
select have1.*, overlap 
from have1 inner join have2000 on
    have1.year=2000 and 
    have1.country1=have2000.country1 and 
    have1.country2=have2000.country2
union all
select have1.*, overlap  
from have1 inner join have2001 on
    have1.year=2001 and 
    have1.country1=have2001.country1 and 
    have1.country2=have2001.country2;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 11 Sep 2018 03:06:25 GMT</pubDate>
    <dc:creator>PGStats</dc:creator>
    <dc:date>2018-09-11T03:06:25Z</dc:date>
    <item>
      <title>How to merge many tables and replace variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-merge-many-tables-and-replace-variables/m-p/494318#M130232</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have many many tables. And I would like to have them merged together. I would like to put OVERLAP in HAVE2000 in 2000's row in HAVE1. And put OVERLAP in HAVE2001 in 2001 row in HAVE1.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have1 ;
input
COUNTRY1 YEAR COUNTRY2 $5.;
datalines;
001  2000  101
001  2000  002
001  2001  103
002  2000  201
002  2000  202
002  2001  203
003  2001  203
003  2000  301
003  2000  302
003  2001  303
;
run;

data have2000 ;
input
COUNTRY1 COUNTRY2 OVERLAP $5.;
datalines;
001  101  1
001  002  2
001  103  1
002  201  0
002  202  1
002  203  2
003  203  1
003  301  2
003  302  0
003  303  0
;
run;

data have2001 ;
input
COUNTRY1 COUNTRY2 OVERLAP $5.;
datalines;
001  101  2
001  002  1
001  103  0
002  201  1
002  202  2
002  203  1
003  203  1
003  301  1
003  302  2
003  303  0
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Want is like this&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
input
COUNTRY1 YEAR COUNTRY2 OVERLAP $5.;
datalines;
001  2000  101  1
001  2000  002  2
001  2001  103  0
002  2000  201  0
002  2000  202  1
002  2001  203  1
003  2001  203  1
003  2000  301  2
003  2000  302  0
003  2001  303  0
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Shall I use left join or something?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you so much.&lt;/P&gt;</description>
      <pubDate>Mon, 10 Sep 2018 20:42:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-merge-many-tables-and-replace-variables/m-p/494318#M130232</guid>
      <dc:creator>yanshuai</dc:creator>
      <dc:date>2018-09-10T20:42:02Z</dc:date>
    </item>
    <item>
      <title>Re: How to merge many tables and replace variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-merge-many-tables-and-replace-variables/m-p/494322#M130236</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have1 ;
input
COUNTRY1 YEAR COUNTRY2 ;
datalines;
001  2000  101
001  2000  002
001  2001  103
002  2000  201
002  2000  202
002  2001  203
003  2001  203
003  2000  301
003  2000  302
003  2001  303
;
run;

data have2000 ;
input
COUNTRY1 COUNTRY2 OVERLAP $5.;
datalines;
001  101  1
001  002  2
001  103  1
002  201  0
002  202  1
002  203  2
003  203  1
003  301  2
003  302  0
003  303  0
;
run;

data have2001 ;
input
COUNTRY1 COUNTRY2 OVERLAP $5.;
datalines;
001  101  2
001  002  1
001  103  0
002  201  1
002  202  2
002  203  1
003  203  1
003  301  1
003  302  2
003  303  0
;
run;

data want;
if 0 then set have1;
dcl hash H (multidata:'y') ;
   h.definekey  ('year','COUNTRY1','COUNTRY2') ;
   h.definedata ('overlap') ;
   h.definedone();
do until(lr);
set have2000 have2001 indsname=name end=lr;
year=input(compress(name,,'kd'),8.);
rc=h.add();
end;
lr=0;
do until(lr);
set have1;
rc=h.find();
output;
end;
stop;
drop rc;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 10 Sep 2018 21:04:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-merge-many-tables-and-replace-variables/m-p/494322#M130236</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-09-10T21:04:35Z</dc:date>
    </item>
    <item>
      <title>Re: How to merge many tables and replace variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-merge-many-tables-and-replace-variables/m-p/494375#M130260</link>
      <description>&lt;P&gt;If you prefer SQL:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
select have1.*, overlap 
from have1 inner join have2000 on
    have1.year=2000 and 
    have1.country1=have2000.country1 and 
    have1.country2=have2000.country2
union all
select have1.*, overlap  
from have1 inner join have2001 on
    have1.year=2001 and 
    have1.country1=have2001.country1 and 
    have1.country2=have2001.country2;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 11 Sep 2018 03:06:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-merge-many-tables-and-replace-variables/m-p/494375#M130260</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2018-09-11T03:06:25Z</dc:date>
    </item>
    <item>
      <title>Re: How to merge many tables and replace variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-merge-many-tables-and-replace-variables/m-p/495422#M130745</link>
      <description>&lt;P&gt;Thank you.&lt;/P&gt;&lt;P&gt;SQL is always my favorite. So far I just add a YEAR variable to and append all have&amp;amp;year. tables and match by year.&lt;/P&gt;&lt;P&gt;SQL is also working. But since I have 20 tables have&amp;amp;years., should I add UNION ALL 20 times in SQL?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you. If there is a convenient way to repeat UNION ALL steps by year, it will be very greate.&lt;/P&gt;</description>
      <pubDate>Thu, 13 Sep 2018 17:35:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-merge-many-tables-and-replace-variables/m-p/495422#M130745</guid>
      <dc:creator>yanshuai</dc:creator>
      <dc:date>2018-09-13T17:35:17Z</dc:date>
    </item>
    <item>
      <title>Re: How to merge many tables and replace variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-merge-many-tables-and-replace-variables/m-p/495424#M130747</link>
      <description>&lt;P&gt;Thank you very much for the answer!&lt;/P&gt;</description>
      <pubDate>Thu, 13 Sep 2018 17:35:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-merge-many-tables-and-replace-variables/m-p/495424#M130747</guid>
      <dc:creator>yanshuai</dc:creator>
      <dc:date>2018-09-13T17:35:54Z</dc:date>
    </item>
    <item>
      <title>Re: How to merge many tables and replace variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-merge-many-tables-and-replace-variables/m-p/495479#M130768</link>
      <description>&lt;P&gt;It would be a lot simpler to concatenate your yearly datasets (while adding a YEAR variable) before joining with your main (have1) dataset on YEAR, COUNTRY1, and COUNTRY2.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Concatenate with something like:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data haveAll;&lt;/P&gt;
&lt;P&gt;set have20: INDSNAME=dsn;&lt;/P&gt;
&lt;P&gt;/* get year from dataset name, such as WORK.HAVE2010 */&lt;/P&gt;
&lt;P&gt;year = input(substr(scan(dsn, 2), 5), best.);&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 13 Sep 2018 19:44:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-merge-many-tables-and-replace-variables/m-p/495479#M130768</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2018-09-13T19:44:56Z</dc:date>
    </item>
    <item>
      <title>Re: How to merge many tables and replace variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-merge-many-tables-and-replace-variables/m-p/495489#M130776</link>
      <description>&lt;P&gt;Thank you very much.&lt;/P&gt;&lt;P&gt;I actually did that. Here is something I use so far. It has been a while since last using....but it seems work out fine.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data haveALL ;
   set have2000-have2017 indsname=dsname ;
   length YEAR $4. ;
   year = substr(dsname,length(dsname)-3);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 13 Sep 2018 20:08:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-merge-many-tables-and-replace-variables/m-p/495489#M130776</guid>
      <dc:creator>yanshuai</dc:creator>
      <dc:date>2018-09-13T20:08:10Z</dc:date>
    </item>
    <item>
      <title>Re: How to merge many tables and replace variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-merge-many-tables-and-replace-variables/m-p/495503#M130782</link>
      <description>&lt;P&gt;Great! But year is a number in have1 and created as a character variable in haveALL. You can make them more compatible with the input function.&lt;/P&gt;</description>
      <pubDate>Thu, 13 Sep 2018 20:26:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-merge-many-tables-and-replace-variables/m-p/495503#M130782</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2018-09-13T20:26:47Z</dc:date>
    </item>
  </channel>
</rss>

