<?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: how to find missing months up to four months in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/how-to-find-missing-months-up-to-four-months/m-p/481411#M124524</link>
    <description>&lt;OL&gt;
&lt;LI&gt;set up a reference table for months&lt;/LI&gt;
&lt;LI&gt;with a cartesian join, build all possible combinations of id/month&lt;/LI&gt;
&lt;LI&gt;use that as base for a join with your original data, and set sale according to your rule&lt;/LI&gt;
&lt;/OL&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data months;
input month;
cards;
1
2
3
4
;
run;

proc sql;
create table lookup as
select distinct s.id, b.month
from
  s, months b
;
create table want as
select
  a.id,
  a.month,
  case
    when s.sale is not missing then s.sale
    else 0
  end as sale
from
  lookup a
  left join s
  on a.id=s.id and a.month=s.month
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 26 Jul 2018 10:32:08 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2018-07-26T10:32:08Z</dc:date>
    <item>
      <title>how to find missing months up to four months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-find-missing-months-up-to-four-months/m-p/481403#M124521</link>
      <description>&lt;P&gt;data s ;&lt;BR /&gt;input id $ month sale ;&lt;BR /&gt;cards ;&lt;BR /&gt;111 1 486&lt;BR /&gt;111 2 486&lt;BR /&gt;111 3 486&lt;BR /&gt;112 1 487&lt;BR /&gt;112 2 489&lt;BR /&gt;112 3 789&lt;BR /&gt;112 4 963&lt;BR /&gt;113 1 789&lt;BR /&gt;113 2 879&lt;BR /&gt;114 1 589&lt;BR /&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;actually 111 id hasn't 4th month like that how to find missing months and assign zero value to sale variable&lt;/P&gt;</description>
      <pubDate>Thu, 26 Jul 2018 10:05:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-find-missing-months-up-to-four-months/m-p/481403#M124521</guid>
      <dc:creator>thanikondharish</dc:creator>
      <dc:date>2018-07-26T10:05:24Z</dc:date>
    </item>
    <item>
      <title>Re: how to find missing months up to four months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-find-missing-months-up-to-four-months/m-p/481407#M124522</link>
      <description>&lt;P&gt;Please add the expected result to your post. Some assumptions made:&lt;/P&gt;
&lt;P&gt;- only months at the end are missing&lt;/P&gt;
&lt;P&gt;- not only the fourth obs is added, but up to 3, so that there are four obs per id&lt;/P&gt;
&lt;P&gt;- the source-data is sorted by id and month&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   set have;
   by id;

   output;

   if last.id and month &amp;lt; 4 then do;
      do month = month + 1 to 4;
         sale = 0;
         output;
      end;
   end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 26 Jul 2018 10:20:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-find-missing-months-up-to-four-months/m-p/481407#M124522</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2018-07-26T10:20:58Z</dc:date>
    </item>
    <item>
      <title>Re: how to find missing months up to four months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-find-missing-months-up-to-four-months/m-p/481408#M124523</link>
      <description>&lt;P&gt;Assuming your data is in order as you have indicated:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;set have;&lt;/P&gt;
&lt;P&gt;by id;&lt;/P&gt;
&lt;P&gt;output;&lt;/P&gt;
&lt;P&gt;if last.id;&lt;/P&gt;
&lt;P&gt;sale = 0;&lt;/P&gt;
&lt;P&gt;if month &amp;lt; 4 then do month = month + 1 to 4;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; output;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A few seconds apart ... great minds think alike.&lt;/P&gt;</description>
      <pubDate>Thu, 26 Jul 2018 10:22:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-find-missing-months-up-to-four-months/m-p/481408#M124523</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-07-26T10:22:48Z</dc:date>
    </item>
    <item>
      <title>Re: how to find missing months up to four months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-find-missing-months-up-to-four-months/m-p/481411#M124524</link>
      <description>&lt;OL&gt;
&lt;LI&gt;set up a reference table for months&lt;/LI&gt;
&lt;LI&gt;with a cartesian join, build all possible combinations of id/month&lt;/LI&gt;
&lt;LI&gt;use that as base for a join with your original data, and set sale according to your rule&lt;/LI&gt;
&lt;/OL&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data months;
input month;
cards;
1
2
3
4
;
run;

proc sql;
create table lookup as
select distinct s.id, b.month
from
  s, months b
;
create table want as
select
  a.id,
  a.month,
  case
    when s.sale is not missing then s.sale
    else 0
  end as sale
from
  lookup a
  left join s
  on a.id=s.id and a.month=s.month
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 26 Jul 2018 10:32:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-find-missing-months-up-to-four-months/m-p/481411#M124524</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-07-26T10:32:08Z</dc:date>
    </item>
  </channel>
</rss>

