<?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 SAS get Total of Category in Data Step in Statistical Procedures</title>
    <link>https://communities.sas.com/t5/Statistical-Procedures/SAS-get-Total-of-Category-in-Data-Step/m-p/98623#M4945</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;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;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 dateOnly betdate;&amp;nbsp; &lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; retain calendarTime eventTime cumulativeDailyProfit profitableFlag totalDailyProfit;&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; &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 cumulativeDailyProfit = 0;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; if first.dateOnly then cumulativeDailyProfit = 0;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; if first.betdate then cumulativeDailyProfit + amount;&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 totalDailyProfit = 0;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; if first.betdate then totalDailyProfit + amount;&lt;/P&gt;&lt;P&gt;PROC PRINT data=want; RUN;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I want to run the query below, and get the mean stake on all days (which is correct now) and the mean stake on losing days and on winnings days. If I get the 'profitableFlag' field set properly above this should be ok.&lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;&amp;nbsp; select calendarTime,&lt;/P&gt;&lt;P&gt;&amp;nbsp; mean(amount) as meanStake,&lt;/P&gt;&lt;P&gt;&amp;nbsp; mean(case when 1 = profitableFlag then amount else . End) as meanLosingDayStake,&lt;/P&gt;&lt;P&gt;&amp;nbsp; mean(case when 1 = profitableFlag then amount else . End) as meanWinningDayStake&lt;/P&gt;&lt;P&gt;&amp;nbsp; from want&lt;/P&gt;&lt;P&gt;&amp;nbsp; group by 1;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Is this possible in the data step above?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 06 Jun 2013 20:44:48 GMT</pubDate>
    <dc:creator>tobriain</dc:creator>
    <dc:date>2013-06-06T20:44:48Z</dc:date>
    <item>
      <title>SAS get Total of Category in Data Step</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/SAS-get-Total-of-Category-in-Data-Step/m-p/98623#M4945</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;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;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 dateOnly betdate;&amp;nbsp; &lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; retain calendarTime eventTime cumulativeDailyProfit profitableFlag totalDailyProfit;&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; &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 cumulativeDailyProfit = 0;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; if first.dateOnly then cumulativeDailyProfit = 0;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; if first.betdate then cumulativeDailyProfit + amount;&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 totalDailyProfit = 0;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; if first.betdate then totalDailyProfit + amount;&lt;/P&gt;&lt;P&gt;PROC PRINT data=want; RUN;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I want to run the query below, and get the mean stake on all days (which is correct now) and the mean stake on losing days and on winnings days. If I get the 'profitableFlag' field set properly above this should be ok.&lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;&amp;nbsp; select calendarTime,&lt;/P&gt;&lt;P&gt;&amp;nbsp; mean(amount) as meanStake,&lt;/P&gt;&lt;P&gt;&amp;nbsp; mean(case when 1 = profitableFlag then amount else . End) as meanLosingDayStake,&lt;/P&gt;&lt;P&gt;&amp;nbsp; mean(case when 1 = profitableFlag then amount else . End) as meanWinningDayStake&lt;/P&gt;&lt;P&gt;&amp;nbsp; from want&lt;/P&gt;&lt;P&gt;&amp;nbsp; group by 1;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Is this possible in the data step above?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 06 Jun 2013 20:44:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/SAS-get-Total-of-Category-in-Data-Step/m-p/98623#M4945</guid>
      <dc:creator>tobriain</dc:creator>
      <dc:date>2013-06-06T20:44:48Z</dc:date>
    </item>
    <item>
      <title>Re: SAS get Total of Category in Data Step</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/SAS-get-Total-of-Category-in-Data-Step/m-p/98624#M4946</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;What is the business rule for profitable fkag?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 06 Jun 2013 21:21:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/SAS-get-Total-of-Category-in-Data-Step/m-p/98624#M4946</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2013-06-06T21:21:03Z</dc:date>
    </item>
    <item>
      <title>Re: SAS get Total of Category in Data Step</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/SAS-get-Total-of-Category-in-Data-Step/m-p/98625#M4947</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Profitable flag should be set to 1 if the cumulative total is greater than 0 and 0 otherwise. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have tried doing this with a (horrible) subselect as follows, but would rather something a little more elegant:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;&amp;nbsp; select calendarTime,&lt;/P&gt;&lt;P&gt;&amp;nbsp; mean(amount ) as meanStake,&lt;/P&gt;&lt;P&gt;&amp;nbsp; mean(case when (case when (select cumulativeDailyProfit from work.want as sub where sub.username = outer.username and sub.betdate = (select max(subsub.betdate) from work.want as subsub where subsub.username = outer.username and subsub.dateOnly = outer.dateOnly)) &amp;lt;= 0 then 1 else 0 end) = 1 then amount else . End) as meanLosingDayStake,&lt;/P&gt;&lt;P&gt;&amp;nbsp; sum(case when (case when (select cumulativeDailyProfit from work.want as sub where sub.username = outer.username and sub.betdate = (select max(subsub.betdate) from work.want as subsub where subsub.username = outer.username and subsub.dateOnly = outer.dateOnly)) &amp;lt;= 0 then 1 else 0 end) = 1 then amount else . End) as sumLosingDayStake,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; mean(case when (case when (select cumulativeDailyProfit from work.want as sub where sub.username = outer.username and sub.betdate = (select max(subsub.betdate) from work.want as subsub where subsub.username = outer.username and subsub.dateOnly = outer.dateOnly)) &amp;gt; 0 then 1 else 0 end) = 1 then amount else . End) as meanWinningDayStake,&lt;/P&gt;&lt;P&gt;&amp;nbsp; sum(case when (case when (select cumulativeDailyProfit from work.want as sub where sub.username = outer.username and sub.betdate = (select max(subsub.betdate) from work.want as subsub where subsub.username = outer.username and subsub.dateOnly = outer.dateOnly)) &amp;gt; 0 then 1 else 0 end) = 1 then amount else . End) as sumWinningDayStake&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;from want as outer&lt;/P&gt;&lt;P&gt;&amp;nbsp; group by 1;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 06 Jun 2013 21:29:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/SAS-get-Total-of-Category-in-Data-Step/m-p/98625#M4947</guid>
      <dc:creator>tobriain</dc:creator>
      <dc:date>2013-06-06T21:29:04Z</dc:date>
    </item>
  </channel>
</rss>

