<?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: Calculating Monthly subtotals in EG in SAS Enterprise Guide</title>
    <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Calculating-Monthly-subtotals-in-EG/m-p/38998#M4693</link>
    <description>I think that you may need to give us a bit more information about what you are doing and what method you are using to create your sub-totals.&lt;BR /&gt;
&lt;BR /&gt;
SAS is fantastic at being able to group stuff for sub-totals by various aspects of a date (ie Month, Month/Year, Quarter, Day of Week etc.). Your biggest hurdle is really to make sure that your data has its date value stored as proper SAS dates (ie not just text values that look like dates).&lt;BR /&gt;
&lt;BR /&gt;
So go on, give us some more information........are you new to SAS and only use SAS/EG or are you someone who is more familiar with writing SAS code but are currently using SAS/EG ?&lt;BR /&gt;
&lt;BR /&gt;
Some typical data and examples of what you hope to for as results may not go amiss in any reply, it helps people to see what you see.&lt;BR /&gt;
&lt;BR /&gt;
Cheers,&lt;BR /&gt;
&lt;BR /&gt;
Down-Under-Dave&lt;BR /&gt;
Wellington&lt;BR /&gt;
New Zealand</description>
    <pubDate>Wed, 13 Aug 2008 23:57:05 GMT</pubDate>
    <dc:creator>DaveShea</dc:creator>
    <dc:date>2008-08-13T23:57:05Z</dc:date>
    <item>
      <title>Calculating Monthly subtotals in EG</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Calculating-Monthly-subtotals-in-EG/m-p/38997#M4692</link>
      <description>I just got through figuring out how to do subtotals in general, and now I have come across the problem of needing to compute monthly subtotals.  Again I do not believe that there is a SAS function that computes these numbers, but there has to be some way to calculate this.  Does anyone know how this could be done?</description>
      <pubDate>Wed, 13 Aug 2008 22:50:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Calculating-Monthly-subtotals-in-EG/m-p/38997#M4692</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2008-08-13T22:50:12Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating Monthly subtotals in EG</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Calculating-Monthly-subtotals-in-EG/m-p/38998#M4693</link>
      <description>I think that you may need to give us a bit more information about what you are doing and what method you are using to create your sub-totals.&lt;BR /&gt;
&lt;BR /&gt;
SAS is fantastic at being able to group stuff for sub-totals by various aspects of a date (ie Month, Month/Year, Quarter, Day of Week etc.). Your biggest hurdle is really to make sure that your data has its date value stored as proper SAS dates (ie not just text values that look like dates).&lt;BR /&gt;
&lt;BR /&gt;
So go on, give us some more information........are you new to SAS and only use SAS/EG or are you someone who is more familiar with writing SAS code but are currently using SAS/EG ?&lt;BR /&gt;
&lt;BR /&gt;
Some typical data and examples of what you hope to for as results may not go amiss in any reply, it helps people to see what you see.&lt;BR /&gt;
&lt;BR /&gt;
Cheers,&lt;BR /&gt;
&lt;BR /&gt;
Down-Under-Dave&lt;BR /&gt;
Wellington&lt;BR /&gt;
New Zealand</description>
      <pubDate>Wed, 13 Aug 2008 23:57:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Calculating-Monthly-subtotals-in-EG/m-p/38998#M4693</guid>
      <dc:creator>DaveShea</dc:creator>
      <dc:date>2008-08-13T23:57:05Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating Monthly subtotals in EG</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Calculating-Monthly-subtotals-in-EG/m-p/38999#M4694</link>
      <description>Hi:&lt;BR /&gt;
  It really depends on what your data looks like and what EG task you're using to calculate the subtotals and what outcome you want (data set or report).&lt;BR /&gt;
  &lt;BR /&gt;
  Most SAS procedures and/or EG tasks have a way to "group" observations. So, for example, if your data has a MONTH variable or a DATE variable, then you could select "GROUP BY" in your task and specify the MONTH variable. &lt;BR /&gt;
     &lt;BR /&gt;
  Another thing to consider is -- do you want monthly subtotals in a SAS data set or do you want a REPORT (like HTML or RTF report) with the subtotals???&lt;BR /&gt;
  Whether there's a function that you could use depends on your data. Generally speaking functions work on a row by row basis. So if your data had these variables:&lt;BR /&gt;
[pre]&lt;BR /&gt;
product month day1 day2 day3 day4.... &lt;BR /&gt;
shoes   jan    100  150  175  100.... &lt;BR /&gt;
shoes   feb     99  100   60  100....&lt;BR /&gt;
shirts  jan    120  130  155  150.... &lt;BR /&gt;
shirts  feb     89  120   60  150....&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
Then you could use the SUM function for every row:&lt;BR /&gt;
[pre]&lt;BR /&gt;
montot = sum (of day1-day31);&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
(even if a month did not have 31 days, missing values for specific days would be ignored with the sum function.)&lt;BR /&gt;
&lt;BR /&gt;
On the other hand, if your data was structured like this:&lt;BR /&gt;
[pre]&lt;BR /&gt;
product mon day amt&lt;BR /&gt;
shoes   jan  1  100  &lt;BR /&gt;
shoes   jan  2  150  &lt;BR /&gt;
shoes   jan  3  175  &lt;BR /&gt;
shoes   jan  4  100  &lt;BR /&gt;
shoes   feb  1   99  &lt;BR /&gt;
shoes   feb  2  100   &lt;BR /&gt;
shoes   feb  3   60  &lt;BR /&gt;
shoes   feb  4  100 &lt;BR /&gt;
shirts  jan  1  120  &lt;BR /&gt;
shirts  jan  2  130  &lt;BR /&gt;
shirts  jan  3  155  &lt;BR /&gt;
shirts  jan  4  150  &lt;BR /&gt;
shirts  feb  1   89  &lt;BR /&gt;
shirts  feb  2  120   &lt;BR /&gt;
shirts  feb  3   60  &lt;BR /&gt;
shirts  feb  4  150&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
Then the SUM function would not be appropriate -- you'd have to use one of the EG tasks or a SAS procedure to create a file or report with monthly subtotals. You could use the Summary Statistics task or the Summary Tables task to get a report of monthly subtotals. &lt;BR /&gt;
    &lt;BR /&gt;
If you wanted to see the "detail" rows AND the monthly subtotals in a report, then you might consider the List Data task and you would group the report  by product and month or just by month. In my fake data, I showed a separate month and day variable, although you might actually have a variable with a whole date value, like a SAS date value and in that case, you would need to apply a format in order to group by month or month and year.&lt;BR /&gt;
&lt;BR /&gt;
At any rate, it is probable that the same method you figured out to do subtotals in general can be extended to the task of computing monthly subtotals. If you are using code, then you would look for a CLASS statement or a BY statement in the syntax. If you are using an EG task, then you would look for a GROUP BY choice in your task. If you are using the Summary Tables task (which creates PROC TABULATE code), then you would drag and drop the ALL "statistic" into the appropriate place in the table when you build it with drag and drop methods.&lt;BR /&gt;
 &lt;BR /&gt;
For more help with this task, you might open a track with Tech Support so someone could walk you through the click-path you would need to follow in your task of choice to get subtotals by month.&lt;BR /&gt;
 &lt;BR /&gt;
cynthia</description>
      <pubDate>Thu, 14 Aug 2008 00:16:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Calculating-Monthly-subtotals-in-EG/m-p/38999#M4694</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2008-08-14T00:16:29Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating Monthly subtotals in EG</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Calculating-Monthly-subtotals-in-EG/m-p/39000#M4695</link>
      <description>Thanks for the help.  I created custom columns of month and day and then just added them both as the classification variables in my columns.  Then made sure subtotals were added and it worked.  It was actually really easy.&lt;BR /&gt;
&lt;BR /&gt;
On this same note, does anyone know how to do monthly running totals?  Assume that i have a date column and a profit column.  What i would want this custom column to do is continually add the previous days profit to the running total until it reaches the end of the month, at which point it would start over.  Suggestions?</description>
      <pubDate>Thu, 14 Aug 2008 14:25:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Calculating-Monthly-subtotals-in-EG/m-p/39000#M4695</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2008-08-14T14:25:01Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating Monthly subtotals in EG</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Calculating-Monthly-subtotals-in-EG/m-p/39001#M4696</link>
      <description>Hi:&lt;BR /&gt;
  Glad that helped.&lt;BR /&gt;
&lt;BR /&gt;
  If you wanted running totals, your choices are to either use a data step program to calculate the running total and add that as a column in a data set or then you could do a List Data Task that showed the running total or a procedure like PROC REPORT that would let you calculate the running total as a report column (not in the data set). &lt;BR /&gt;
&lt;BR /&gt;
  To calculate running totals in a Data step program, you would need to investigate how programs work, how the RETAIN statement works and how to use the FIRST.byvar automatic variable to reset the running total at the first of every month (or LAST.byvar). &lt;BR /&gt;
&lt;BR /&gt;
  The Data step approach is shown in this Tech Support Note:&lt;BR /&gt;
&lt;A href="http://support.sas.com/kb/19/394.html" target="_blank"&gt;http://support.sas.com/kb/19/394.html&lt;/A&gt;&lt;BR /&gt;
 &lt;BR /&gt;
  To modify the Tech Support code for BY group processing, you would only change the code with a BY statement and an IF statement. This Tech Support note illustrates creating a cumulative column based on BY group processing:&lt;BR /&gt;
&lt;A href="http://support.sas.com/kb/24/744.html" target="_blank"&gt;http://support.sas.com/kb/24/744.html&lt;/A&gt;&lt;BR /&gt;
 &lt;BR /&gt;
cynthia</description>
      <pubDate>Thu, 14 Aug 2008 15:29:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Calculating-Monthly-subtotals-in-EG/m-p/39001#M4696</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2008-08-14T15:29:58Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating Monthly subtotals in EG</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Calculating-Monthly-subtotals-in-EG/m-p/39002#M4697</link>
      <description>Ok, I have already used that second link that you have provided to set up my cumulative totals field, so I am familiar with it.  However, I am new to SAS programming, and I am not entirely sure how to go about implementing the BY and IF statements to create the MTD running totals. &lt;BR /&gt;
&lt;BR /&gt;
Here is where I am at so far:&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
  data work.calculated1;&lt;BR /&gt;
	set work.query_for_query9446;&lt;BR /&gt;
	by Month Day;&lt;BR /&gt;
	If FIRST.Month = 1 then	'Cum Total Loans Made MTD'n + 'Loans - Total (sum)'n;&lt;BR /&gt;
  run;&lt;BR /&gt;
&lt;BR /&gt;
This will separate the new field by month, but they are not calculating running totals, they are only returning the first value in the 'Loans - Total (sum)' column, and then outputting that value for every day of the month. Then it sums that value with the first value of the next month and displays that number for every observation within the following month and so on.&lt;BR /&gt;
&lt;BR /&gt;
I tried to do this same code with Day as my only byvar, but I receive error messages because it is not sorted (which it wouldn't be because the 'Day' field streams 1 through 31 and then starts over again at 1 on the new month)&lt;BR /&gt;
&lt;BR /&gt;
Any advice would be greatly appreciated. &lt;BR /&gt;
&lt;BR /&gt;
Thanks,&lt;BR /&gt;
&lt;BR /&gt;
Ben

Message was edited by: bmartin</description>
      <pubDate>Fri, 15 Aug 2008 15:31:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Calculating-Monthly-subtotals-in-EG/m-p/39002#M4697</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2008-08-15T15:31:52Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating Monthly subtotals in EG</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Calculating-Monthly-subtotals-in-EG/m-p/39003#M4698</link>
      <description>Hi:&lt;BR /&gt;
  The key is NOT to do the addition on the FIRST.MONTH condition -- all you probably want to do at FIRST.MONTH is reset the cumulative variable to 0.&lt;BR /&gt;
&lt;BR /&gt;
  Consider this data sorted by month product and day. I have added what the values for first.month and last.month will look like on every observation. So you can see that when first.month = 1 SAS is reading the first observation for the month -- at this point you want to set your month accumulator variable to 0. But when SAS is reading ROW 2, 3, 4, etc, you want to keep adding those values for AMT to your accumulator variable. In your logic, you were only adding the number for the first row of your group, which will not get you a running total for every month:&lt;BR /&gt;
[pre]&lt;BR /&gt;
product    month    day    amt    first.month  last.month    &lt;BR /&gt;
 shirts      01       1     120      1            0           &lt;BR /&gt;
 shirts      01       2     130      0            0                      &lt;BR /&gt;
 shirts      01       3     155      0            0           &lt;BR /&gt;
 shirts      01       4     150      0            0           &lt;BR /&gt;
 shoes       01       1     100      0            0&lt;BR /&gt;
 shoes       01       2     150      0            0         &lt;BR /&gt;
 shoes       01       3     175      0            0         &lt;BR /&gt;
 shoes       01       4     100      0            1   &lt;BR /&gt;
**************************change between months**********      &lt;BR /&gt;
 shirts      02       1      89      1            0           &lt;BR /&gt;
 shirts      02       2     120      0            0           &lt;BR /&gt;
 shirts      02       3      60      0            0           &lt;BR /&gt;
 shirts      02       4     150      0            0           &lt;BR /&gt;
 shoes       02       1      99      0            0          &lt;BR /&gt;
 shoes       02       2     100      0            0         &lt;BR /&gt;
 shoes       02       3      60      0            0         &lt;BR /&gt;
 shoes       02       4     100      0            1  &lt;BR /&gt;
[/pre]&lt;BR /&gt;
                                    &lt;BR /&gt;
Assume that we want to accumulate a monthly running total, a "grand" running total and for every month, we also want a running total for shirts and shoes. I need several "accumulator variables" -- MONTOT, GRNDTOT, SHIRTTOT and SHOETOT.&lt;BR /&gt;
&lt;BR /&gt;
What I want on &lt;B&gt;every&lt;/B&gt; observation is to add the AMT on that row to both MONTOT and GRNDTOT -- however, I want to &lt;B&gt;conditionally&lt;/B&gt; add AMT to either SHIRTTOT or SHOETOT, depending on the value of PRODUCT.&lt;BR /&gt;
&lt;BR /&gt;
So this program will do that:&lt;BR /&gt;
[pre]&lt;BR /&gt;
&lt;BR /&gt;
data mon_data;&lt;BR /&gt;
  infile datalines;&lt;BR /&gt;
  input product $ month $ day amt;&lt;BR /&gt;
return;&lt;BR /&gt;
datalines;&lt;BR /&gt;
shoes    01    1  100  &lt;BR /&gt;
shoes    01    2  150  &lt;BR /&gt;
shoes    01    3  175  &lt;BR /&gt;
shoes    01    4  100  &lt;BR /&gt;
shoes    02    1   99  &lt;BR /&gt;
shoes    02    2  100   &lt;BR /&gt;
shoes    02    3   60  &lt;BR /&gt;
shoes    02    4  100 &lt;BR /&gt;
shirts   01    1  120  &lt;BR /&gt;
shirts   01    2  130  &lt;BR /&gt;
shirts   01    3  155  &lt;BR /&gt;
shirts   01    4  150  &lt;BR /&gt;
shirts   02    1   89  &lt;BR /&gt;
shirts   02    2  120   &lt;BR /&gt;
shirts   02    3   60  &lt;BR /&gt;
shirts   02    4  150&lt;BR /&gt;
;&lt;BR /&gt;
run;&lt;BR /&gt;
               &lt;BR /&gt;
proc sort data=mon_data;&lt;BR /&gt;
by month product day;&lt;BR /&gt;
run;&lt;BR /&gt;
             &lt;BR /&gt;
data work.calculated1 &lt;BR /&gt;
     sumonly(keep=month montot shirttot shoetot);&lt;BR /&gt;
  set work.mon_data;&lt;BR /&gt;
  by Month;&lt;BR /&gt;
                  &lt;BR /&gt;
  ** I like to use an explicit RETAIN statement;&lt;BR /&gt;
  retain montot shirttot shoetot grndtot 0 ;&lt;BR /&gt;
               &lt;BR /&gt;
  ** Use first.month condition to set cum variables to 0;&lt;BR /&gt;
  if first.month then do;&lt;BR /&gt;
     montot = 0;&lt;BR /&gt;
     shirttot = 0;&lt;BR /&gt;
     shoetot = 0;&lt;BR /&gt;
   end;&lt;BR /&gt;
                    &lt;BR /&gt;
  ** add the amount for every observation;&lt;BR /&gt;
  montot + amt;&lt;BR /&gt;
  grndtot + amt;&lt;BR /&gt;
                    &lt;BR /&gt;
  ** add the amount for shirt and shoes separately;&lt;BR /&gt;
  if product = 'shirts' then shirttot + amt;&lt;BR /&gt;
  else if product = 'shoes' then shoetot + amt;&lt;BR /&gt;
           &lt;BR /&gt;
  ** output calculated1 -- same number of obs out as obs in;&lt;BR /&gt;
  output calculated1;&lt;BR /&gt;
         &lt;BR /&gt;
  ** output monthly totals for last.month condition;&lt;BR /&gt;
  if last.month then output sumonly;&lt;BR /&gt;
             &lt;BR /&gt;
run;&lt;BR /&gt;
             &lt;BR /&gt;
proc print data=work.calculated1;&lt;BR /&gt;
title 'see different running totals';&lt;BR /&gt;
run;&lt;BR /&gt;
                &lt;BR /&gt;
proc print data=work.sumonly;&lt;BR /&gt;
title 'summary file';&lt;BR /&gt;
run;&lt;BR /&gt;
                                                                 &lt;BR /&gt;
[/pre]&lt;BR /&gt;
                          &lt;BR /&gt;
If you were going to accumulate on DAY, then you'd use slightly different logic, but the basic idea is &lt;BR /&gt;
the same -- you use FIRST.byvar to initialize or reset your accumulator variable to 0. Then you just add your numbers on every row.&lt;BR /&gt;
&lt;BR /&gt;
As you can see with SHIRTTOT and SHOETOT -- at some point, the accumulator variable value just repeats on every row because there are no new values to add to the number. They're not worth much as running totals, but if you want to make a summary data set of ONLY monthly totals without products or days -- like SUMONLY -- then they do serve a purpose. By the time the LAST.MONTH observation is being held in the input buffer you have all the information you need to output MONTH, MONTOT, SHIRTTOT and SHOETOT. The program output is shown below. &lt;BR /&gt;
 &lt;BR /&gt;
cynthia&lt;BR /&gt;
&lt;BR /&gt;
[pre]&lt;BR /&gt;
see different running totals&lt;BR /&gt;
                  &lt;BR /&gt;
Obs    product    month    day    amt    montot    shirttot    shoetot    grndtot&lt;BR /&gt;
                              &lt;BR /&gt;
  1    shirts      01       1     120      120        120          0         120&lt;BR /&gt;
  2    shirts      01       2     130      250        250          0         250&lt;BR /&gt;
  3    shirts      01       3     155      405        405          0         405&lt;BR /&gt;
  4    shirts      01       4     150      555        555          0         555&lt;BR /&gt;
  5    shoes       01       1     100      655        555        100         655&lt;BR /&gt;
  6    shoes       01       2     150      805        555        250         805&lt;BR /&gt;
  7    shoes       01       3     175      980        555        425         980&lt;BR /&gt;
  8    shoes       01       4     100     1080        555        525        1080&lt;BR /&gt;
  9    shirts      02       1      89       89         89          0        1169&lt;BR /&gt;
 10    shirts      02       2     120      209        209          0        1289&lt;BR /&gt;
 11    shirts      02       3      60      269        269          0        1349&lt;BR /&gt;
 12    shirts      02       4     150      419        419          0        1499&lt;BR /&gt;
 13    shoes       02       1      99      518        419         99        1598&lt;BR /&gt;
 14    shoes       02       2     100      618        419        199        1698&lt;BR /&gt;
 15    shoes       02       3      60      678        419        259        1758&lt;BR /&gt;
 16    shoes       02       4     100      778        419        359        1858&lt;BR /&gt;
***************************************************************************************&lt;BR /&gt;
                   &lt;BR /&gt;
summary file&lt;BR /&gt;
                         &lt;BR /&gt;
Obs    month    montot    shirttot    shoetot&lt;BR /&gt;
                                    &lt;BR /&gt;
 1      01       1080        555        525&lt;BR /&gt;
 2      02        778        419        359&lt;BR /&gt;
                 &lt;BR /&gt;
[/pre]</description>
      <pubDate>Fri, 15 Aug 2008 17:25:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Calculating-Monthly-subtotals-in-EG/m-p/39003#M4698</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2008-08-15T17:25:44Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating Monthly subtotals in EG</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Calculating-Monthly-subtotals-in-EG/m-p/39004#M4699</link>
      <description>Thanks, got it working fine.</description>
      <pubDate>Tue, 19 Aug 2008 15:40:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Calculating-Monthly-subtotals-in-EG/m-p/39004#M4699</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2008-08-19T15:40:42Z</dc:date>
    </item>
  </channel>
</rss>

