I use historical prices to compute returns and cumulative returns as follows.
data ko(drop=dummy);
infile 'https://query1.finance.yahoo.com/v7/finance/download/ko?
period1=-9999999999&period2=9999999999' url firstobs=2 dsd truncover;
input date yymmdd10. +1 (4*dummy)(:$1.) ko;
return=ko/lag(ko);
run;
proc expand method=none out=ko;
id date;
convert return=cumulative/tout=(cuprod);
run;
And I wanted to shorten the code using retain as follows but failed.
data ko(drop=dummy);
infile 'https://query1.finance.yahoo.com/v7/finance/download/ko?
period1=-9999999999&period2=9999999999' url firstobs=2 dsd truncover;
input date yymmdd10. +1 (4*dummy)(:$1.) ko;
retain cumulative 1;
cumulative=cumulative*ko/lag(ko);
run;
I found that cumulative without /lag(ko) recursively multiplies the historical ko values but the /lag(ko) makes everything missing. What am I doing wrong here?
Updated: This is not the best but what I found.
data ko2(drop=dummy);
infile 'https://query1.finance.yahoo.com/v7/finance/download/ko?
period1=-9999999999&period2=9999999999' url firstobs=2 dsd truncover;
input date yymmdd10. +1 (4*dummy)(:$1.) ko;
retain cumulative;
cumulative=ifn(lag(ko)>.,cumulative*ko/lag(ko),1);
run;