<?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: SAS Running total by category and date in Statistical Procedures</title>
    <link>https://communities.sas.com/t5/Statistical-Procedures/SAS-Running-total-by-category-and-date/m-p/97893#M4916</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Great stuff. That is now giving me the cumulative dailyProfit in each row as I wanted, and resetting to zero each new date and new user. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;But rather than a cumulative value, how can I sum from first.&lt;SPAN style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;betdate&lt;/SPAN&gt; to last.&lt;SPAN style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;betdate&lt;/SPAN&gt; over each username? This would give me the total dailyProfit for each username for each date (repearing across multiple rows just like a SUMIF() in Excel). &lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;Thanks again.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 06 Jun 2013 16:06:10 GMT</pubDate>
    <dc:creator>tobriain</dc:creator>
    <dc:date>2013-06-06T16:06:10Z</dc:date>
    <item>
      <title>SAS Running total by category and date</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/SAS-Running-total-by-category-and-date/m-p/97890#M4913</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have the following data: &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data have;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; input username $ amount betdate : datetime.;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; dateOnly = datepart(betdate) ;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; format betdate DATETIME.;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; format dateOnly ddmmyy8.;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; datalines; &lt;/P&gt;&lt;P&gt;player1 90 12NOV2008:12:04:01&lt;/P&gt;&lt;P&gt;player1 -100 04NOV2008:09:03:44&lt;/P&gt;&lt;P&gt;player2 120 07NOV2008:14:03:33&lt;/P&gt;&lt;P&gt;player1 -50 05NOV2008:09:00:00&lt;/P&gt;&lt;P&gt;player1 -30 05NOV2008:09:05:00&lt;/P&gt;&lt;P&gt;player1 20 05NOV2008:09:00:05&lt;/P&gt;&lt;P&gt;player2 10 09NOV2008:10:05:10&lt;/P&gt;&lt;P&gt;player2 -35 15NOV2008:15:05:33&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt; PROC PRINT data=have; RUN;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc sort data=have;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; by username betdate;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data want;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; set have;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; by username betdate dateOnly;&amp;nbsp;&amp;nbsp; &lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; retain calendarTime eventTime dailyProfit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; if first.username then calendarTime = 0;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; if first.dateOnly then calendarTime + 1;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; if first.username then eventTime = 0;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; if first.betdate then eventTime + 1;&amp;nbsp;&amp;nbsp; &lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; if first.username then dailyProfit = 0;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; dailyProfit = dailyProfit + amount;&lt;/P&gt;&lt;P&gt; PROC PRINT data=want; RUN;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #000000; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; font-size: 14px; background-color: #ffffff;"&gt;The value in dailyProfit is incorrect, as it is resetting every new betdate, not every new dateOnly and new username. How can I get the syntax right on this?&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Best regards.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 06 Jun 2013 14:49:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/SAS-Running-total-by-category-and-date/m-p/97890#M4913</guid>
      <dc:creator>tobriain</dc:creator>
      <dc:date>2013-06-06T14:49:38Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Running total by category and date</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/SAS-Running-total-by-category-and-date/m-p/97891#M4914</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Try removing BETDATE from the BY statement and use DATEONLY instead of BETDATE. Dateonly is "first" for each leverl of BETDATE.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 06 Jun 2013 15:36:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/SAS-Running-total-by-category-and-date/m-p/97891#M4914</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2013-06-06T15:36:55Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Running total by category and date</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/SAS-Running-total-by-category-and-date/m-p/97892#M4915</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Your program is missing one statement:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;retain dailyProfit;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;You can add it anywhere within the DATA step.&amp;nbsp; The other variables (calendarTime and eventTime) don't need it because there are already being retained.&amp;nbsp; The syntax "variablename + 1" will automatically retain that variable.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 06 Jun 2013 15:44:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/SAS-Running-total-by-category-and-date/m-p/97892#M4915</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2013-06-06T15:44:26Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Running total by category and date</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/SAS-Running-total-by-category-and-date/m-p/97893#M4916</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Great stuff. That is now giving me the cumulative dailyProfit in each row as I wanted, and resetting to zero each new date and new user. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;But rather than a cumulative value, how can I sum from first.&lt;SPAN style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;betdate&lt;/SPAN&gt; to last.&lt;SPAN style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;betdate&lt;/SPAN&gt; over each username? This would give me the total dailyProfit for each username for each date (repearing across multiple rows just like a SUMIF() in Excel). &lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;Thanks again.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 06 Jun 2013 16:06:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/SAS-Running-total-by-category-and-date/m-p/97893#M4916</guid>
      <dc:creator>tobriain</dc:creator>
      <dc:date>2013-06-06T16:06:10Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Running total by category and date</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/SAS-Running-total-by-category-and-date/m-p/97894#M4917</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;It's up to you when you reset DailyProfit to 0.&amp;nbsp; Right now you have coded:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;if first.username than dailyProfit=0;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;You can switch that to whatever conditions you would like, such as:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;if first.betdate then dailyProfit=0;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Of course, to do that, BETDATE must remain part of your BY statement.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 06 Jun 2013 16:15:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/SAS-Running-total-by-category-and-date/m-p/97894#M4917</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2013-06-06T16:15:52Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Running total by category and date</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/SAS-Running-total-by-category-and-date/m-p/97895#M4918</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I'm not sure if that resolves it. I really need to add a flag to each user flagging whether the customers total dailyProfit (for each parituclar day) was positive or not. Because the main query I want to run is this:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;&amp;nbsp; select&lt;/P&gt;&lt;P&gt;&amp;nbsp; outer.calendarTime as calendarTime,&lt;/P&gt;&lt;P&gt;&amp;nbsp; [dailyProfitIndicator,]&lt;/P&gt;&lt;P&gt;&amp;nbsp; mean(outer.stake) as meanStake,&lt;/P&gt;&lt;P&gt;&amp;nbsp; sum(outer.stake) as totalStake,&lt;/P&gt;&lt;P&gt;&amp;nbsp; sum(outer.winnings - outer.stake) as profit,&lt;/P&gt;&lt;P&gt;&amp;nbsp; sum(outer.winnings - outer.stake) /sum(outer.stake) as margin&lt;/P&gt;&lt;P&gt;from work.want as outer&lt;/P&gt;&lt;P&gt;where calendarTime =1&lt;/P&gt;&lt;P&gt;[and dailyProfitIndicator = 1]&lt;/P&gt;&lt;P&gt;group by 1&lt;/P&gt;&lt;P&gt;having sum(outer.winnings - outer.stake) &amp;gt; 0;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;What it's supposed to do is get the averageStake for customers on their first day if the first day was a 'winning' day i.e. if the total daily profit for the user on that day was greater than zero. And I want to do this over ALL customers, so I don't think the 'having sum(outer.winnings - outer.stake) &amp;gt; 0;' is aggregating over each username and each day, just over each row in this query (which is only 1).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I've changed the data step above to add another 'totalProfit' rather than the cumulative 'dailyProfit' variable above. But it still shows a cumulative rather than repeating the total in each row. Is this expected?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data want;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; set work.temp;&amp;nbsp; &lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; by username dateOnly betdate;&amp;nbsp; &lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; retain calendarTime eventTime dailyProfit totalProfit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; if first.username then calendarTime = 0;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; if first.dateOnly then calendarTime + 1;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; if first.username then eventTime = 0;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; if first.betdate then eventTime + 1;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; if first.username then dailyProfit = 0;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; if first.dateOnly then dailyProfit = 0;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; if first.betdate then dailyProfit + sum(winnings - stake);&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; if first.dateOnly then totalProfit = 0;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; if first.betdate then totalProfit + sum(winnings - stake);&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 06 Jun 2013 16:38:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/SAS-Running-total-by-category-and-date/m-p/97895#M4918</guid>
      <dc:creator>tobriain</dc:creator>
      <dc:date>2013-06-06T16:38:12Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Running total by category and date</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/SAS-Running-total-by-category-and-date/m-p/97896#M4919</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;If I wanted a summary of total by date for each user I would drop out of a datastep and go to proc means or summary;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc means data=have sum;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; class username betdate;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; var amount;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If a data set is needed then an output statement can be added.&lt;/P&gt;&lt;P&gt;&lt;BR /&gt; &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 06 Jun 2013 16:39:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/SAS-Running-total-by-category-and-date/m-p/97896#M4919</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2013-06-06T16:39:27Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Running total by category and date</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/SAS-Running-total-by-category-and-date/m-p/97897#M4920</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;The question I need to answer is 'What is the average spent by profitable people on their first day'? I can add the 'first day' flag (as above), but can't control for getting the average spent by those that are profitable on the first day rather than the entire sample.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 06 Jun 2013 16:41:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/SAS-Running-total-by-category-and-date/m-p/97897#M4920</guid>
      <dc:creator>tobriain</dc:creator>
      <dc:date>2013-06-06T16:41:54Z</dc:date>
    </item>
  </channel>
</rss>

