<?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: Merge create new variable based on values of variables from each dataset in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Merge-create-new-variable-based-on-values-of-variables-from-each/m-p/309346#M66515</link>
    <description>&lt;P&gt;If you have a variable in both sets unless you rename at least one of the variables then the value will be from only one data set.&lt;/P&gt;
&lt;P&gt;If you don't want to keep a specific variable that is what the DROP statement is for. Rename the county in set have2 using a dataset option rename statement and the drop that one would likely be simplest.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You will likely want to have an In option for the second set as well so that you know if one or both sets contribute to the current observation.&lt;/P&gt;</description>
    <pubDate>Fri, 04 Nov 2016 16:44:04 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2016-11-04T16:44:04Z</dc:date>
    <item>
      <title>Merge create new variable based on values of variables from each dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-create-new-variable-based-on-values-of-variables-from-each/m-p/309343#M66514</link>
      <description>&lt;P&gt;Let's say I have this merge and the datasets have1 and have2 each have two variables: ID and Country.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  merge have1(in=h1) have2;
  by ID;
  if h1;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I want to create a new variable based on the value of the variable Country in each dataset - a condition of whether Country&amp;nbsp;from have1 = Country from have 2. In SQL, I would do this simply by using a case-when-end-as, but how would I do it in data step merge? I could rename the variables say to Country1 and Country2 then say if Country1=Country2, assign this value to new variable. But I don't wnat to keep these renamed variables - I want the original variable to stay.&lt;/P&gt;</description>
      <pubDate>Fri, 04 Nov 2016 16:34:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-create-new-variable-based-on-values-of-variables-from-each/m-p/309343#M66514</guid>
      <dc:creator>JediApprentice</dc:creator>
      <dc:date>2016-11-04T16:34:28Z</dc:date>
    </item>
    <item>
      <title>Re: Merge create new variable based on values of variables from each dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-create-new-variable-based-on-values-of-variables-from-each/m-p/309346#M66515</link>
      <description>&lt;P&gt;If you have a variable in both sets unless you rename at least one of the variables then the value will be from only one data set.&lt;/P&gt;
&lt;P&gt;If you don't want to keep a specific variable that is what the DROP statement is for. Rename the county in set have2 using a dataset option rename statement and the drop that one would likely be simplest.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You will likely want to have an In option for the second set as well so that you know if one or both sets contribute to the current observation.&lt;/P&gt;</description>
      <pubDate>Fri, 04 Nov 2016 16:44:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-create-new-variable-based-on-values-of-variables-from-each/m-p/309346#M66515</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-11-04T16:44:04Z</dc:date>
    </item>
    <item>
      <title>Re: Merge create new variable based on values of variables from each dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-create-new-variable-based-on-values-of-variables-from-each/m-p/309350#M66518</link>
      <description>&lt;P&gt;You can't have two variables of same name in a dataset, so anyhow you must rename COUNTRY of one of the two datasets.&lt;/P&gt;
&lt;P&gt;Your suggestion is right:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; merge have1(in=in1)&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;have2(in=in2 &lt;STRONG&gt;rename&lt;/STRONG&gt;=(country=country2))&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;; by ID;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if in1 and not in2 then new_var = &amp;nbsp;&amp;lt;any value-1&lt;SPAN&gt;&amp;nbsp;or DELETE;&lt;/SPAN&gt; &amp;gt;; else&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if in2 and not in1 then new_var = &amp;lt;any value-2 &amp;nbsp;or DELETE; &amp;gt;; else&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/* ID common on both datasets */&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if country = country2 then new_var = &amp;lt;any value-3&amp;gt;;&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; else new_var = &amp;lt;any value-4&amp;gt;;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;</description>
      <pubDate>Fri, 04 Nov 2016 16:52:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-create-new-variable-based-on-values-of-variables-from-each/m-p/309350#M66518</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2016-11-04T16:52:57Z</dc:date>
    </item>
    <item>
      <title>Re: Merge create new variable based on values of variables from each dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-create-new-variable-based-on-values-of-variables-from-each/m-p/309352#M66519</link>
      <description>&lt;P&gt;You've set up an impossible goal:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;There should only be one COUNTRY variable&lt;/LI&gt;
&lt;LI&gt;The COUNTRY values from the two data sets might be different&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;So if they are different, what would you like COUNTRY to contain? &amp;nbsp;The value from HAVE1 or the value from HAVE2?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For some applications (not clear if this applies to yours or not), the solution is to sort and merge by both variables (BY ID COUNTRY). &amp;nbsp;Then the in= variables verify whether you have a match or a mismatch.&lt;/P&gt;</description>
      <pubDate>Fri, 04 Nov 2016 16:57:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-create-new-variable-based-on-values-of-variables-from-each/m-p/309352#M66519</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-11-04T16:57:53Z</dc:date>
    </item>
  </channel>
</rss>

