title: SAS: Calculate Standard Deviation on-the-fly in datastep
body: <p>I have the following sample data:</p>
  
  <pre><code>data have;
   input username $ stake betdate : datetime.;
  dateOnly = datepart(betdate) ;
  format betdate DATETIME.;
  format dateOnly ddmmyy8.;
  datalines; 
  player1 90 12NOV2008:12:04:01
  player1 -100 04NOV2008:09:03:44
  player2 120 07NOV2008:14:03:33
  player1 -50 05NOV2008:09:00:00
  player1 -30 05NOV2008:09:05:00
  player1 20 05NOV2008:09:00:05
  player2 10 09NOV2008:10:05:10
  player2 -35 15NOV2008:15:05:33
  run;
  PROC PRINT; RUN;
  proc sort data=have;
  by username betdate;
     run;
   data want;
  set have;
  by username dateOnly betdate;   
  retain calendarTime eventTime cumulativeDailyProfit standardDeviationStake;
  if first.username then calendarTime = 0;
  if first.dateOnly then calendarTime + 1;
  if first.username then eventTime = 0;
  if first.betdate then eventTime + 1;
  if first.username then cumulativeDailyProfit = 0;
  if first.dateOnly then cumulativeDailyProfit = 0;
  if first.betdate then cumulativeDailyProfit + stake;
  run;
  PROC PRINT; RUN;
  </code></pre>
  
  <p>I need some way of comparing players with very different stake sizes and normalize their betting stakes. For each player's bet, I was thinking of calculating the standard deviation out that bet (as below). Then I could add the squares of these and get the square root to have the total standard deviation of each players bets. Then I could compare each stake being playing by the player to his total standard deviation. </p>
  
  <p>If this game is a coin toss, the probability of winning is 0.50. This is a binomial outcome, so the standard deviation is σ = (p(1 − p)/n)1/2. So the standard deviation of the first bet above is 90*[0.5*0.5]^0.5 = 45. </p>
  
  <p>How can I calculate the standard deviation of each players stake like I calculated the cumulative profit values below? I probably need the standard deviation of each bet (for each player), the total standard deviation for each player, and the 'normalised stake' i.e. stake of the bet divided by the standrad deviation of the bet. I can then do a kind of comparision between players with different magnitudes of stake. </p>
  
  <p>I'd appreciate any help at all on this!</p>
  
  <p>Thanks.</p>