<?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: Aggregate Value with By Variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Aggregate-Value-with-By-Variable/m-p/349772#M81216</link>
    <description>&lt;P&gt;The proc summary part is easy. The qtr format required a bit more work:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data have;
  informat StartDate1 StartDate2 date9.;
  format StartDate1 StartDate2 date9.;
  input measure $ StartDate1 Measure2 $ StartDate2 Nu1 de1 Nu2 De2;
  cards;
A 1jul2016 A 1oct2016 3 10 4 10
A 1aug2016 A 1dec2016 5 20 2 20
B 1jul2016 B 1nov2016 1 10 5 20
B 1sep2016 B 1oct2016 1 10 4 40
;

proc fcmp outlib=work.functions.smd; 

   function qfmt(date) $;
      length yyq4 $4;
      yyq4=put(date,yyq4.);
      if substr(yyq4,3,1)='Q'
         then return(substr(yyq4,3,2));
      else return(yyq4);
   endsub;
run;

/* Specifies one or more SAS data sets that contain compiler subroutines */
/* to include during program compilation.                                */
options cmplib=(work.functions);

/* Specify the name of the function created above as the label followed by */
/* parentheses.                                                            */
proc format; 
 value qfmt other=[qfmt()]; 
run;

proc summary data=have nway;
  class Measure StartDate1;
  format StartDate1 Qfmt.;
  var Nu1 De1;
  output out=need1 (drop=_:) sum=/autoname;
run;

proc summary data=have nway;
  class Measure2 StartDate2;
  format StartDate2 Qfmt.;
  var Nu2 De2;
  output out=need2 (drop=_:) sum=/autoname;
run;

data want;
  retain measure StartDate1 Measure2 StartDate2; 
  set need1;
  set need2;
run;
&lt;/PRE&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;</description>
    <pubDate>Thu, 13 Apr 2017 14:49:28 GMT</pubDate>
    <dc:creator>art297</dc:creator>
    <dc:date>2017-04-13T14:49:28Z</dc:date>
    <item>
      <title>Aggregate Value with By Variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Aggregate-Value-with-By-Variable/m-p/349769#M81214</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am trying to get an aggregate of my numerators and denominators for two separate time periods by Measure. Each time period has 3 months of data that I need to aggregate for each measure.&lt;/P&gt;&lt;P&gt;July 2016 - Sept 2016 = Q3&lt;/P&gt;&lt;P&gt;Oct 2016 - Dec 2016 = Q4&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please see the attachment for the data set and an example of how I need to aggregate the data. &amp;nbsp;I ran a proc summary, but it's not aggregating the way that I need it to.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;BR /&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/13704i6AEFA739CA368E7C/image-size/large?v=1.0&amp;amp;px=600" border="0" alt="SASHelp.JPG" title="SASHelp.JPG" /&gt;</description>
      <pubDate>Thu, 13 Apr 2017 14:24:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Aggregate-Value-with-By-Variable/m-p/349769#M81214</guid>
      <dc:creator>thb</dc:creator>
      <dc:date>2017-04-13T14:24:45Z</dc:date>
    </item>
    <item>
      <title>Re: Aggregate Value with By Variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Aggregate-Value-with-By-Variable/m-p/349772#M81216</link>
      <description>&lt;P&gt;The proc summary part is easy. The qtr format required a bit more work:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data have;
  informat StartDate1 StartDate2 date9.;
  format StartDate1 StartDate2 date9.;
  input measure $ StartDate1 Measure2 $ StartDate2 Nu1 de1 Nu2 De2;
  cards;
A 1jul2016 A 1oct2016 3 10 4 10
A 1aug2016 A 1dec2016 5 20 2 20
B 1jul2016 B 1nov2016 1 10 5 20
B 1sep2016 B 1oct2016 1 10 4 40
;

proc fcmp outlib=work.functions.smd; 

   function qfmt(date) $;
      length yyq4 $4;
      yyq4=put(date,yyq4.);
      if substr(yyq4,3,1)='Q'
         then return(substr(yyq4,3,2));
      else return(yyq4);
   endsub;
run;

/* Specifies one or more SAS data sets that contain compiler subroutines */
/* to include during program compilation.                                */
options cmplib=(work.functions);

/* Specify the name of the function created above as the label followed by */
/* parentheses.                                                            */
proc format; 
 value qfmt other=[qfmt()]; 
run;

proc summary data=have nway;
  class Measure StartDate1;
  format StartDate1 Qfmt.;
  var Nu1 De1;
  output out=need1 (drop=_:) sum=/autoname;
run;

proc summary data=have nway;
  class Measure2 StartDate2;
  format StartDate2 Qfmt.;
  var Nu2 De2;
  output out=need2 (drop=_:) sum=/autoname;
run;

data want;
  retain measure StartDate1 Measure2 StartDate2; 
  set need1;
  set need2;
run;
&lt;/PRE&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;</description>
      <pubDate>Thu, 13 Apr 2017 14:49:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Aggregate-Value-with-By-Variable/m-p/349772#M81216</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-04-13T14:49:28Z</dc:date>
    </item>
    <item>
      <title>Re: Aggregate Value with By Variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Aggregate-Value-with-By-Variable/m-p/349774#M81218</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;First, are your dates SAS date values or not? It is very hard to tell with a picture.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If the dates are SAS date values I might start with&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc summary&amp;nbsp; data=have nway;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; class measure startdate1 measure2 startdate2;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; format startdate: yyQ. ;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; var Nu1 De1 Nu2 De2;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; output out=want (drop= _:) sum = / autoname;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;This will create Nu1_sum and similar names instead of SumNu1 though you can create the names with the syntax.&lt;/P&gt;
&lt;P&gt;Actually, for most of my uses I wouldn't even rename the variables as there is only one statistic involved, but that's your choice.&lt;/P&gt;
&lt;P&gt;I recommend keeping the Year component in the format so that you know which year the data come from. Since you show Dec 2016 data I would not be surprised to see Jan 2017 as well.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you abosultely must have Q1, Q2 etc. Proc Format will allow you to create a custom picture format that will display dates that way.&lt;/P&gt;
&lt;P&gt;If your dates are not SAS date variables then you need a data step that will involve something like;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; set have;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; startdate1 = cats('Q', put(input(startdate1,date9.),QTR1.));&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; startdate2 = cats('Q', put(input(startdate2,date9.),QTR1.));&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;And use the proc summary code without the format.&lt;/P&gt;</description>
      <pubDate>Thu, 13 Apr 2017 15:02:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Aggregate-Value-with-By-Variable/m-p/349774#M81218</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-04-13T15:02:24Z</dc:date>
    </item>
    <item>
      <title>Re: Aggregate Value with By Variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Aggregate-Value-with-By-Variable/m-p/349787#M81225</link>
      <description>&lt;P&gt;data have;&lt;BR /&gt;informat StartDate1 StartDate2 date9.;&lt;BR /&gt;format StartDate1 StartDate2 date9.;&lt;BR /&gt;input measure $ StartDate1 Measure2 $ StartDate2 Nu1 de1 Nu2 De2;&lt;BR /&gt;cards;&lt;BR /&gt;A 1jul2016 A 1oct2016 3 10 4 10&lt;BR /&gt;A 1aug2016 A 1dec2016 5 20 2 20&lt;BR /&gt;B 1jul2016 B 1nov2016 1 10 5 20&lt;BR /&gt;B 1sep2016 B 1oct2016 1 10 4 40&lt;BR /&gt;;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;data temp;&lt;BR /&gt;set have;&lt;BR /&gt;_StartDate1='Q'||left(qtr(StartDate1));&lt;BR /&gt;_StartDate2='Q'||left(qtr(StartDate2));&lt;BR /&gt;rename _StartDate1= StartDate1;&lt;BR /&gt;rename _StartDate2= StartDate2;&lt;BR /&gt;drop start:;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data want;&lt;BR /&gt;set temp;&lt;BR /&gt;by measure;&lt;BR /&gt;if first.measure then do;&lt;BR /&gt;SumNu1=0;&lt;BR /&gt;Sumde1=0;&lt;BR /&gt;Sumnu2=0;&lt;BR /&gt;Sumde2=0;&lt;BR /&gt;end;&lt;BR /&gt;SumNu1+nu1;&lt;BR /&gt;Sumde1+de1;&lt;BR /&gt;sumnu2+de2;&lt;BR /&gt;sumde2+de2;&lt;BR /&gt;if last.measure;&lt;BR /&gt;drop nu1 de1 nu2 de2;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Naveen Srinivasan&lt;/P&gt;</description>
      <pubDate>Thu, 13 Apr 2017 15:57:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Aggregate-Value-with-By-Variable/m-p/349787#M81225</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2017-04-13T15:57:54Z</dc:date>
    </item>
    <item>
      <title>Re: Aggregate Value with By Variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Aggregate-Value-with-By-Variable/m-p/349817#M81238</link>
      <description>&lt;P&gt;Hi&lt;/P&gt;&lt;P&gt;Can anyone suggest a proc sql solution for the same problem ?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance&lt;/P&gt;</description>
      <pubDate>Thu, 13 Apr 2017 17:32:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Aggregate-Value-with-By-Variable/m-p/349817#M81238</guid>
      <dc:creator>soham_sas</dc:creator>
      <dc:date>2017-04-13T17:32:41Z</dc:date>
    </item>
    <item>
      <title>Re: Aggregate Value with By Variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Aggregate-Value-with-By-Variable/m-p/351617#M81851</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;someone please suggest a PROC SQL solutions for this issue , &amp;nbsp;i am asking for my knowledge purpose only .&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 20 Apr 2017 11:28:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Aggregate-Value-with-By-Variable/m-p/351617#M81851</guid>
      <dc:creator>soham_sas</dc:creator>
      <dc:date>2017-04-20T11:28:30Z</dc:date>
    </item>
  </channel>
</rss>

