Suppose a stock return panel data set as follows. data RAW;
format FIRM $8. DATE yymmddn8.;
do FIRM="GOOGL","MSFT";
do DATE="12apr2019"d to "22apr2019"d;
RETURN=round(0.01+sqrt(0.01)*rannor(1),0.01);
output;
end;
end;
run; In many cases, I create a cumulative return by (1) using a log return, (2) accumulating, and (3) taking an exponential. I tried to shorten these tasks using IFN, but failed. Is there any shorter way to do the multiplicative works? data PROCESSED;
set RAW;
by FIRM DATE;
/*1. Conventional. Works nicely, but too many lines*/
if first.FIRM then LOG=log(1+RETURN);
else LOG+log(1+RETURN);
CUMULATIVE=exp(LOG)-1;
/*2. Tried IFN to shorten, but doesn't work*/
LOG1=ifn(first.FIRM,log(1+RETURN),LOG1+log(1+RETURN));
/*3. Wondered if it's about LAG, but wasn't*/
LOG2=ifn(first.FIRM,log(1+RETURN),lag(LOG2)+log(1+RETURN));
/*4. Tried to accumulate by multiplying, but doesn't work*/
if first.FIRM then CUMULATIVE1=RETURN;
else CUMULATIVE1=(1+CUMULATIVE1)*(1+RETURN)-1;
/*5. Wondered if it's about LAG, but wasn't*/
if first.FIRM then CUMULATIVE2=RETURN;
else CUMULATIVE2=(1+lag(CUMULATIVE2))*(1+RETURN)-1;
/*6. Tried to shorten using IFN, but doesn't work*/
CUMULATIVE3=ifn(first.FIRM,RETURN,(1+CUMULATIVE3)*(1+RETURN)-1);
/*7. Tried the same thing with a LAG function, but doesn't work*/
CUMULATIVE4=ifn(first.FIRM,RETURN,(1+lag(CUMULATIVE4))*(1+RETURN)-1);
run; Thanks.
... View more