09-16-2016 12:28 PM
I've been working on this for a while now (using Lag function) but i'm not getting anywhere.
Bascially, the dataset is sorted by scrape time. They are all bets and assume that after the event the stake is returned to my account. So my capital to start is 10000. In the first row my capital is 10000-stake. In the second row, I need to look back to the first row. Using lag If the first event is over then my stake is returned.
Here is my code:
infile datalines dsd truncover;
input stake:32. scrapetime: DATETIME14. eventtime: DATETIME14.;
if 0 then set dataset nobs=n;
if _n_ = 1 then do; running_capital = &initialcapital - stake; end;
%do look_back=1 %to &nrows;
if lag&look_back.(eventtime) < scrapetime then running_capital = running_capital+lag&look_back.(stake);
running_capital = running_capital - stake;
09-16-2016 01:05 PM
Hi , I don't know what the expected result is as I've simplified the code to highlight where I'm having the trouble.
09-16-2016 01:59 PM
I submit that if you cannot determine by hand (manually as in pencil and paper) what your desired result is for a small number of records then you really can't code a solution.
09-16-2016 02:28 PM
09-16-2016 03:04 PM
Here's a reasonable way to approach this.
proc sort data=have (drop=scrapetime) out=returns;
rename eventtime = scrapetime;
retain running_capital 10000;
set have (in=in1) returns2 (in=in2);
if in1 then running_capital = running_capital - stake;
running_capital = running_capital + stake;
It's untested, but definitely represents an easier way to approach the problem. Also, you don't necessarily have to delete the returns records if you think that information would be valuable (or if it helps illustrate how the program is working).