<?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 two files with matching variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Combining-two-files-with-matching-variables/m-p/570638#M160939</link>
    <description>&lt;P&gt;Will this be close to your need?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=file1(where=(not missing(id)) out=file1_sort;
by id;
run;

proc sort data=file2(where=(not missing(id)) out=file2_sort;
by id;
run;

data temp;
set file1_sort(in=a) file2_sort(in=b);
by id;
if last.id;
run;

/*Now you could filter file1 and file2 keeping only the missing ids and append them to the above temp*/
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 02 Jul 2019 16:47:53 GMT</pubDate>
    <dc:creator>novinosrin</dc:creator>
    <dc:date>2019-07-02T16:47:53Z</dc:date>
    <item>
      <title>Combining two files with matching variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Combining-two-files-with-matching-variables/m-p/570627#M160932</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have two data files. Each one has an ID variable, var1-var7 (which are the same variables on each file), and then the first file has some additional variables not found on the other file. The ID variable is unique on both files, BUT on File1 can sometimes be missing. Some of the IDs match across files, but there are also IDs that are unique to each. If the ID is missing, var1 - var7 will be missing, but other vars can be populated. If the IDs match across both files, var1-var7 will only be populated on File2.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;FILE1&lt;/P&gt;&lt;P&gt;ID&amp;nbsp; &amp;nbsp;var1&amp;nbsp; &amp;nbsp; var2&amp;nbsp; &amp;nbsp; var3....var7&amp;nbsp; &amp;nbsp; other_vars...&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;xyz&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;d&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; e&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; f&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;xyz&lt;/P&gt;&lt;P&gt;.&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; xyz&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;FILE2&lt;/P&gt;&lt;P&gt;ID&amp;nbsp; &amp;nbsp;var1&amp;nbsp; &amp;nbsp; var2&amp;nbsp; &amp;nbsp; var3....var7&amp;nbsp; &amp;nbsp;&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;a&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;b&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; c&amp;nbsp; &amp;nbsp; &amp;nbsp; d&lt;/P&gt;&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;a&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;b&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; c&amp;nbsp; &amp;nbsp; &amp;nbsp; d&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I want is to combine the files such that:&lt;/P&gt;&lt;P&gt;1. All records from File1 and File2 are kept (even those with no ID)&lt;/P&gt;&lt;P&gt;2. If a record is in both files, the var1-var7 values come from File2&lt;/P&gt;&lt;P&gt;3. If a record is just in File1 or just in File2, all values should remain intact for all variables.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I considered using UPDATE to update File1 using File2 (as the update process is essentially what I want to do), but the missing ID variables seemed to be problematic when I tried. I also tried using a data step merge and then a PROC SQL join, but in both of those cases, either the File1-only or File2-only values were being overwritten.&lt;/P&gt;</description>
      <pubDate>Tue, 02 Jul 2019 16:24:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Combining-two-files-with-matching-variables/m-p/570627#M160932</guid>
      <dc:creator>Walternate</dc:creator>
      <dc:date>2019-07-02T16:24:55Z</dc:date>
    </item>
    <item>
      <title>Re: Combining two files with matching variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Combining-two-files-with-matching-variables/m-p/570638#M160939</link>
      <description>&lt;P&gt;Will this be close to your need?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=file1(where=(not missing(id)) out=file1_sort;
by id;
run;

proc sort data=file2(where=(not missing(id)) out=file2_sort;
by id;
run;

data temp;
set file1_sort(in=a) file2_sort(in=b);
by id;
if last.id;
run;

/*Now you could filter file1 and file2 keeping only the missing ids and append them to the above temp*/
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 02 Jul 2019 16:47:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Combining-two-files-with-matching-variables/m-p/570638#M160939</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-07-02T16:47:53Z</dc:date>
    </item>
    <item>
      <title>Re: Combining two files with matching variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Combining-two-files-with-matching-variables/m-p/570651#M160943</link>
      <description>&lt;P&gt;Have you tried adding in an identity variable to your dataset and using that to merge? e.g.:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data one;
set file1;
file1=1;
proc sort data=one;
by ID;
run;

data two;
set file2;
file2=1;
proc sort data=two;
by ID;
run;

data new;
merge one two;
by ID;
if file1 eq 1 or file2 eq 1;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 02 Jul 2019 17:03:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Combining-two-files-with-matching-variables/m-p/570651#M160943</guid>
      <dc:creator>anhl1206</dc:creator>
      <dc:date>2019-07-02T17:03:46Z</dc:date>
    </item>
    <item>
      <title>Re: Combining two files with matching variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Combining-two-files-with-matching-variables/m-p/570656#M160945</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA WORK.HAVE_1;
FORMAT 	 ID	1. Var1 $1. 	Var2 $1. 	Var3 $1. 	Var4 $1. 	Var5 $1. 	Var6 $1.	Var7 $1.	OtherVar $3.;
INFORMAT ID	1. Var2 $1. 	Var2 $1. 	Var3 $1. 	Var4 $1. 	Var5 $1. 	Var6 $1.	Var7 $1.	OtherVar $3.;
INPUT    ID		VAR1 		VAR2 		VAR3 		VAR4 		VAR5 		VAR6		VAR7		OtherVar;
INFILE DATALINES DLM='|' DSD;
DATALINES;
1||||||||xyz
2|d||e||||f|xyz
.||||||||xyz
;

DATA WORK.HAVE_2;
FORMAT 	 ID	1. Var1 $1. 	Var2 $1. 	Var3 $1. 	Var4 $1. 	Var5 $1. 	Var6 $1.	Var7 $1.;
INFORMAT ID	1. Var2 $1. 	Var2 $1. 	Var3 $1. 	Var4 $1. 	Var5 $1. 	Var6 $1.	Var7 $1.;
INPUT    ID		VAR1 		VAR2 		VAR3 		VAR4 		VAR5 		VAR6		VAR7;
INFILE DATALINES DLM='|' DSD;
DATALINES;
1|a|b|c| | | |d
3|a|b|c| | | |d
;



PROC SQL;
CREATE TABLE WORK.WANT	AS
	SELECT a.ID, b.VAR1, b.VAR2, b.VAR3, b.VAR4, b.VAR5, b.VAR6, b.VAR7, a.OtherVar

	FROM 		WORK.Have_1 AS a
	INNER JOIN	WORK.Have_2 AS b	ON a.ID=b.ID
UNION
	SELECT a.ID, a.VAR1, a.VAR2, a.VAR3, a.VAR4, a.VAR5, a.VAR6, a.VAR7, a.OtherVar
	FROM 		WORK.Have_1 AS a
	LEFT JOIN	WORK.Have_2 AS b	ON a.ID=b.ID
	WHERE b.ID IS NULL
UNION
	SELECT a.ID, a.VAR1, a.VAR2, a.VAR3, a.VAR4, a.VAR5, a.VAR6, a.VAR7, b.OtherVar

	FROM 		WORK.Have_2 AS a
	LEFT JOIN	WORK.Have_1 AS b	ON a.ID=b.ID
	WHERE b.ID IS NULL;
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;resulting output aligns with the request in your post:&lt;/P&gt;&lt;PRE&gt;ID	Var1	Var2	Var3	Var4	Var5	Var6	Var7	OtherVar
.								xyz
1	a	b	c				d	xyz
2	d		e				f	xyz
3	a	b	c				d	&lt;/PRE&gt;</description>
      <pubDate>Tue, 02 Jul 2019 17:12:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Combining-two-files-with-matching-variables/m-p/570656#M160945</guid>
      <dc:creator>tsap</dc:creator>
      <dc:date>2019-07-02T17:12:58Z</dc:date>
    </item>
    <item>
      <title>Re: Combining two files with matching variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Combining-two-files-with-matching-variables/m-p/570658#M160946</link>
      <description>&lt;P&gt;That's perfect, thank you!&lt;/P&gt;</description>
      <pubDate>Tue, 02 Jul 2019 17:16:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Combining-two-files-with-matching-variables/m-p/570658#M160946</guid>
      <dc:creator>Walternate</dc:creator>
      <dc:date>2019-07-02T17:16:56Z</dc:date>
    </item>
    <item>
      <title>Re: Combining two files with matching variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Combining-two-files-with-matching-variables/m-p/570659#M160947</link>
      <description>You're welcome. Glad I could help.</description>
      <pubDate>Tue, 02 Jul 2019 17:20:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Combining-two-files-with-matching-variables/m-p/570659#M160947</guid>
      <dc:creator>tsap</dc:creator>
      <dc:date>2019-07-02T17:20:37Z</dc:date>
    </item>
  </channel>
</rss>

