<?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: Proc SQL: handling null values after joining tables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-handling-null-values-after-joining-tables/m-p/846720#M334708</link>
    <description>&lt;P&gt;Thanks, to handle nulls conditionally I think I should use a case statement? This way I can set a null total equal to zero if an employee has null values for all of their records within that field, or the max if one of their records has a non-null value. My instinct would be to partition by the max total, but I don't think that is supported by proc sql.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I was thinking something like this, but I know that won't work.&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;create table reprex as
select a.id,
          a.region,
          a.calls,
          a.title,
          case when total_sales is null then max(coalesce(b.total_sales,0)) over (partition by id) else b.total_sales
from ...
     

&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 29 Nov 2022 01:37:58 GMT</pubDate>
    <dc:creator>everyone</dc:creator>
    <dc:date>2022-11-29T01:37:58Z</dc:date>
    <item>
      <title>Proc SQL: handling null values after joining tables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-handling-null-values-after-joining-tables/m-p/846711#M334701</link>
      <description>&lt;P&gt;I'm summing a field within a join subquery to calculate a field's total and join it to a given table. However, I'm having difficulty handling null values.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I realize a null value plus anything will equal null, so I hoped to handle with coalesce within my sum function. This approach is not working for me, because not all tables contain all IDs. When such a table is joined, I end up with a null value. I'd like to handle these instances conditionally, so that a null total is equal to an employee's max total if this value is non-null or 0 if this value is null.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've included a reprex below. What adjustments should I make to end up with non-null totals for all records in my joined table? Thank you.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;/*sample table a*/
data table_a;
input id title $ region $ calls;
cards;
1 manager south 30
1 agent north 20
2 manager west 20
2 agent south 25
;
run;

/*sample table b*/
data table_b;
input id title $ sales;
cards;
1 manager 20
2 manager 5
2 agent 3
;
run;

/*sample table c*/
data table_c;
input id title $ leads;
cards;
1 manager .
1 agent 10
;
run;

/*join tables &lt;BR /&gt;(in this code, my sums return null values for my calculated totals for IDs where records don't exist in all tables. &lt;BR /&gt; e.g. total_sales are null for id = 1, title = agent&lt;BR /&gt;      total_leads are null for id 2&lt;BR /&gt;      I'd like to replace null values with an employee's max total (20 for employee 1's total sales) or 0 if max total is null (0 for employee 2's total leads)*/
proc sql;
create table reprex as

select a.id, a.region, a.calls, a.title, b.total_sales, b.sales, c.total_leads, c.leads
from table_a as a 

left join (select sum(coalesce(sales, 0)) as total_sales, sales, id, title from table_b group by id) b
on a.id = b.id and a.title = b.title

left join (select sum(coalesce(leads, 0)) as total_leads, leads, id, title from table_c group by id) c 
on a.id = c.id and a.title = c.title;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 28 Nov 2022 23:28:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-handling-null-values-after-joining-tables/m-p/846711#M334701</guid>
      <dc:creator>everyone</dc:creator>
      <dc:date>2022-11-28T23:28:15Z</dc:date>
    </item>
    <item>
      <title>Re: Proc SQL: handling null values after joining tables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-handling-null-values-after-joining-tables/m-p/846715#M334703</link>
      <description>&lt;P&gt;If the goal is to replace the null (missing) values in the final result then move the COALESCE() function to that final list of variables.&lt;/P&gt;
&lt;P&gt;For example if it is just the two variables whose names start with TOTAL_ that you are worried about then make the following change:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;create table reprex as
  select a.id
      , a.region
      , a.calls
      , a.title
      , coalesce(b.total_sales,0) as total_sales
      , b.sales
      , coalesce(c.total_leads,0) as total_leads
      , c.leads
from ...&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 29 Nov 2022 00:38:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-handling-null-values-after-joining-tables/m-p/846715#M334703</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-11-29T00:38:34Z</dc:date>
    </item>
    <item>
      <title>Re: Proc SQL: handling null values after joining tables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-handling-null-values-after-joining-tables/m-p/846720#M334708</link>
      <description>&lt;P&gt;Thanks, to handle nulls conditionally I think I should use a case statement? This way I can set a null total equal to zero if an employee has null values for all of their records within that field, or the max if one of their records has a non-null value. My instinct would be to partition by the max total, but I don't think that is supported by proc sql.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I was thinking something like this, but I know that won't work.&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;create table reprex as
select a.id,
          a.region,
          a.calls,
          a.title,
          case when total_sales is null then max(coalesce(b.total_sales,0)) over (partition by id) else b.total_sales
from ...
     

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 29 Nov 2022 01:37:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-handling-null-values-after-joining-tables/m-p/846720#M334708</guid>
      <dc:creator>everyone</dc:creator>
      <dc:date>2022-11-29T01:37:58Z</dc:date>
    </item>
  </channel>
</rss>

