<?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 Compare variables of two datasets and Merge in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/How-to-Compare-variables-of-two-datasets-and-Merge/m-p/739756#M29009</link>
    <description>&lt;P&gt;As I understand it, your question has two parts:&lt;BR /&gt;1.It is desired to identify the variables which are common and which are unique to each of the two datasets.&lt;/P&gt;
&lt;P&gt;2.Merge the two datasets.&lt;/P&gt;
&lt;P&gt;While there are many approaches, I would may use the following:&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Part 1 :Identifying common and unique variables&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;The following code identifies which variable is present in each dataset. You will have yes if the variable is present in the data set .&lt;BR /&gt;You can modify the code to suite your needs. I am giving a proof of concept or demo.&lt;/P&gt;
&lt;P&gt;I am using sashelp.class and sashelp.shoes datasets. Again it is for demo purpose. (different people may have different ideas)&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc contents data=sashelp.class out=class (keep=Name);
run;
proc contents data=sashelp.shoes out=shoes (keep=Name);
run;

data var_comparison (keep=Name class shoes);
merge class (in=a) shoes(in=b);
by Name;
if a then Class="Yes" ;
if b then Shoes="Yes";
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The output will be something like this. If the variables are common to both, you will have two "YES" in a row. In the example there are no common variables, hence only one yes.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Screenshot 2021-05-07 8.47.38 AM.png" style="width: 182px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/59131i525E50360CEEB26D/image-size/large?v=v2&amp;amp;px=999" role="button" title="Screenshot 2021-05-07 8.47.38 AM.png" alt="Screenshot 2021-05-07 8.47.38 AM.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Part 2: Merging the two datasets&lt;/STRONG&gt;&lt;BR /&gt;This question has been&amp;nbsp; already answered and there is a lot in the literature.&lt;BR /&gt;There is one brief review I would suggest you have a look.&lt;/P&gt;
&lt;P&gt;&lt;A href="https://www.lexjansen.com/nesug/nesug11/ds/ds03.pdf" target="_blank"&gt;https://www.lexjansen.com/nesug/nesug11/ds/ds03.pdf&lt;/A&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 07 May 2021 12:57:55 GMT</pubDate>
    <dc:creator>Sajid01</dc:creator>
    <dc:date>2021-05-07T12:57:55Z</dc:date>
    <item>
      <title>How to Compare variables of two datasets and Merge</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-Compare-variables-of-two-datasets-and-Merge/m-p/739616#M28983</link>
      <description>&lt;P&gt;I have two big datasets and I wanted to compare their variables and merge them. I know there are one or more variables that might be in one or the other. So, I was wondering what is the best way to identify which variables are in common and which variables are in one or the other. After identifying this, I want to merge the two datasets into one dataset. what is the best way to follow and the merging. If someone can show me an example that would be wonderful. Please help! Thank you!&lt;/P&gt;</description>
      <pubDate>Thu, 06 May 2021 19:07:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-Compare-variables-of-two-datasets-and-Merge/m-p/739616#M28983</guid>
      <dc:creator>hjjijkkl</dc:creator>
      <dc:date>2021-05-06T19:07:34Z</dc:date>
    </item>
    <item>
      <title>Re: How to Compare variables of two datasets and Merge</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-Compare-variables-of-two-datasets-and-Merge/m-p/739621#M28984</link>
      <description>Merge is dependent on first step. Have you looked into PROC COMPARE?&lt;BR /&gt;The first part of the output will have what you need - ie which variables are in which data set and which are missing. It will also compare types and formats. &lt;BR /&gt;&lt;BR /&gt;proc compare data=data1 compare=data2;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;See Part one of the output illustrated here:&lt;BR /&gt;&lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/proc/p1k00d45g03uv8n1bfx3d20breg6.htm" target="_blank"&gt;https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/proc/p1k00d45g03uv8n1bfx3d20breg6.htm&lt;/A&gt;</description>
      <pubDate>Thu, 06 May 2021 19:17:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-Compare-variables-of-two-datasets-and-Merge/m-p/739621#M28984</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-05-06T19:17:47Z</dc:date>
    </item>
    <item>
      <title>Re: How to Compare variables of two datasets and Merge</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-Compare-variables-of-two-datasets-and-Merge/m-p/739622#M28985</link>
      <description>&lt;P&gt;Proc compare is likely the easiest way to get a summary of the variable names/ types and differences that are involved.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The question next becomes do your mean to "stack" the data, one set after the other, or align values side-by-side such as on a common identifier?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;SAS has a very particular meaning for MERGE and we have many people new to SAS using "merge" for combining data when they actually mean Append (stack vertically).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Which you are doing makes a difference because the side-by-side MERGE will replace values of commonly named variables. So if you need "sales" variable value from both sets there would be additional work needed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Best is provide small examples of your data and what you expect the result to look like. This can be done in text with 3 or 4 variables from each set, with at least one variable different, for each and then show the expected result.&lt;/P&gt;
&lt;P&gt;Example of two data sets.&lt;/P&gt;
&lt;PRE&gt;Data one;
   input id $  x y;
datalines;
abc 1 2
pdq 22 33
;

data two;
   input id $ x z;
datalines;
abc 2 55
zzz  33 888
;

&lt;/PRE&gt;
&lt;P&gt;What would the result you are looking for be with these two examples?&lt;/P&gt;</description>
      <pubDate>Fri, 07 May 2021 16:56:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-Compare-variables-of-two-datasets-and-Merge/m-p/739622#M28985</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-05-07T16:56:54Z</dc:date>
    </item>
    <item>
      <title>Re: How to Compare variables of two datasets and Merge</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-Compare-variables-of-two-datasets-and-Merge/m-p/739626#M28986</link>
      <description>Actually you are rights. I need to append(stack vertically) than merging the two data sets. This an example I can give.&lt;BR /&gt;ID	Sex	age&lt;BR /&gt;1	F	26&lt;BR /&gt;2	F	43&lt;BR /&gt;&lt;BR /&gt;ID	Sex	age	Education &lt;BR /&gt;4	M	33	BA&lt;BR /&gt;6	F	67	PHD&lt;BR /&gt;&lt;BR /&gt;ID	Sex	age	Education&lt;BR /&gt;1	F	26	&lt;BR /&gt;2	F	43	&lt;BR /&gt;4	M	33	BA&lt;BR /&gt;6	F	67	PHD&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Thu, 06 May 2021 19:36:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-Compare-variables-of-two-datasets-and-Merge/m-p/739626#M28986</guid>
      <dc:creator>hjjijkkl</dc:creator>
      <dc:date>2021-05-06T19:36:12Z</dc:date>
    </item>
    <item>
      <title>Re: How to Compare variables of two datasets and Merge</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-Compare-variables-of-two-datasets-and-Merge/m-p/739647#M28987</link>
      <description>Then this is all you need. &lt;BR /&gt;You can use the INDSNAME option to track which record comes from which data set, if that's needed.&lt;BR /&gt;&lt;BR /&gt;Data want;&lt;BR /&gt;length _source source $50.;&lt;BR /&gt;Set Table1 Table2 INDSNAME = _source;&lt;BR /&gt;source = source;&lt;BR /&gt;&lt;BR /&gt;run;</description>
      <pubDate>Thu, 06 May 2021 21:12:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-Compare-variables-of-two-datasets-and-Merge/m-p/739647#M28987</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-05-06T21:12:55Z</dc:date>
    </item>
    <item>
      <title>Re: How to Compare variables of two datasets and Merge</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-Compare-variables-of-two-datasets-and-Merge/m-p/739756#M29009</link>
      <description>&lt;P&gt;As I understand it, your question has two parts:&lt;BR /&gt;1.It is desired to identify the variables which are common and which are unique to each of the two datasets.&lt;/P&gt;
&lt;P&gt;2.Merge the two datasets.&lt;/P&gt;
&lt;P&gt;While there are many approaches, I would may use the following:&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Part 1 :Identifying common and unique variables&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;The following code identifies which variable is present in each dataset. You will have yes if the variable is present in the data set .&lt;BR /&gt;You can modify the code to suite your needs. I am giving a proof of concept or demo.&lt;/P&gt;
&lt;P&gt;I am using sashelp.class and sashelp.shoes datasets. Again it is for demo purpose. (different people may have different ideas)&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc contents data=sashelp.class out=class (keep=Name);
run;
proc contents data=sashelp.shoes out=shoes (keep=Name);
run;

data var_comparison (keep=Name class shoes);
merge class (in=a) shoes(in=b);
by Name;
if a then Class="Yes" ;
if b then Shoes="Yes";
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The output will be something like this. If the variables are common to both, you will have two "YES" in a row. In the example there are no common variables, hence only one yes.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Screenshot 2021-05-07 8.47.38 AM.png" style="width: 182px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/59131i525E50360CEEB26D/image-size/large?v=v2&amp;amp;px=999" role="button" title="Screenshot 2021-05-07 8.47.38 AM.png" alt="Screenshot 2021-05-07 8.47.38 AM.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Part 2: Merging the two datasets&lt;/STRONG&gt;&lt;BR /&gt;This question has been&amp;nbsp; already answered and there is a lot in the literature.&lt;BR /&gt;There is one brief review I would suggest you have a look.&lt;/P&gt;
&lt;P&gt;&lt;A href="https://www.lexjansen.com/nesug/nesug11/ds/ds03.pdf" target="_blank"&gt;https://www.lexjansen.com/nesug/nesug11/ds/ds03.pdf&lt;/A&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 07 May 2021 12:57:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-Compare-variables-of-two-datasets-and-Merge/m-p/739756#M29009</guid>
      <dc:creator>Sajid01</dc:creator>
      <dc:date>2021-05-07T12:57:55Z</dc:date>
    </item>
  </channel>
</rss>

