<?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: adding values not in other table in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/adding-values-not-in-other-table/m-p/735899#M229253</link>
    <description>&lt;P&gt;Not sure why in your have data there is also calendar_month 202&lt;FONT color="#993300"&gt;&lt;STRONG&gt;0&lt;/STRONG&gt;&lt;/FONT&gt;01 but in your desired data there is only 202&lt;FONT color="#993300"&gt;&lt;STRONG&gt;1&lt;/STRONG&gt;&lt;/FONT&gt;01.&lt;/P&gt;
&lt;P&gt;Check below code. It shouldn't be hard to change in case you only want a table for the most recent calendar month in your data or you need to exclude users from your table that don't exist yet/anymore for an specific calendar month (would require from/to columns in your users table).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data users;
  input users $50.;
  datalines;
Andrew Lup
Dan Louis
Mike Mora
Farren Farrao
Joseph Mia
;

data leave;
  input calender_month leave_days leave_type_code username $50.;
  datalines;
202101 1 2 Andrew Lup
202101 1 2 Andrew Lup
202101 1 2 Sol Sebe
202001 1 2 Ant Joe
202101 1 2 Dan Louis
202001 1 2 Mike Mora
202001 1 2 Mike Mora
202001 1 2 Mike Mora
202001 1 2 Mike Mora
;

proc sql;
  select 
    l2.username,
    l2.calender_month,
    coalesce(r2.leave_days,0) as leave_days
  from
    (  
      select 
        l1.users as username,
        r1.calender_month
      from 
        users l1,
        (select distinct calender_month from leave) r1
    ) l2
    left join
    (
      select
        username,
        calender_month,
        sum(leave_days) as leave_days
      from leave
      group by username, calender_month
    ) r2
    on l2.username=r2.username and l2.calender_month=r2.calender_month
  ;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1618999436408.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/58446i841CBA19CE0BDDD2/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_0-1618999436408.png" alt="Patrick_0-1618999436408.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 21 Apr 2021 10:05:36 GMT</pubDate>
    <dc:creator>Patrick</dc:creator>
    <dc:date>2021-04-21T10:05:36Z</dc:date>
    <item>
      <title>adding values not in other table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/adding-values-not-in-other-table/m-p/735887#M229248</link>
      <description>&lt;P&gt;I have two tables, &lt;STRONG&gt;users&lt;/STRONG&gt; table and &lt;STRONG&gt;leave&lt;/STRONG&gt; table which stores all the information for the user who have captured leave.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I need help in adding people who did not apply leave in table &lt;STRONG&gt;Leave_new&lt;/STRONG&gt; with 0 value under&amp;nbsp;&lt;STRONG&gt;&amp;nbsp;leave_days&lt;/STRONG&gt; column&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data users;
input users $50.;
datalines;
Andrew Lup
Dan Louis
Mike Mora
Farren Farrao
Joseph Mia
;
run;

data leave;
input calender_month leave_days leave_type_code username $50.;
datalines;
202101 1 2 Andrew Lup
202101 1 2 Andrew Lup
202101 1 2 Sol Sebe
202001 1 2 Ant Joe
202101 1 2 Dan Louis
202101 1 2 Mike Mora
202101 1 2 Mike Mora
202101 1 2 Mike Mora
202101 1 2 Mike Mora

;

proc sql;
create table Leave_new as
select distinct
a.username,
a.calender_month,
sum(a.leave_days) as leave_days
from have as a

where a.username in (select users from users)

group by 1,2

;quit;&lt;/PRE&gt;
&lt;P&gt;&lt;STRONG&gt;Data Want&lt;/STRONG&gt;&lt;/P&gt;
&lt;TABLE width="352"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="166"&gt;username&lt;/TD&gt;
&lt;TD width="110"&gt;calender_month&lt;/TD&gt;
&lt;TD width="76"&gt;leave_days&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="166"&gt;Andrew Lup&lt;/TD&gt;
&lt;TD width="110"&gt;202101&lt;/TD&gt;
&lt;TD width="76"&gt;2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="166"&gt;Dan Louis&lt;/TD&gt;
&lt;TD width="110"&gt;202101&lt;/TD&gt;
&lt;TD width="76"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;Farren Farrao&lt;/TD&gt;
&lt;TD width="110"&gt;202101&lt;/TD&gt;
&lt;TD width="76"&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;Joseph Mia&lt;/TD&gt;
&lt;TD width="110"&gt;202101&lt;/TD&gt;
&lt;TD width="76"&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="166"&gt;Mike Mora&lt;/TD&gt;
&lt;TD width="110"&gt;202101&lt;/TD&gt;
&lt;TD width="76"&gt;4&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;</description>
      <pubDate>Wed, 21 Apr 2021 09:32:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/adding-values-not-in-other-table/m-p/735887#M229248</guid>
      <dc:creator>Solly7</dc:creator>
      <dc:date>2021-04-21T09:32:36Z</dc:date>
    </item>
    <item>
      <title>Re: adding values not in other table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/adding-values-not-in-other-table/m-p/735894#M229252</link>
      <description>&lt;P&gt;Can a user have different calender_month in the leave table?&lt;/P&gt;</description>
      <pubDate>Wed, 21 Apr 2021 09:59:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/adding-values-not-in-other-table/m-p/735894#M229252</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2021-04-21T09:59:09Z</dc:date>
    </item>
    <item>
      <title>Re: adding values not in other table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/adding-values-not-in-other-table/m-p/735899#M229253</link>
      <description>&lt;P&gt;Not sure why in your have data there is also calendar_month 202&lt;FONT color="#993300"&gt;&lt;STRONG&gt;0&lt;/STRONG&gt;&lt;/FONT&gt;01 but in your desired data there is only 202&lt;FONT color="#993300"&gt;&lt;STRONG&gt;1&lt;/STRONG&gt;&lt;/FONT&gt;01.&lt;/P&gt;
&lt;P&gt;Check below code. It shouldn't be hard to change in case you only want a table for the most recent calendar month in your data or you need to exclude users from your table that don't exist yet/anymore for an specific calendar month (would require from/to columns in your users table).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data users;
  input users $50.;
  datalines;
Andrew Lup
Dan Louis
Mike Mora
Farren Farrao
Joseph Mia
;

data leave;
  input calender_month leave_days leave_type_code username $50.;
  datalines;
202101 1 2 Andrew Lup
202101 1 2 Andrew Lup
202101 1 2 Sol Sebe
202001 1 2 Ant Joe
202101 1 2 Dan Louis
202001 1 2 Mike Mora
202001 1 2 Mike Mora
202001 1 2 Mike Mora
202001 1 2 Mike Mora
;

proc sql;
  select 
    l2.username,
    l2.calender_month,
    coalesce(r2.leave_days,0) as leave_days
  from
    (  
      select 
        l1.users as username,
        r1.calender_month
      from 
        users l1,
        (select distinct calender_month from leave) r1
    ) l2
    left join
    (
      select
        username,
        calender_month,
        sum(leave_days) as leave_days
      from leave
      group by username, calender_month
    ) r2
    on l2.username=r2.username and l2.calender_month=r2.calender_month
  ;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1618999436408.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/58446i841CBA19CE0BDDD2/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_0-1618999436408.png" alt="Patrick_0-1618999436408.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 21 Apr 2021 10:05:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/adding-values-not-in-other-table/m-p/735899#M229253</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2021-04-21T10:05:36Z</dc:date>
    </item>
    <item>
      <title>Re: adding values not in other table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/adding-values-not-in-other-table/m-p/735903#M229257</link>
      <description>Yes, i just use 0nly 202101 as an example</description>
      <pubDate>Wed, 21 Apr 2021 10:37:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/adding-values-not-in-other-table/m-p/735903#M229257</guid>
      <dc:creator>Solly7</dc:creator>
      <dc:date>2021-04-21T10:37:35Z</dc:date>
    </item>
    <item>
      <title>Re: adding values not in other table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/adding-values-not-in-other-table/m-p/735906#M229260</link>
      <description>Hi, code is not working..sorry im still a learner..im just confused by alias names 'l2,l1,r1,r2' from the solution you have provided..and one more thing the calender month values is only 202101, i just made typing error..i only provided 202101 for example purposes</description>
      <pubDate>Wed, 21 Apr 2021 10:50:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/adding-values-not-in-other-table/m-p/735906#M229260</guid>
      <dc:creator>Solly7</dc:creator>
      <dc:date>2021-04-21T10:50:37Z</dc:date>
    </item>
    <item>
      <title>Re: adding values not in other table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/adding-values-not-in-other-table/m-p/736261#M229324</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/296078"&gt;@Solly7&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Hi, code is not working..sorry im still a learner..im just confused by alias names 'l2,l1,r1,r2' from the solution you have provided..and one more thing the calender month values is only 202101, i just made typing error..i only provided 202101 for example purposes&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You've marked my previous post as solution so I assume you made it work now.&lt;/P&gt;
&lt;P&gt;"code is not working" doesn't tell me much. What is not working? Does it throw an error or just not return the desired result.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To get a more "step by step" view what the SQL does you could also formulate it as below.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data users;
  input users $50.;
  datalines;
Andrew Lup
Dan Louis
Mike Mora
Farren Farrao
Joseph Mia
;

data leave;
  input calender_month leave_days leave_type_code username $50.;
  datalines;
202101 1 2 Andrew Lup
202101 1 2 Andrew Lup
202101 1 2 Sol Sebe
202101 1 2 Ant Joe
202101 1 2 Dan Louis
202101 1 2 Mike Mora
202101 1 2 Mike Mora
202101 1 2 Mike Mora
202101 1 2 Mike Mora
;

proc sql;
  create view distinct_calendar_mt as
    select distinct 
      calender_month 
    from leave
    ;
quit;
proc sql;
  create view user_per_calender_mth as
    select 
      l.users as username,
      r.calender_month
    from 
      users l,
      distinct_calendar_mt r
    ; 
quit;

proc sql;
  create view sum_leave_days_per_user as
    select
      username,
      calender_month,
      sum(leave_days) as leave_days
    from leave
    group by username, calender_month
    ;
quit;

proc sql;
  create table want as
  select 
    l.username,
    l.calender_month,
    coalesce(r.leave_days,0) as leave_days
  from
    user_per_calender_mth l
    left join
    sum_leave_days_per_user r
    on 
      l.username=r.username 
      and l.calender_month=r.calender_month
    ;
quit;

proc print data=want noobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Think of a SQL view as encapsulated SQL code that only gets executed when the view gets used. In looking at it this way it's only the very last SQL (creating table WANT) that actually executes all the SQL code - and then if you would in this last SQL replace the view names with the SQL creating the views you'd end up pretty much with the SQL I've posted initially.&lt;/P&gt;
&lt;P&gt;Splitting things up gives you the opportunity to look at the intermediary results (the views) and though makes it eventually easier for you to understand what's happening/the logic used.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And just as a side note: You could have above SQL also wrapped into a single Proc SQL; Quit; block (but that makes debugging harder).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  create view distinct_calendar_mt as
    select distinct 
      calender_month 
    from leave
    ;

  create view user_per_calender_mth as
    select 
      l.users as username,
      r.calender_month
    from 
      users l,
      distinct_calendar_mt r
    ; 

  create view sum_leave_days_per_user as
    select
      username,
      calender_month,
      sum(leave_days) as leave_days
    from leave
    group by username, calender_month
    ;

  create table want as
  select 
    l.username,
    l.calender_month,
    coalesce(r.leave_days,0) as leave_days
  from
    user_per_calender_mth l
    left join
    sum_leave_days_per_user r
    on 
      l.username=r.username 
      and l.calender_month=r.calender_month
    ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 21 Apr 2021 21:27:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/adding-values-not-in-other-table/m-p/736261#M229324</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2021-04-21T21:27:46Z</dc:date>
    </item>
    <item>
      <title>Re: adding values not in other table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/adding-values-not-in-other-table/m-p/736336#M229372</link>
      <description>Thank a lot Pat..the solution you provided yesterday worked, i managed to get it right.. thanks a lot</description>
      <pubDate>Thu, 22 Apr 2021 07:27:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/adding-values-not-in-other-table/m-p/736336#M229372</guid>
      <dc:creator>Solly7</dc:creator>
      <dc:date>2021-04-22T07:27:42Z</dc:date>
    </item>
  </channel>
</rss>

