<?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: data step equivalent of proc sql coalese in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/data-step-equivalent-of-proc-sql-coalese/m-p/825799#M326164</link>
    <description>&lt;P&gt;In a data step, you can use the COALESCE function on numeric variables, or the COALESCEC function on character variables. You just have to merge the tables in a DATA step, and rename the variables in one of the tables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In addition you can use an ARRAY to prevent the need from writing COALESCE 100 times, you code it once and loop through it 100 times.&lt;/P&gt;</description>
    <pubDate>Wed, 27 Jul 2022 23:21:15 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2022-07-27T23:21:15Z</dc:date>
    <item>
      <title>data step equivalent of proc sql coalese</title>
      <link>https://communities.sas.com/t5/SAS-Programming/data-step-equivalent-of-proc-sql-coalese/m-p/825788#M326159</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I have two dataset A and B, A and B have same column names but different data.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I use the following sql code to 'coalesce" data from A and B:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;create table c as&lt;/P&gt;&lt;P&gt;select coalesce(a.column1, b.column1) as column 1,&lt;/P&gt;&lt;P&gt;...&lt;/P&gt;&lt;P&gt;...&lt;/P&gt;&lt;P&gt;coalesce(a.column100, b.column100) as column 100&lt;/P&gt;&lt;P&gt;from A as a full join B as b&lt;/P&gt;&lt;P&gt;on a.key = b.key and a.period = b.period;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My question is: is there any data step equivalent to the proc sql procedure listed above?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tried&lt;/P&gt;&lt;P&gt;data c;&lt;/P&gt;&lt;P&gt;update a b;&lt;/P&gt;&lt;P&gt;by key, period;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But the problem is that, whenever both table a and table b contains values for say, column1, then the column1 value from the transactional dataset b will be used. My intention is to keep value from the master table a whenever it's not missing and only use value from table b if such value is missing in a(like the way coalesce)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance!&lt;/P&gt;</description>
      <pubDate>Thu, 28 Jul 2022 02:45:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/data-step-equivalent-of-proc-sql-coalese/m-p/825788#M326159</guid>
      <dc:creator>Julie99999</dc:creator>
      <dc:date>2022-07-28T02:45:35Z</dc:date>
    </item>
    <item>
      <title>Re: data step equivalent of proc sql coalese</title>
      <link>https://communities.sas.com/t5/SAS-Programming/data-step-equivalent-of-proc-sql-coalese/m-p/825799#M326164</link>
      <description>&lt;P&gt;In a data step, you can use the COALESCE function on numeric variables, or the COALESCEC function on character variables. You just have to merge the tables in a DATA step, and rename the variables in one of the tables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In addition you can use an ARRAY to prevent the need from writing COALESCE 100 times, you code it once and loop through it 100 times.&lt;/P&gt;</description>
      <pubDate>Wed, 27 Jul 2022 23:21:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/data-step-equivalent-of-proc-sql-coalese/m-p/825799#M326164</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-07-27T23:21:15Z</dc:date>
    </item>
    <item>
      <title>Re: data step equivalent of proc sql coalese</title>
      <link>https://communities.sas.com/t5/SAS-Programming/data-step-equivalent-of-proc-sql-coalese/m-p/825800#M326165</link>
      <description>The UPDATE approach will work, so long as the data sets are limited to one observation per PERIOD KEY combination.  Just change the order in the UPDATE statement:&lt;BR /&gt;&lt;BR /&gt;update b a;</description>
      <pubDate>Wed, 27 Jul 2022 23:21:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/data-step-equivalent-of-proc-sql-coalese/m-p/825800#M326165</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2022-07-27T23:21:11Z</dc:date>
    </item>
    <item>
      <title>Re: data step equivalent of proc sql coalese</title>
      <link>https://communities.sas.com/t5/SAS-Programming/data-step-equivalent-of-proc-sql-coalese/m-p/825802#M326166</link>
      <description>&lt;P&gt;My first suggestion would be to try this assuming the two data sets are sorted correctly:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data c;
   merge b a ;
   by key period;
run;

 &lt;/PRE&gt;
&lt;P&gt;The Merge, assuming only ONE of the data sets has multiples of the By variables, will replace variables left to right on a appearance on the Merge statement. So like named variables in A will replace those of B when the By variables match. This will include missing values in A replacing values in B if such occur. If you do not want the missing values to replace them &lt;STRONG&gt;and&lt;/STRONG&gt; as &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954"&gt;@Astounding&lt;/a&gt; said neither has duplicates of the By variables you can prevent update of missing values from A by using UPDATE.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 28 Jul 2022 00:29:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/data-step-equivalent-of-proc-sql-coalese/m-p/825802#M326166</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-07-28T00:29:10Z</dc:date>
    </item>
    <item>
      <title>Re: data step equivalent of proc sql coalese</title>
      <link>https://communities.sas.com/t5/SAS-Programming/data-step-equivalent-of-proc-sql-coalese/m-p/825816#M326171</link>
      <description>&lt;P&gt;thank you so much my friend. I was thinking the same thing and your comment makes me more confident about the solution I was thinking about. By the way, I recognize your ID, you previously helped me (with my another ID). Thank you so much for continuing contributing to the community!&lt;/P&gt;</description>
      <pubDate>Thu, 28 Jul 2022 02:39:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/data-step-equivalent-of-proc-sql-coalese/m-p/825816#M326171</guid>
      <dc:creator>Julie99999</dc:creator>
      <dc:date>2022-07-28T02:39:26Z</dc:date>
    </item>
    <item>
      <title>Re: data step equivalent of proc sql coalese</title>
      <link>https://communities.sas.com/t5/SAS-Programming/data-step-equivalent-of-proc-sql-coalese/m-p/825818#M326173</link>
      <description>thank you for your reply. THis is my quesiton actually. Because for UPDATE, if we do update b a; then whenever there is value in a, the previous value in b will be overrided. that's not intended. What if I want to always keep values from b, but only get values from a if such values in b is missing? It seems for "update b a", the result is slightly different.</description>
      <pubDate>Thu, 28 Jul 2022 02:41:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/data-step-equivalent-of-proc-sql-coalese/m-p/825818#M326173</guid>
      <dc:creator>Julie99999</dc:creator>
      <dc:date>2022-07-28T02:41:50Z</dc:date>
    </item>
    <item>
      <title>Re: data step equivalent of proc sql coalese</title>
      <link>https://communities.sas.com/t5/SAS-Programming/data-step-equivalent-of-proc-sql-coalese/m-p/825829#M326174</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/430421"&gt;@Julie99999&lt;/a&gt;&amp;nbsp;:&lt;/P&gt;
&lt;P&gt;"update b a" works if you want to keep A if populated and only take B if A is not populated.&lt;/P&gt;
&lt;P&gt;"update a b" works if you want to keep B if populated and only take A if B is not populated.&lt;/P&gt;</description>
      <pubDate>Thu, 28 Jul 2022 03:15:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/data-step-equivalent-of-proc-sql-coalese/m-p/825829#M326174</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2022-07-28T03:15:32Z</dc:date>
    </item>
    <item>
      <title>Re: data step equivalent of proc sql coalese</title>
      <link>https://communities.sas.com/t5/SAS-Programming/data-step-equivalent-of-proc-sql-coalese/m-p/825901#M326221</link>
      <description>&lt;P&gt;Julie,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Actually, that's different than what you posted originally.&amp;nbsp; The originally post looks for the value from A, but if that is missing use the value from B.&amp;nbsp; You would get this (but by a slightly different route) using:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;update b a;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You you start with the value from B.&amp;nbsp; But if the value from A exists, use that to replace the value from B.&amp;nbsp; So you would get the value from A (when it exists), but the value from B (when A has just a missing value).&lt;/P&gt;</description>
      <pubDate>Thu, 28 Jul 2022 12:15:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/data-step-equivalent-of-proc-sql-coalese/m-p/825901#M326221</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2022-07-28T12:15:24Z</dc:date>
    </item>
    <item>
      <title>Re: data step equivalent of proc sql coalese</title>
      <link>https://communities.sas.com/t5/SAS-Programming/data-step-equivalent-of-proc-sql-coalese/m-p/825968#M326244</link>
      <description>Astounding, I see your point. That's smart! That would work and solves exactly my question, how come I didn't realize by simply switching to "b a" would solve the problem.&lt;BR /&gt;update b a;&lt;BR /&gt;thanks for the "A ha" moment you've brought to me. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;BR /&gt;</description>
      <pubDate>Thu, 28 Jul 2022 15:45:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/data-step-equivalent-of-proc-sql-coalese/m-p/825968#M326244</guid>
      <dc:creator>Julie99999</dc:creator>
      <dc:date>2022-07-28T15:45:14Z</dc:date>
    </item>
    <item>
      <title>Re: data step equivalent of proc sql coalese</title>
      <link>https://communities.sas.com/t5/SAS-Programming/data-step-equivalent-of-proc-sql-coalese/m-p/825970#M326246</link>
      <description>thanks for the succinct take-away!</description>
      <pubDate>Thu, 28 Jul 2022 15:45:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/data-step-equivalent-of-proc-sql-coalese/m-p/825970#M326246</guid>
      <dc:creator>Julie99999</dc:creator>
      <dc:date>2022-07-28T15:45:41Z</dc:date>
    </item>
  </channel>
</rss>

