<?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 sum selected rows for given dates in a panel data-set in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-sum-selected-rows-for-given-dates-in-a-panel-data-set/m-p/347018#M80090</link>
    <description>How exactly?</description>
    <pubDate>Tue, 04 Apr 2017 12:43:42 GMT</pubDate>
    <dc:creator>chris2377</dc:creator>
    <dc:date>2017-04-04T12:43:42Z</dc:date>
    <item>
      <title>How sum selected rows for given dates in a panel data-set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-sum-selected-rows-for-given-dates-in-a-panel-data-set/m-p/347009#M80083</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;I have a dataset with annual sales of selected companies. I want to account for merges&amp;amp;acquisitions that took place and aggregate histrorical (pre-merge) data for both companies involved in a merge.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example, I have a following dataset with 5 companies A-E. There were two merges: &lt;BR /&gt;1. in year 3, company B took over company A (merge No. 1)&lt;BR /&gt;2. in year 4, company C took over company D (merge No. 2)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data have;
input year company $1. takeover :$1. sales;
datalines;
1 A . 100
1 B . 120
1 C . 130
1 D . 140
1 E . 150
2 A . 105
2 B . 125
2 C . 135
2 D . 145
2 E . 155
3 B A 240
3 C . 140
3 D . 150
3 E . 160
4 B . 245
4 C D 300
4 E . 170
;
run;

proc sgplot data=have;
 series x=year y=sales/group=company;
run;&lt;/PRE&gt;
&lt;P&gt;I want to get rid of the structural breaks in data for companies B and C. To do this, I want to sum sales of both companies invloved in each merge before it took place. In other words, I want to:&lt;BR /&gt;1. Sum the sales of A and B in each year 1 and 2&lt;BR /&gt;2. Sum the sales of C and D in each year 1,2 and 3&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Can you advice me how to do it in SAS?&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I was thinking of creating another variable &lt;EM&gt;merge_number&lt;/EM&gt; that would equal to:&lt;BR /&gt;- 1 for companies A and B in year 1 and 2 (to indicate the companies involved in merge No. 1)&lt;BR /&gt;- 2 for companies C and D in year 1,2 and 3 (to indicate the companies involved in merge No. 2)&lt;BR /&gt;- 0 otherwise&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;Then I could use:&lt;/P&gt;
&lt;PRE&gt;proc sql;
 create table want as
 select merge_number, sum(sales) as new_sales
 from have
 group by merge_number;
quit;&lt;/PRE&gt;
&lt;P&gt;&lt;BR /&gt;&lt;STRONG&gt;The problem is that I don't know how to create a variable&lt;EM&gt; merge_number&lt;/EM&gt;. Could you help?&lt;/STRONG&gt; &lt;STRONG&gt;Or maybe there is some other (simpler) way of summing the sales?&lt;/STRONG&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 04 Apr 2017 11:47:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-sum-selected-rows-for-given-dates-in-a-panel-data-set/m-p/347009#M80083</guid>
      <dc:creator>chris2377</dc:creator>
      <dc:date>2017-04-04T11:47:33Z</dc:date>
    </item>
    <item>
      <title>Re: How sum selected rows for given dates in a panel data-set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-sum-selected-rows-for-given-dates-in-a-panel-data-set/m-p/347012#M80086</link>
      <description>&lt;P&gt;If i have&amp;nbsp;undeerstood your scenario,if below code can help you&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sql;&lt;BR /&gt;select year,company,sum(sales) as sale_amt&lt;BR /&gt;from have&lt;BR /&gt;group by company,year&lt;BR /&gt;order by company,year;&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 04 Apr 2017 12:30:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-sum-selected-rows-for-given-dates-in-a-panel-data-set/m-p/347012#M80086</guid>
      <dc:creator>kaverisonu1</dc:creator>
      <dc:date>2017-04-04T12:30:28Z</dc:date>
    </item>
    <item>
      <title>Re: How sum selected rows for given dates in a panel data-set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-sum-selected-rows-for-given-dates-in-a-panel-data-set/m-p/347015#M80087</link>
      <description>&lt;P&gt;You can try with case statements &amp;nbsp;in proc sql for creating a&amp;nbsp;&lt;STRONG&gt;&lt;EM&gt;merge_number&amp;nbsp;&lt;/EM&gt;&lt;/STRONG&gt;&lt;EM&gt; variable&lt;/EM&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 04 Apr 2017 12:32:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-sum-selected-rows-for-given-dates-in-a-panel-data-set/m-p/347015#M80087</guid>
      <dc:creator>kaverisonu1</dc:creator>
      <dc:date>2017-04-04T12:32:24Z</dc:date>
    </item>
    <item>
      <title>Re: How sum selected rows for given dates in a panel data-set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-sum-selected-rows-for-given-dates-in-a-panel-data-set/m-p/347016#M80088</link>
      <description>No, in this way I would get the exactly the same dataset. I want to sum sales of companies A and B, C and D only</description>
      <pubDate>Tue, 04 Apr 2017 12:42:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-sum-selected-rows-for-given-dates-in-a-panel-data-set/m-p/347016#M80088</guid>
      <dc:creator>chris2377</dc:creator>
      <dc:date>2017-04-04T12:42:17Z</dc:date>
    </item>
    <item>
      <title>Re: How sum selected rows for given dates in a panel data-set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-sum-selected-rows-for-given-dates-in-a-panel-data-set/m-p/347018#M80090</link>
      <description>How exactly?</description>
      <pubDate>Tue, 04 Apr 2017 12:43:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-sum-selected-rows-for-given-dates-in-a-panel-data-set/m-p/347018#M80090</guid>
      <dc:creator>chris2377</dc:creator>
      <dc:date>2017-04-04T12:43:42Z</dc:date>
    </item>
    <item>
      <title>Re: How sum selected rows for given dates in a panel data-set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-sum-selected-rows-for-given-dates-in-a-panel-data-set/m-p/347019#M80091</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;try this&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   length sumSales 8;
   set have;
   call missing(sumSales);
   if not missing(takeover) then do;
      call execute(compbl('
      PROC SQL;
         UPDATE want 
         SET sumSales=(SELECT sum(sales) 
                       FROM have 
                       WHERE company in ("'||strip(company)||'","'||strip(takeover)||'")
                       AND year le '||strip(put(year,best.))||'
                       ) 
         WHERE company eq "'||strip(company)||'" AND takeover eq "'||strip(takeover)||'";
      QUIT;
      '));
      
   end;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 04 Apr 2017 12:51:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-sum-selected-rows-for-given-dates-in-a-panel-data-set/m-p/347019#M80091</guid>
      <dc:creator>Oligolas</dc:creator>
      <dc:date>2017-04-04T12:51:22Z</dc:date>
    </item>
    <item>
      <title>Re: How sum selected rows for given dates in a panel data-set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-sum-selected-rows-for-given-dates-in-a-panel-data-set/m-p/347026#M80093</link>
      <description>&lt;P&gt;My SAS has died at the moment, so this is just a guess, but could you not just replace the company based on the smaller dataset?&lt;/P&gt;
&lt;P&gt;E.g.;&lt;/P&gt;
&lt;PRE&gt;proc sql;
  create table WANT as 
  select  A.YEAR,
          case when B.COMPANY is not null then B.COMPANY else A.COMPANY end as COMPANY,
          A.TAKEOVER,
          A.SALES
   from   HAVE A
   left join HAVE B
   on     A.TAKEOVER=B.COMPANY
   and    A.YEAR &amp;lt; B.YEAR;
quit;&lt;/PRE&gt;</description>
      <pubDate>Tue, 04 Apr 2017 13:03:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-sum-selected-rows-for-given-dates-in-a-panel-data-set/m-p/347026#M80093</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-04-04T13:03:19Z</dc:date>
    </item>
    <item>
      <title>Re: How sum selected rows for given dates in a panel data-set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-sum-selected-rows-for-given-dates-in-a-panel-data-set/m-p/347033#M80096</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/45151"&gt;@RW9&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/77163"&gt;@Oligolas&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks for your answers, but they don't produce what I need.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would like to have a following dataset at the end:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
	input Year new_company :$2. new_sales ;
	datalines;
1 AB 220
1 CD 270
1 E 150
2 AB 230
2 CD 280
2 E 155
3 AB 240
3 CD 290
3 E 160
4 AB 245
4 CD 300
4 E 170
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;where AB means sales of synthetic&amp;nbsp;company combined of A and B in each year (e.g. 100+120=220) and CD - joint sales of C and D (e.g. 270=130+140). Of course, starting from year3, sales of AB=sales of B and in year4 sales of CD=sales of C as companies formally merged.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 04 Apr 2017 13:28:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-sum-selected-rows-for-given-dates-in-a-panel-data-set/m-p/347033#M80096</guid>
      <dc:creator>chris2377</dc:creator>
      <dc:date>2017-04-04T13:28:48Z</dc:date>
    </item>
    <item>
      <title>Re: How sum selected rows for given dates in a panel data-set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-sum-selected-rows-for-given-dates-in-a-panel-data-set/m-p/347039#M80099</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;Well, I still dont have SAS to try this out but my idea should still work. &amp;nbsp;The idea is to combine data from rows with takeover, with all the other companies in that pair, and creating a code from that:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;proc sql;
  create table WANT as 
  select  A.YEAR,
          case when B.COMPANY is not null then cats(A.COMPANY,B.COMPANY) else A.COMPANY end as COMPANY,
          A.TAKEOVER,
          A.SALES
   from   HAVE A
   left join HAVE B
   on     A.TAKEOVER=B.COMPANY
   and    A.YEAR &amp;lt; B.YEAR;
quit;&lt;/PRE&gt;
&lt;P&gt;What the idea is to get your original data to look something like:&lt;/P&gt;
&lt;PRE&gt;1 AD . 100
1 B . 120
1 CD . 130
1 CD . 140
1 E . 150
2 AB . 105
2 B . 125
2 CD . 135
2 CD . 145
2 E . 155
3 AB A 240
3 CD . 140
3 CD . 150
3 E . 160
4 B . 245
4 CD D 300
4 E . 170&lt;/PRE&gt;
&lt;P&gt;Which you can then sum up as normal.&lt;/P&gt;</description>
      <pubDate>Tue, 04 Apr 2017 13:47:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-sum-selected-rows-for-given-dates-in-a-panel-data-set/m-p/347039#M80099</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-04-04T13:47:46Z</dc:date>
    </item>
    <item>
      <title>Re: How sum selected rows for given dates in a panel data-set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-sum-selected-rows-for-given-dates-in-a-panel-data-set/m-p/347051#M80102</link>
      <description>&lt;P&gt;You can use the merge column post the code to sum the sales accordingly.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;data have;&lt;BR /&gt;input year company $1. takeover :$1. sales;&lt;BR /&gt;datalines;&lt;BR /&gt;1 A . 100&lt;BR /&gt;1 B . 120&lt;BR /&gt;1 C . 130&lt;BR /&gt;1 D . 140&lt;BR /&gt;1 E . 150&lt;BR /&gt;2 A . 105&lt;BR /&gt;2 B . 125&lt;BR /&gt;2 C . 135&lt;BR /&gt;2 D . 145&lt;BR /&gt;2 E . 155&lt;BR /&gt;3 B A 240&lt;BR /&gt;3 C . 140&lt;BR /&gt;3 D . 150&lt;BR /&gt;3 E . 160&lt;BR /&gt;4 B . 245&lt;BR /&gt;4 C D 300&lt;BR /&gt;4 E . 170&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;data have1;&lt;BR /&gt;set have;&lt;BR /&gt;select;&lt;BR /&gt;when(year=1 and company in('A','B'))do;&lt;BR /&gt;merge="AandB in 1";&lt;BR /&gt;end;&lt;BR /&gt;when(year=1 and company in ('C','D'))do;&lt;BR /&gt;merge="CandD in 2";&lt;BR /&gt;end;&lt;BR /&gt;when(year=1 and company="E") do;&lt;BR /&gt;merge="E in 1";&lt;BR /&gt;end;&lt;BR /&gt;when(year=2 and company in('A','B'))do;&lt;BR /&gt;merge="AandB in 2";&lt;BR /&gt;end;&lt;BR /&gt;when(year=2 and company in ('C','D'))do;&lt;BR /&gt;merge="CandD in 2";&lt;BR /&gt;end;&lt;BR /&gt;when(year=2 and company='E')do;&lt;BR /&gt;merge="E in 2";&lt;BR /&gt;end;&lt;BR /&gt;otherwise;&lt;BR /&gt;end;&lt;/P&gt;</description>
      <pubDate>Tue, 04 Apr 2017 14:33:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-sum-selected-rows-for-given-dates-in-a-panel-data-set/m-p/347051#M80102</guid>
      <dc:creator>kaverisonu1</dc:creator>
      <dc:date>2017-04-04T14:33:54Z</dc:date>
    </item>
    <item>
      <title>Re: How sum selected rows for given dates in a panel data-set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-sum-selected-rows-for-given-dates-in-a-panel-data-set/m-p/347056#M80106</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/45151"&gt;@RW9&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Ok, I think I get the idea now.&lt;SPAN&gt;The problem is that with the code I don't get the "new" company name (the one that "cats(A.COMPANY,B.COMPANY)" should produce) - see the attached file. Any idea why?&lt;/SPAN&gt;&lt;/P&gt;&lt;BR /&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/13669i16496CA01C46A242/image-size/large?v=1.0&amp;amp;px=600" border="0" alt="results.jpg" title="results.jpg" /&gt;</description>
      <pubDate>Tue, 04 Apr 2017 14:38:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-sum-selected-rows-for-given-dates-in-a-panel-data-set/m-p/347056#M80106</guid>
      <dc:creator>chris2377</dc:creator>
      <dc:date>2017-04-04T14:38:16Z</dc:date>
    </item>
    <item>
      <title>Re: How sum selected rows for given dates in a panel data-set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-sum-selected-rows-for-given-dates-in-a-panel-data-set/m-p/347059#M80108</link>
      <description>&lt;P&gt;Right, got SAS back, this should work:&lt;/P&gt;
&lt;PRE&gt;data have;
input year company $1. takeover :$1. sales;
datalines;
1 A . 100
1 B . 120
1 C . 130
1 D . 140
1 E . 150
2 A . 105
2 B . 125
2 C . 135
2 D . 145
2 E . 155
3 B A 240
3 C . 140
3 D . 150
3 E . 160
4 B . 245
4 C D 300
4 E . 170
;
run;

proc sql;
  create table WANT as
  select  A.YEAR,
          case when B.CMP is not null then B.CMP else A.COMPANY end as COMPANY,
          A.SALES
  from    HAVE A
  left join ( 
              select  YEAR,
                      CATS(COMPANY,TAKEOVER) as CMP,
                      TAKEOVER,
                      SALES
               from   HAVE
               where  TAKEOVER is not null
            ) B
  on      index(B.CMP,A.COMPANY)
  order by YEAR,
           COMPANY;
quit;&lt;/PRE&gt;</description>
      <pubDate>Tue, 04 Apr 2017 14:52:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-sum-selected-rows-for-given-dates-in-a-panel-data-set/m-p/347059#M80108</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-04-04T14:52:18Z</dc:date>
    </item>
    <item>
      <title>Re: How sum selected rows for given dates in a panel data-set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-sum-selected-rows-for-given-dates-in-a-panel-data-set/m-p/347076#M80119</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/45151"&gt;@RW9&lt;/a&gt;&amp;nbsp;Thanks, now it works.&lt;/P&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/129433"&gt;@kaverisonu1&lt;/a&gt;&amp;nbsp;Thanks, I guess that's another way of doing it,&amp;nbsp;though some fine-tuning would be neccesary, as the output dataset is still a bit different from&amp;nbsp;the one I need.&lt;/P&gt;</description>
      <pubDate>Tue, 04 Apr 2017 15:23:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-sum-selected-rows-for-given-dates-in-a-panel-data-set/m-p/347076#M80119</guid>
      <dc:creator>chris2377</dc:creator>
      <dc:date>2017-04-04T15:23:32Z</dc:date>
    </item>
    <item>
      <title>Re: How sum selected rows for given dates in a panel data-set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-sum-selected-rows-for-given-dates-in-a-panel-data-set/m-p/347086#M80124</link>
      <description>&lt;P&gt;Since you already know the problem cases perhaps this alternative:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;proc format library=work;
value $NewComp
'A','B' = 'BA'
'C','D' = 'CD'
;
run;

data want;
  set have;
  NewCompany=put(company,$NewComp.);
run;&lt;/PRE&gt;
&lt;P&gt;I often find that Formats are good for mapping /combining changed coded or names.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;With the format you may not even need a new data set but apply the format to the variable in the summary or display procedure that does grouping.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Such as&lt;/P&gt;
&lt;P&gt;proc summary data=have;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; class company;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; format company $newcomp. ;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; var sales;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;</description>
      <pubDate>Tue, 04 Apr 2017 15:46:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-sum-selected-rows-for-given-dates-in-a-panel-data-set/m-p/347086#M80124</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-04-04T15:46:07Z</dc:date>
    </item>
  </channel>
</rss>

