<?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: combining data sets with only common variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/combining-data-sets-with-only-common-variables/m-p/533842#M146420</link>
    <description>&lt;P&gt;I'm still not following, but I'll take a guess. You have data sets for 3 years. You want records that are in all three years.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1. Stack the data (append, instead of merge)&lt;/P&gt;
&lt;P&gt;2. Count the number of records per your grouping variables. If it's 3 you have records for three years, If it's not three you can drop it.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set data2015-data2017;

run;

proc sql;
create table want as
select *
from want
group by var1, var2, var3
having count(*) = 3;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/253384"&gt;@mmhxc5&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;, here is a screenshot of my data from fhwaed2015 with STRUCNUM and EN and the other variables. I want to combine this with the other two datasets - fhwaed2016, and fhwaed2017 that is matching&amp;nbsp; STRUCNUM and EN condition and generate the dataset to have all variables matching in&amp;nbsp;STRUCNUM and EN. &lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.JPG" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/26972iB1D9E366BE1FC625/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture.JPG" alt="Capture.JPG" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Thanks,&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Obviously untested because no workable data was provided. If this isn't what you want, you'll need to clarify in detail.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;EDIT: If you have duplicates of your grouping variable in the data sets, this method will not work.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 08 Feb 2019 05:12:41 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2019-02-08T05:12:41Z</dc:date>
    <item>
      <title>combining data sets with only common variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/combining-data-sets-with-only-common-variables/m-p/533827#M146415</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;I have three data sets and want to make another one to contain only observarions that is shared in all three datasets. The two variables that shall match the condition in all three datasets are STRUCNUM and EN such as this;&lt;/P&gt;&lt;P&gt;&lt;CODE class=" language-sas"&gt;STRUCNUM 12354 EN 410 from&amp;nbsp;Fhwaed2015&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&lt;CODE class=" language-sas"&gt;STRUCNUM 12354 EN 410 from&amp;nbsp;Fhwaed2016&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&lt;CODE class=" language-sas"&gt;STRUCNUM 12354 EN 410 from&amp;nbsp;Fhwaed2017&lt;/CODE&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If the above condition matched in all three datasets, I want to have them in my dataset.&lt;/P&gt;&lt;P&gt;I tried the following code, but could not get the job done.&lt;/P&gt;&lt;P&gt;I would appreciate if anyone could help me.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table taxesdata as
select *
from data.Fhwaed2015, data.Fhwaed2016, data.Fhwaed2017
where Fhwaed2015.STRUCNUM=Fhwaed2016.STRUCNUM and Fhwaed2015.STRUCNUM=Fhwaed2017.STRUCNUM
Fhwaed2015.en=Fhwaed2016.en and Fhwaed2015.en=Fhwaed2017.en
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp; mmhxc5&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 08 Feb 2019 04:30:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/combining-data-sets-with-only-common-variables/m-p/533827#M146415</guid>
      <dc:creator>mmhxc5</dc:creator>
      <dc:date>2019-02-08T04:30:43Z</dc:date>
    </item>
    <item>
      <title>Re: combining data sets with only common variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/combining-data-sets-with-only-common-variables/m-p/533833#M146416</link>
      <description>Can you expand your sample data set to show some more records of what you have an what you want? If you just want records that are in all three by key variables a data step merge may be more effective here.</description>
      <pubDate>Fri, 08 Feb 2019 04:36:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/combining-data-sets-with-only-common-variables/m-p/533833#M146416</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-02-08T04:36:18Z</dc:date>
    </item>
    <item>
      <title>Re: combining data sets with only common variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/combining-data-sets-with-only-common-variables/m-p/533840#M146419</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;, here is a screenshot of my data from fhwaed2015 with STRUCNUM and EN and the other variables. I want to combine this with the other two datasets - fhwaed2016, and fhwaed2017 that is matching&amp;nbsp; STRUCNUM and EN condition and generate the dataset to have all variables matching in&amp;nbsp;STRUCNUM and EN. &lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.JPG" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/26972iB1D9E366BE1FC625/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture.JPG" alt="Capture.JPG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;</description>
      <pubDate>Fri, 08 Feb 2019 04:55:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/combining-data-sets-with-only-common-variables/m-p/533840#M146419</guid>
      <dc:creator>mmhxc5</dc:creator>
      <dc:date>2019-02-08T04:55:48Z</dc:date>
    </item>
    <item>
      <title>Re: combining data sets with only common variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/combining-data-sets-with-only-common-variables/m-p/533842#M146420</link>
      <description>&lt;P&gt;I'm still not following, but I'll take a guess. You have data sets for 3 years. You want records that are in all three years.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1. Stack the data (append, instead of merge)&lt;/P&gt;
&lt;P&gt;2. Count the number of records per your grouping variables. If it's 3 you have records for three years, If it's not three you can drop it.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set data2015-data2017;

run;

proc sql;
create table want as
select *
from want
group by var1, var2, var3
having count(*) = 3;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/253384"&gt;@mmhxc5&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;, here is a screenshot of my data from fhwaed2015 with STRUCNUM and EN and the other variables. I want to combine this with the other two datasets - fhwaed2016, and fhwaed2017 that is matching&amp;nbsp; STRUCNUM and EN condition and generate the dataset to have all variables matching in&amp;nbsp;STRUCNUM and EN. &lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.JPG" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/26972iB1D9E366BE1FC625/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture.JPG" alt="Capture.JPG" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Thanks,&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Obviously untested because no workable data was provided. If this isn't what you want, you'll need to clarify in detail.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;EDIT: If you have duplicates of your grouping variable in the data sets, this method will not work.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 08 Feb 2019 05:12:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/combining-data-sets-with-only-common-variables/m-p/533842#M146420</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-02-08T05:12:41Z</dc:date>
    </item>
    <item>
      <title>Re: combining data sets with only common variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/combining-data-sets-with-only-common-variables/m-p/533849#M146422</link>
      <description>&lt;P&gt;Following &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt; 's guess, but doing everything in SQL:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table records as
select unique Fhwaed2015.STRUCNUM, Fhwaed2015.en
from data.Fhwaed2015, data.Fhwaed2016, data.Fhwaed2017
where Fhwaed2015.STRUCNUM=Fhwaed2016.STRUCNUM and Fhwaed2015.STRUCNUM=Fhwaed2017.STRUCNUM
Fhwaed2015.en=Fhwaed2016.en and Fhwaed2015.en=Fhwaed2017.en;

create table taxesData as
select a.* from Fhwaed2015 as a inner join records as b on a.STRUCNUM=b.STRUCNUM and a.en=b.en
union all
select a.* from Fhwaed2016 as a inner join records as b on a.STRUCNUM=b.STRUCNUM and a.en=b.en
union all
select a.* from Fhwaed2017 as a inner join records as b on a.STRUCNUM=b.STRUCNUM and a.en=b.en
;

drop table records;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;(untested)&lt;/P&gt;</description>
      <pubDate>Fri, 08 Feb 2019 05:43:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/combining-data-sets-with-only-common-variables/m-p/533849#M146422</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2019-02-08T05:43:41Z</dc:date>
    </item>
    <item>
      <title>Re: combining data sets with only common variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/combining-data-sets-with-only-common-variables/m-p/533918#M146440</link>
      <description>&lt;PRE&gt;Code Not Tested.


proc sql;
create table key as
select STRUCNUM , en from  data.Fhwaed2015
intersect
select STRUCNUM , en from  data.Fhwaed2016
intersect
select STRUCNUM , en from  data.Fhwaed2017
;


create table taxesdata as
select * ,'From Fhwaed2015 ' as label from data.Fhwaed2015  
 where catx(' ', STRUCNUM , en) in (select catx(' ', STRUCNUM , en) from key)
union all 
select * ,'From Fhwaed2016 ' as label from data.Fhwaed2016 
 where catx(' ', STRUCNUM , en) in (select catx(' ', STRUCNUM , en) from key)
union all 
select * ,'From Fhwaed2017' as label from data.Fhwaed2017  
 where catx(' ', STRUCNUM , en) in (select catx(' ', STRUCNUM , en) from key)
;
quit;&lt;/PRE&gt;</description>
      <pubDate>Fri, 08 Feb 2019 14:20:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/combining-data-sets-with-only-common-variables/m-p/533918#M146440</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2019-02-08T14:20:26Z</dc:date>
    </item>
    <item>
      <title>Re: combining data sets with only common variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/combining-data-sets-with-only-common-variables/m-p/533955#M146448</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;, and &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;, thank you for our time and help. Your codes both worked well - same result. &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt; ,&amp;nbsp; ur code gives me this note in the log.&lt;/P&gt;&lt;P&gt;NOTE: The query requires remerging summary statistics back with the original data.&lt;/P&gt;&lt;P&gt;Thank you &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/462"&gt;@PGStats&lt;/a&gt; for ur time.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 08 Feb 2019 15:11:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/combining-data-sets-with-only-common-variables/m-p/533955#M146448</guid>
      <dc:creator>mmhxc5</dc:creator>
      <dc:date>2019-02-08T15:11:23Z</dc:date>
    </item>
    <item>
      <title>Re: combining data sets with only common variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/combining-data-sets-with-only-common-variables/m-p/534218#M146550</link>
      <description>It's a note, not a warning.</description>
      <pubDate>Sun, 10 Feb 2019 00:22:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/combining-data-sets-with-only-common-variables/m-p/534218#M146550</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-02-10T00:22:54Z</dc:date>
    </item>
  </channel>
</rss>

