<?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: Summarizing costs by month and parallely add missing months to the sequence of months. in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Summarizing-costs-by-month-and-parallely-add-missing-months-to/m-p/637475#M189476</link>
    <description>Thank you for your quick reply. My data has years ranging from 2007- 2017. It would be lengthy for me to create many formats. I appreciate your time and help.</description>
    <pubDate>Sat, 04 Apr 2020 00:17:57 GMT</pubDate>
    <dc:creator>shasank</dc:creator>
    <dc:date>2020-04-04T00:17:57Z</dc:date>
    <item>
      <title>Summarizing costs by month and parallely add missing months to the sequence of months.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Summarizing-costs-by-month-and-parallely-add-missing-months-to/m-p/637469#M189473</link>
      <description>&lt;P&gt;Hi Community,&lt;/P&gt;&lt;P&gt;I am trying to find a way to solve an issues below mentioned.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Raw data:&lt;/P&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;ID&lt;/TD&gt;&lt;TD&gt;Month&lt;/TD&gt;&lt;TD&gt;Cost&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;Jan-07&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;Jan-07&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;Mar-07&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;May-07&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;Aug-08&lt;/TD&gt;&lt;TD&gt;6&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;Aug-08&lt;/TD&gt;&lt;TD&gt;6&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;Dec-08&lt;/TD&gt;&lt;TD&gt;13&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Logic:&lt;/P&gt;&lt;P&gt;The requirement has 2 tasks&lt;/P&gt;&lt;P&gt;1) Get the Minimum and Maximum of &lt;EM&gt;Month&lt;/EM&gt; variable by ID. &lt;U&gt;For Ex&lt;/U&gt;: for ID 1 Min is Jan-07 and Max is May-07.&lt;/P&gt;&lt;P&gt;2) Create a sequence of month-year from Minimum to Maximum.&lt;U&gt; For Ex:&lt;/U&gt; for ID 1 The output starts at&amp;nbsp;Jan-07 and ends at May-07, Even if raw data doesn't include Feb-07,April-07 the final output needs all the missing months to be added in sequence.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;3) Sum the costs by ID and month and If no costs present then assign 0.&amp;nbsp;&lt;U&gt; For Ex:&lt;/U&gt;&amp;nbsp; For ID 1 the Jan-07 costs add up to 10 and the output has 10 under the cost variables. and if not costs present to add then 0 is expected under the cells.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Output:&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt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;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am trying to solve this in Proc Sql but unable to get it work.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any suggestions.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for your time.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 04 Apr 2020 00:15:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Summarizing-costs-by-month-and-parallely-add-missing-months-to/m-p/637469#M189473</guid>
      <dc:creator>shasank</dc:creator>
      <dc:date>2020-04-04T00:15:59Z</dc:date>
    </item>
    <item>
      <title>Re: Summarizing costs by month and parallely add missing months to the sequence of months.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Summarizing-costs-by-month-and-parallely-add-missing-months-to/m-p/637473#M189474</link>
      <description>&lt;P&gt;Hi Shasank,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am not sure if I understood what you wanted exactly, but I tried something that reached the same output as you wanted. Hope this helps!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data cost;
input ID	Month $ 	Cost;
cards;
1	Jan-07	5
1	Jan-07	5
1	Mar-07	5
1	May-07	5
2	Aug-08	6
2	Aug-08	6
2	Dec-08	13
;
run;

proc format;
value mth 1='Jan-07'
		  2='Feb-07'
		  3='Mar-07'
		  4='Apr-07'
		  5='May-07'
		  8='Aug-08'
		  9='Sep-08'
		10='Oct-08'
	    11='Nov-08'
		12='Dec-08';
run;

data cmonth (drop=i);
do i = 1 to 12;
if i le 5 then do;
	month1=i;
	id=1;
	output;
end;
else if i ge 8 then do;
	month1=i;
	id=2;
	output;
	end;
end;
run;

proc sql;
create table mreport as 
select A.ID, A.month1 format=mth. as month, sum(B.cost) as cost
from cmonth as A full join cost as B
on A.ID=B.ID and A.month1=month(input(cats('01',substr(B.month, 1,3),'1960'), date9.))
group by a.id, month1;
update mreport 
set cost=case when cost=. then 0 else cost end;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 03 Apr 2020 23:52:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Summarizing-costs-by-month-and-parallely-add-missing-months-to/m-p/637473#M189474</guid>
      <dc:creator>JIX</dc:creator>
      <dc:date>2020-04-03T23:52:08Z</dc:date>
    </item>
    <item>
      <title>Re: Summarizing costs by month and parallely add missing months to the sequence of months.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Summarizing-costs-by-month-and-parallely-add-missing-months-to/m-p/637474#M189475</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/110575"&gt;@shasank&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;


data have;
input ID	Month :monyy7.	Cost;
format month monyy7.;
cards;
1	Jan-07	5
1	Jan-07	5
1	Mar-07	5
1	May-07	5
2	Aug-08	6
2	Aug-08	6
2	Dec-08	13
;
run;

data want;
 if _n_=1 then do;
   dcl hash H () ;
   h.definekey  ("month") ;
   h.definedata ("cost") ;
   h.definedone () ;
 end;
 do until(last.id);
  set have;
  by id month;
  if first.month then s=0;
  s=sum(cost,s);
  if last.month then h.add(key:month,data:s);
  _min=min(_min,month);
  _max=max(_max,month);
 end;
 month=_min;
 do while(month&amp;lt;=_max);
  if h.find() ne 0 then cost=0;
  output;
  month=intnx('mon',month,1);
 end;
 h.clear();
 drop s _:;
run;
 &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 04 Apr 2020 00:09:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Summarizing-costs-by-month-and-parallely-add-missing-months-to/m-p/637474#M189475</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-04-04T00:09:55Z</dc:date>
    </item>
    <item>
      <title>Re: Summarizing costs by month and parallely add missing months to the sequence of months.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Summarizing-costs-by-month-and-parallely-add-missing-months-to/m-p/637475#M189476</link>
      <description>Thank you for your quick reply. My data has years ranging from 2007- 2017. It would be lengthy for me to create many formats. I appreciate your time and help.</description>
      <pubDate>Sat, 04 Apr 2020 00:17:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Summarizing-costs-by-month-and-parallely-add-missing-months-to/m-p/637475#M189476</guid>
      <dc:creator>shasank</dc:creator>
      <dc:date>2020-04-04T00:17:57Z</dc:date>
    </item>
    <item>
      <title>Re: Summarizing costs by month and parallely add missing months to the sequence of months.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Summarizing-costs-by-month-and-parallely-add-missing-months-to/m-p/637477#M189477</link>
      <description>Thank you for the quick reply. I appreciate your time and expertise. One quick clarification, Does this code have the flexibility to sum more than 1 variable. Currently its only summarizing "cost". Can I add more variables ??</description>
      <pubDate>Sat, 04 Apr 2020 00:34:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Summarizing-costs-by-month-and-parallely-add-missing-months-to/m-p/637477#M189477</guid>
      <dc:creator>shasank</dc:creator>
      <dc:date>2020-04-04T00:34:05Z</dc:date>
    </item>
    <item>
      <title>Re: Summarizing costs by month and parallely add missing months to the sequence of months.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Summarizing-costs-by-month-and-parallely-add-missing-months-to/m-p/637481#M189479</link>
      <description>&lt;P&gt;You could ho&lt;SPAN&gt;wever by defining an array and sum all the measures, albeit that's gonna complicate things. &lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Alternatively, Just get the full ID Month data first and then merge back with the original. So the nonmatches can have value of 0.&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; 
data have;
input ID	Month :monyy7.	Cost;
format month monyy7.;
cards;
1	Jan-07	5
1	Jan-07	5
1	Mar-07	5
1	May-07	5
2	Aug-08	6
2	Aug-08	6
2	Dec-08	13
;
run;
data want;
 do until(last.id);
  set have;
  by id;
  _min=min(_min,month);
  _max=max(_max,month);
 end;
 month=_min;
 do while(month&amp;lt;=_max);
  output;
  month=intnx('mon',month,1);
 end;
 keep id month;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Finally, once you have the merged complete data in place, then you are all set for a simple proc summary. So,&lt;STRONG&gt; use the above result to merge back with the original&lt;/STRONG&gt; and then summarize using proc summary or SQL whichever is convenient for you.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 04 Apr 2020 01:16:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Summarizing-costs-by-month-and-parallely-add-missing-months-to/m-p/637481#M189479</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-04-04T01:16:30Z</dc:date>
    </item>
    <item>
      <title>Re: Summarizing costs by month and parallely add missing months to the sequence of months.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Summarizing-costs-by-month-and-parallely-add-missing-months-to/m-p/637524#M189493</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input ID	Month :monyy7.	Cost;
format month monyy7.;
cards;
1	Jan-07	5
1	Jan-07	5
1	Mar-07	5
1	May-07	5
2	Aug-08	6
2	Aug-08	6
2	Dec-08	13
;
run;
proc summary data=have ;
by id month;
var cost;
output out=temp sum=;
run;
data want;
 merge temp temp(keep=id month rename=(id=_id month=_month) firstobs=2);
output;
if id=_id then do;
  do i=1 to intck('month',month,_month)-1;
    month=intnx('month',month,1);cost=0;output;
  end;
end;
drop i _:;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 04 Apr 2020 11:09:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Summarizing-costs-by-month-and-parallely-add-missing-months-to/m-p/637524#M189493</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2020-04-04T11:09:16Z</dc:date>
    </item>
    <item>
      <title>Re: Summarizing costs by month and parallely add missing months to the sequence of months.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Summarizing-costs-by-month-and-parallely-add-missing-months-to/m-p/637750#M189609</link>
      <description>Thank you for your time and reply.</description>
      <pubDate>Sun, 05 Apr 2020 22:59:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Summarizing-costs-by-month-and-parallely-add-missing-months-to/m-p/637750#M189609</guid>
      <dc:creator>shasank</dc:creator>
      <dc:date>2020-04-05T22:59:50Z</dc:date>
    </item>
  </channel>
</rss>

