<?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 with a dataset and create a new variable if a value of an existing variable is missing in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Merge-with-a-dataset-and-create-a-new-variable-if-a-value-of-an/m-p/600689#M173683</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/172057"&gt;@gsk&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;In SAS can we do something like&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;but b.new_var is created if an observation from a has missing(variable1). If missing(a.variable1), then read b.variable2, b.variable3, b.variable4 and create a new_var based on some direction involving variable2 and variable3 and variable4.&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;For example, something like this&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;&amp;nbsp;
&amp;nbsp; &amp;nbsp; select a.*, 
      case when missing(variable1) then b.variable2+b.variable3+b.variable4
            end else . as new_var,
&amp;nbsp; &amp;nbsp; from data1 as a
&amp;nbsp; &amp;nbsp; left join data2 as b
&amp;nbsp; &amp;nbsp; &amp;nbsp; on a.subjid=b.subjd;&amp;nbsp;
quit;&amp;nbsp;&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;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 31 Oct 2019 14:22:33 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2019-10-31T14:22:33Z</dc:date>
    <item>
      <title>Merge with a dataset and create a new variable if a value of an existing variable is missing</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-with-a-dataset-and-create-a-new-variable-if-a-value-of-an/m-p/600686#M173681</link>
      <description>&lt;P&gt;In SAS can we do something like&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sql;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; select a.*, b.new_var&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; from data1 as a&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; left join data2 as b&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; on a.subjid=b.subjd;&amp;nbsp;&lt;/P&gt;&lt;P&gt;quit;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;but b.new_var is created if an observation from a has missing(variable1). If missing(a.variable1), then read b.variable2, b.variable3, b.variable4 and create a new_var based on some direction involving variable2 and variable3 and variable4.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 31 Oct 2019 13:40:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-with-a-dataset-and-create-a-new-variable-if-a-value-of-an/m-p/600686#M173681</guid>
      <dc:creator>gsk</dc:creator>
      <dc:date>2019-10-31T13:40:21Z</dc:date>
    </item>
    <item>
      <title>Re: Merge with a dataset and create a new variable if a value of an existing variable is missing</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-with-a-dataset-and-create-a-new-variable-if-a-value-of-an/m-p/600689#M173683</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/172057"&gt;@gsk&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;In SAS can we do something like&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;but b.new_var is created if an observation from a has missing(variable1). If missing(a.variable1), then read b.variable2, b.variable3, b.variable4 and create a new_var based on some direction involving variable2 and variable3 and variable4.&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;For example, something like this&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;&amp;nbsp;
&amp;nbsp; &amp;nbsp; select a.*, 
      case when missing(variable1) then b.variable2+b.variable3+b.variable4
            end else . as new_var,
&amp;nbsp; &amp;nbsp; from data1 as a
&amp;nbsp; &amp;nbsp; left join data2 as b
&amp;nbsp; &amp;nbsp; &amp;nbsp; on a.subjid=b.subjd;&amp;nbsp;
quit;&amp;nbsp;&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;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 31 Oct 2019 14:22:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-with-a-dataset-and-create-a-new-variable-if-a-value-of-an/m-p/600689#M173683</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-10-31T14:22:33Z</dc:date>
    </item>
    <item>
      <title>Re: Merge with a dataset and create a new variable if a value of an existing variable is missing</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-with-a-dataset-and-create-a-new-variable-if-a-value-of-an/m-p/600710#M173690</link>
      <description>&lt;P&gt;Just use the coalesce function:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql; 
    select data1.*, coalesce(data2.new_var,data2.var2*data2.var3,data2.var4)
    from data1 
    left join data2
      on data1.subjid=data2.subjd; 
quit; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;In the case shown, if new_var is missing, the step will use var2*var3, and if that is also missing, var4, in this case all from data2 (but you could also use variables from the data1 table).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And for God's sake: Stop using stupid and non-informative aliases like "a" and "b" in PROC SQL. It may save you a few keystrokes in the beginning, but in the long run it makes the code totally unreadable!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 31 Oct 2019 14:36:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-with-a-dataset-and-create-a-new-variable-if-a-value-of-an/m-p/600710#M173690</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2019-10-31T14:36:10Z</dc:date>
    </item>
    <item>
      <title>Re: Merge with a dataset and create a new variable if a value of an existing variable is missing</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-with-a-dataset-and-create-a-new-variable-if-a-value-of-an/m-p/600745#M173705</link>
      <description>&lt;P&gt;I guess you wanted to do this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;coalesce(data1.variable,data2.var2*data2.var3,data2.var4) as new_var&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 31 Oct 2019 16:56:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-with-a-dataset-and-create-a-new-variable-if-a-value-of-an/m-p/600745#M173705</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-10-31T16:56:16Z</dc:date>
    </item>
  </channel>
</rss>

