BookmarkSubscribeRSS Feed
Junyong
Pyrite | Level 9

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;
1 REPLY 1
Shmuel
Garnet | Level 18

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. 

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 1 reply
  • 749 views
  • 0 likes
  • 2 in conversation