Dear Statalist users,

I would like to annualize monthly returns on funds by using the following formula: APY = (1+r1)*(1+r2) *(1+r3) *(1+r4) *(1+r5) *(1+r6) *(1+r7) *(1+r8) *(1+r9) *(1+r10) *(1+r11) *(1+r12) – 1 (Source: How to Annualize Monthly Returns – Example - Finance Train).
In my dataset i have a fundid, years, months and my monthly returns as variables.
I tried to solve the problem by using a forvalue loop for the first 25 rows. But due to the fact that i don't always have data for 12 months each year, i'm not able to use the following code for the whole dataset (about a quarter million rows).
Code:
gen x = return
gen z = 1
forvalue j = 1(12)25{
    replace z = `j' +2
    replace x = (1+return[`z'-1])*(1+return[`z'-2])-1 in `z'
    forvalues i = 3/12 {
        replace z = z + 1
        replace x = (1+return[`z'])*(1+x[`z'-1])-1 in `z'
    }
}
Does anyone know, if there's a function in stata or any stata program i can use to solve this? Or do you have any other suggestions?

Many thanks in advance!
Philipp