Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Create a loop to repeat the same operation several times

    Good morning,
    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 & (RICAVIVEND`YearN' <= 50000 | TOTATT`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 would like to create a loop that would automatically select the size of the companies I am analyzing without having to use preserve \ restore three times.

    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)
    But, unfortunately, I don't work. Could you tell me the error?
    Thank you!
    Last edited by Riccardo Busin; 23 Nov 2021, 16:02.

  • #2
    "it didn't work" is unfortunately no guide to what went wrong. See https://www.statalist.org/forums/help#stata There is no example data here, nor is the code self-contained.

    I counted in previous threads three suggestions that this data layout is not fit for purpose. You would be better off with reshape long.

    https://www.statalist.org/forums/for...-them-via-loop

    https://www.statalist.org/forums/for...nges-via-loops

    https://www.statalist.org/forums/for...s-of-variables

    Every time you ignore this advice, you are just writing yet more convoluted code to add yet more variables to an awkward data layout.


    Last edited by Nick Cox; 24 Nov 2021, 02:59.

    Comment

    Working...
    X