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;
By combining the steps you ignored the function of PROC EXPAND
and you got missing value because the variable - cumulative - was not calculated and is missing value.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.