I'll explain my problem to you.
I would like to create a loop to repeat the same operation several times after making a selection, without having to repeat the same code several times.
Here is the example of my code:
Code:
gen SIZE`YearN '= "small" if PERS`YearN' <50 & (RICAVIVEND`YearN '<= 10000 | TOTATT`YearN' <= 10000) replace SIZE`YearN '= "medium" if SIZE`YearN'! = "small" & PERS`YearN '<250 & (REVIVEND`YearN' <= 50000 | TOTAT`YearN '<= 43000) replace SIZE`YearN '= "big" if SIZE`YearN'! = "small" & SIZE`YearN '! = "medium" & PERS`YearN'! =. & (PERS`YearN '> = 250 | RICAVIVEND`YearN'> 50000 | TOTATT`YearN '> 43000) drop if missing (`YearN 'SIZE) ************************************************Small****************************************** preserve keep if SIZE`YearN '== "small" ************ ROE ************ forvalues y = `=` YearN'-4 '(1) `YearN' { gen ROE`y '= RN`y' / PN`y ' } ************ EBITDA / SALES ************ forvalues y = `=` YearN'-4 '(1) `YearN' { gen EBITDA`y '= EBITDA`y' / VALPROD`y ' } *********** ROI ************* forvalues y = `=` YearN'-4 '(1) `YearN' { gen ROI`y '= RO`y' / CAPINV`y ' } *********** TOTFATT ************* forvalues y = `=` YearN'-5 '(1) `YearN' { gen TOTFATT`y '= REVENUE`y' + OTHERIRIC`y ' } ********** LEVERAGE ************** forvalues y = `=` YearN'-4 '(1) `YearN' { gen LEVERAGE`y '= TOTATT`y' / PN`y ' } export excel using "C: \ Users \ Ricky \ Desktop \ Interlocking2020 \ ISP2021 \ ISP2019.xlsx", sheet ("small") firstrow (variables) restore **********************************************Medium****************************************************** preserve keep if SIZE`YearN '== "medium" ************ ROE ************ forvalues y = `=` YearN'-4 '(1) `YearN' { gen ROE`y '= RN`y' / PN`y ' } ************ EBITDA / SALES ************ forvalues y = `=` YearN'-4 '(1) `YearN' { gen EBITDA`y '= EBITDA`y' / VALPROD`y ' } *********** ROI ************* forvalues y = `=` YearN'-4 '(1) `YearN' { gen ROI`y '= RO`y' / CAPINV`y ' } *********** TOTFATT ************* forvalues y = `=` YearN'-5 '(1) `YearN' { gen TOTFATT`y '= REVENUE`y' + OTHERIRIC`y ' } ********** LEVERAGE ************** forvalues y = `=` YearN'-4 '(1) `YearN' { gen LEVERAGE`y '= TOTATT`y' / PN`y ' } export excel using "C: \ Users \ Ricky \ Desktop \ Interlocking2020 \ ISP2021 \ ISP2019.xlsx", sheet ("medium") firstrow (variables) restore ***************************Big***************************************************** preserve keep if SIZE`YearN '== "big" ************ ROE ************ forvalues y = `=` YearN'-4 '(1) `YearN' { gen ROE`y '= RN`y' / PN`y ' } ************ EBITDA / SALES ************ forvalues y = `=` YearN'-4 '(1) `YearN' { gen EBITDA`y '= EBITDA`y' / VALPROD`y ' } *********** ROI ************* forvalues y = `=` YearN'-4 '(1) `YearN' { gen ROI`y '= RO`y' / CAPINV`y ' } *********** TOTFATT ************* forvalues y = `=` YearN'-5 '(1) `YearN' { gen TOTFATT`y '= REVENUE`y' + OTHERIRIC`y ' } ********** LEVERAGE ************** forvalues y = `=` YearN'-4 '(1) `YearN' { gen LEVERAGE`y '= TOTATT`y' / PN`y ' } export excel using "C: \ Users \ Ricky \ Desktop \ Interlocking2020 \ ISP2021 \ ISP2019.xlsx", sheet ("big") firstrow (variables) restore
I had thought about using a foreach like this:
Code:
foresch x in DIMENSION`YearN '{ keep if SIZE`YearN '== `x' ************ ROE ************ forvalues y = `=` YearN'-4 '(1) `YearN' { gen ROE`y '= RN`y' / PN`y ' } ************ EBITDA / SALES ************ forvalues y = `=` YearN'-4 '(1) `YearN' { gen EBITDA`y '= EBITDA`y' / VALPROD`y ' } *********** ROI ************* forvalues y = `=` YearN'-4 '(1) `YearN' { gen ROI`y '= RO`y' / CAPINV`y ' } *********** TOTFATT ************* forvalues y = `=` YearN'-5 '(1) `YearN' { gen TOTFATT`y '= REVENUE`y' + OTHERIRIC`y ' } ********** LEVERAGE ************** forvalues y = `=` YearN'-4 '(1) `YearN' { gen LEVERAGE`y '= TOTATT`y' / PN`y ' } } export excel using "C: \ Users \ Ricky \ Desktop \ Interlocking2020 \ ISP2021 \ ISP2019.xlsx", sheet (`x ') firstrow (variables)
Thank you!
0 Response to Create a loop to repeat the same operation several times
Post a Comment