Announcement

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

  • Loops

    I have a panel dataset with 26 countries and I want to create the below variables in order to do a two plot afterwards. The command I am using takes a lot of time and need someone

    to help me to create a loop.



    1) First I take the country average for particular variable (Ths is ok)

    bysort country: egen m_GDPGR= mean(GDPGR)
    gen out_loss=.
    replace out_loss= GDPGR-m_GDPGR if GDPGR!=.

    by country : summarize out_loss if CurrencyCrises==1





    2) I have to take just country code just once for each country (country code is 24 times for each country and need to extract it just one time for each country: here I need a loop)

    gen Cou_Code_O="A" in 1/26

    replace Cou_Code_O= "ARG" in 1
    replace Cou_Code_O= "AUS" in 2
    replace Cou_Code_O= "BOL" in 3
    replace Cou_Code_O= "BRA" in 4
    replace Cou_Code_O= "CHL" in 5
    replace Cou_Code_O= "CHN" in 6
    replace Cou_Code_O= "COL" in 7
    replace Cou_Code_O= "ECU" in 8
    replace Cou_Code_O= "ESP" in 9
    replace Cou_Code_O= "FIN" in 10
    replace Cou_Code_O= "GBR" in 11
    replace Cou_Code_O= "GRC" in 12
    replace Cou_Code_O= "HUN" in 13
    replace Cou_Code_O= "IDN" in 14
    replace Cou_Code_O= "ISL" in 15
    replace Cou_Code_O= "KOR" in 16
    replace Cou_Code_O= "MEX" in 17
    replace Cou_Code_O= "NOR" in 18
    replace Cou_Code_O= "PHL" in 19
    replace Cou_Code_O= "POL" in 20
    replace Cou_Code_O= "ROU" in 21
    replace Cou_Code_O= "RUS" in 22
    replace Cou_Code_O= "SWE" in 23
    replace Cou_Code_O= "THA" in 24
    replace Cou_Code_O= "TUR" in 25
    replace Cou_Code_O= "VEN" in 26


    3) And in here a have to do the same thing for 10-15 variables, and this take a lot of time. Is there any way to do it in a loop.

    3.1) 1st variable



    gen out_loss_mp=.
    summarize out_loss if CurrencyCrises==1&CountryCode=="ARG"
    replace out_loss_mp = _result(3) in 1

    summarize out_loss if CurrencyCrises==1&CountryCode=="AUS"
    replace out_loss_mp = _result(3) in 2

    summarize out_loss if CurrencyCrises==1&CountryCode=="BOL"
    replace out_loss_mp = _result(3) in 3

    summarize out_loss if CurrencyCrises==1&CountryCode=="BRA"
    replace out_loss_mp = _result(3) in 4

    summarize out_loss if CurrencyCrises==1&CountryCode=="CHL"
    replace out_loss_mp = _result(3) in 5

    summarize out_loss if CurrencyCrises==1&CountryCode=="CHN"
    replace out_loss_mp = _result(3) in 6

    summarize out_loss if CurrencyCrises==1&CountryCode=="COL"
    replace out_loss_mp = _result(3) in 7


    summarize out_loss if CurrencyCrises==1&CountryCode=="ECU"
    replace out_loss_mp = _result(3) in 8


    summarize out_loss if CurrencyCrises==1&CountryCode=="ESP"
    replace out_loss_mp = _result(3) in 9


    summarize out_loss if CurrencyCrises==1&CountryCode=="FIN"
    replace out_loss_mp = _result(3) in 10

    summarize out_loss if CurrencyCrises==1&CountryCode=="GBR"
    replace out_loss_mp = _result(3) in 11

    summarize out_loss if CurrencyCrises==1&CountryCode=="GRC"
    replace out_loss_mp = _result(3) in 12


    summarize out_loss if CurrencyCrises==1&CountryCode=="HUN"
    replace out_loss_mp = _result(3) in 13


    summarize out_loss if CurrencyCrises==1&CountryCode=="IDN"
    replace out_loss_mp = _result(3) in 14


    summarize out_loss if CurrencyCrises==1&CountryCode=="ISL"
    replace out_loss_mp = _result(3) in 15

    summarize out_loss if CurrencyCrises==1&CountryCode=="KOR"
    replace out_loss_mp = _result(3) in 16

    summarize out_loss if CurrencyCrises==1&CountryCode=="MEX"
    replace out_loss_mp = _result(3) in 17


    summarize out_loss if CurrencyCrises==1&CountryCode=="NOR"
    replace out_loss_mp = _result(3) in 18


    summarize out_loss if CurrencyCrises==1&CountryCode=="PHL"
    replace out_loss_mp = _result(3) in 19


    summarize out_loss if CurrencyCrises==1&CountryCode=="POL"
    replace out_loss_mp = _result(3) in 20

    summarize out_loss if CurrencyCrises==1&CountryCode=="ROU"
    replace out_loss_mp = _result(3) in 21

    summarize out_loss if CurrencyCrises==1&CountryCode=="RUS"
    replace out_loss_mp = _result(3) in 22

    summarize out_loss if CurrencyCrises==1&CountryCode=="SWE"
    replace out_loss_mp = _result(3) in 23

    summarize out_loss if CurrencyCrises==1&CountryCode=="THA"
    replace out_loss_mp = _result(3) in 24

    summarize out_loss if CurrencyCrises==1&CountryCode=="TUR"
    replace out_loss_mp = _result(3) in 25

    summarize out_loss if CurrencyCrises==1&CountryCode=="VEN"
    replace out_loss_mp = _result(3) in 26




    3.2) 2nd variable


    gen TOTREXD_mp=. in 1/26
    summarize TOTREXD if CurrencyCrises==1&CountryCode=="ARG"
    replace TOTREXD_mp = _result(3) in 1

    summarize TOTREXD if CurrencyCrises==1&CountryCode=="AUS"
    replace TOTREXD_mp = _result(3) in 2

    summarize TOTREXD if CurrencyCrises==1&CountryCode=="BOL"
    replace TOTREXD_mp = _result(3) in 3

    summarize TOTREXD if CurrencyCrises==1&CountryCode=="BRA"
    replace TOTREXD_mp = _result(3) in 4

    summarize TOTREXD if CurrencyCrises==1&CountryCode=="CHL"
    replace TOTREXD_mp = _result(3) in 5


    summarize TOTREXD if CurrencyCrises==1&CountryCode=="CHN"
    replace TOTREXD_mp = _result(3) in 6

    summarize TOTREXD if CurrencyCrises==1&CountryCode=="COL"
    replace TOTREXD_mp = _result(3) in 7


    summarize TOTREXD if CurrencyCrises==1&CountryCode=="ECU"
    replace TOTREXD_mp = _result(3) in 8


    summarize TOTREXD if CurrencyCrises==1&CountryCode=="ESP"
    replace TOTREXD_mp = _result(3) in 9


    summarize TOTREXD if CurrencyCrises==1&CountryCode=="FIN"
    replace TOTREXD_mp = _result(3) in 10

    summarize TOTREXD if CurrencyCrises==1&CountryCode=="GBR"
    replace TOTREXD_mp = _result(3) in 11

    summarize TOTREXD if CurrencyCrises==1&CountryCode=="GRC"
    replace TOTREXD_mp = _result(3) in 12


    summarize TOTREXD if CurrencyCrises==1&CountryCode=="HUN"
    replace TOTREXD_mp = _result(3) in 13


    summarize TOTREXD if CurrencyCrises==1&CountryCode=="IDN"
    replace TOTREXD_mp = _result(3) in 14


    summarize TOTREXD if CurrencyCrises==1&CountryCode=="ISL"
    replace TOTREXD_mp = _result(3) in 15

    summarize TOTREXD if CurrencyCrises==1&CountryCode=="KOR"
    replace TOTREXD_mp = _result(3) in 16

    summarize TOTREXD if CurrencyCrises==1&CountryCode=="MEX"
    replace TOTREXD_mp = _result(3) in 17


    summarize TOTREXD if CurrencyCrises==1&CountryCode=="NOR"
    replace TOTREXD_mp = _result(3) in 18


    summarize TOTREXD if CurrencyCrises==1&CountryCode=="PHL"
    replace TOTREXD_mp = _result(3) in 19


    summarize TOTREXD if CurrencyCrises==1&CountryCode=="POL"
    replace TOTREXD_mp = _result(3) in 20

    summarize TOTREXD if CurrencyCrises==1&CountryCode=="ROU"
    replace TOTREXD_mp = _result(3) in 21

    summarize TOTREXD if CurrencyCrises==1&CountryCode=="RUS"
    replace TOTREXD_mp = _result(3) in 22

    summarize TOTREXD if CurrencyCrises==1&CountryCode=="SWE"
    replace TOTREXD_mp = _result(3) in 23

    summarize TOTREXD if CurrencyCrises==1&CountryCode=="THA"
    replace TOTREXD_mp = _result(3) in 24

    summarize TOTREXD if CurrencyCrises==1&CountryCode=="TUR"
    replace TOTREXD_mp = _result(3) in 25

    summarize TOTREXD if CurrencyCrises==1&CountryCode=="VEN"
    replace TOTREXD_mp = _result(3) in 26




    * Plot

    twoway (scatter out_loss_mp
    TOTREXD_mp
    , mlabel(Cou_Code_O)) ///
    (lfit out_loss_mp CAGDP_mp)



    Click image for larger version

Name:	gr.PNG
Views:	1
Size:	7.6 KB
ID:	1521430






  • #2
    Code:
    * 1) First I take the country average for particular variable (Ths is ok)
    
    bysort country: egen m_GDPGR= mean(GDPGR)
    gen out_loss=.
    replace out_loss= GDPGR-m_GDPGR if GDPGR!=.
    
    by country : summarize out_loss if CurrencyCrises==1
    That's one line too long. The if qualifier does no harm, but the result is missing both with and without it.

    Code:
    * 1) First I take the country average for particular variable (Ths is ok)
    
    bysort country: egen m_GDPGR= mean(GDPGR)
    gen out_loss= GDPGR-m_GDPGR  
    
    by country : summarize out_loss if CurrencyCrises==1

    Code:
    * 3.1) 1st variable
     
    gen out_loss_mp=.
    summarize out_loss if CurrencyCrises==1&CountryCode=="ARG"
    replace out_loss_mp = _result(3) in 1
    
    summarize out_loss if CurrencyCrises==1&CountryCode=="AUS"
    replace out_loss_mp = _result(3) in 2
    
    summarize out_loss if CurrencyCrises==1&CountryCode=="BOL"
    replace out_loss_mp = _result(3) in 3
    
    summarize out_loss if CurrencyCrises==1&CountryCode=="BRA"
    replace out_loss_mp = _result(3) in 4
    
    summarize out_loss if CurrencyCrises==1&CountryCode=="CHL"
    replace out_loss_mp = _result(3) in 5
    
    summarize out_loss if CurrencyCrises==1&CountryCode=="CHN"
    replace out_loss_mp = _result(3) in 6
    
    summarize out_loss if CurrencyCrises==1&CountryCode=="COL"
    replace out_loss_mp = _result(3) in 7
    
    
    summarize out_loss if CurrencyCrises==1&CountryCode=="ECU"
    replace out_loss_mp = _result(3) in 8
    
    
    summarize out_loss if CurrencyCrises==1&CountryCode=="ESP"
    replace out_loss_mp = _result(3) in 9
    
    
    summarize out_loss if CurrencyCrises==1&CountryCode=="FIN"
    replace out_loss_mp = _result(3) in 10
    
    summarize out_loss if CurrencyCrises==1&CountryCode=="GBR"
    replace out_loss_mp = _result(3) in 11
    
    summarize out_loss if CurrencyCrises==1&CountryCode=="GRC"
    replace out_loss_mp = _result(3) in 12
    
    
    summarize out_loss if CurrencyCrises==1&CountryCode=="HUN"
    replace out_loss_mp = _result(3) in 13
    
    
    summarize out_loss if CurrencyCrises==1&CountryCode=="IDN"
    replace out_loss_mp = _result(3) in 14
    
    
    summarize out_loss if CurrencyCrises==1&CountryCode=="ISL"
    replace out_loss_mp = _result(3) in 15
    
    summarize out_loss if CurrencyCrises==1&CountryCode=="KOR"
    replace out_loss_mp = _result(3) in 16
    
    summarize out_loss if CurrencyCrises==1&CountryCode=="MEX"
    replace out_loss_mp = _result(3) in 17
    
    
    summarize out_loss if CurrencyCrises==1&CountryCode=="NOR"
    replace out_loss_mp = _result(3) in 18
    
    
    summarize out_loss if CurrencyCrises==1&CountryCode=="PHL"
    replace out_loss_mp = _result(3) in 19
    
    
    summarize out_loss if CurrencyCrises==1&CountryCode=="POL"
    replace out_loss_mp = _result(3) in 20
    
    summarize out_loss if CurrencyCrises==1&CountryCode=="ROU"
    replace out_loss_mp = _result(3) in 21
    
    summarize out_loss if CurrencyCrises==1&CountryCode=="RUS"
    replace out_loss_mp = _result(3) in 22
    
    summarize out_loss if CurrencyCrises==1&CountryCode=="SWE"
    replace out_loss_mp = _result(3) in 23
    
    summarize out_loss if CurrencyCrises==1&CountryCode=="THA"
    replace out_loss_mp = _result(3) in 24
    
    summarize out_loss if CurrencyCrises==1&CountryCode=="TUR"
    replace out_loss_mp = _result(3) in 25
    
    summarize out_loss if CurrencyCrises==1&CountryCode=="VEN"
    replace out_loss_mp = _result(3) in 26
    That boils down to

    Code:
    by CountryCode: egen out_loss_mpg = mean(out_loss) if CurrencyCrises ==1
    although the results won't go in the same places. (Where did you read the syntax _result(3) ? It's about 20 years out of date, although it still works. )

    The rest looks like a variation on the above.


    Comment


    • #3
      Your command works perfectly but the thing is that I am using them to do a twoway plot afterwards. In my case it works perfectly the problem was that it take so much time.
      The reason why I am doing that is because I want the values just once for a country in order to have a twoplot after the calculation. After using your command I have no clue how to use them plot.

      Comment


      • #4
        It's really not obscure.

        If you use the same kind of code for 3.2 as for 3.1 then the results will inevitably be aligned! They are aligned with CountryCode and CurrencyCrises. For your size of dataset it doesn't matter for the graph if the same value is repeated more than once.

        What might bite is that if the means are based on different numbers of observations. then the lfit may not be what you want.

        You didn't give a data example so I can test nothing. But the total code looks to me like


        Code:
        bysort country: egen m_GDPGR= mean(GDPGR)
        gen out_loss = GDPGR - m_GDPGR if GDPGR!=.
        by CountryCode: egen out_loss_mp = mean(out_loss) if CurrencyCrises ==1
        by CountryCode: egen TOTREXD_mp = mean(TOTREXD) if CurrencyCrises ==1
        scatter out_loss_mp TOTREXD_mp, mlabel(CountryCode) || lfit out_loss_mp CAGDP_mp
        although I can say nothing about CAGDP_mp which just appears out of the blue at the end of your code.

        Comment


        • #5
          Thanks a lot Nick. it works perfectly. You just made a mistake in

          scatter out_loss_mp TOTREXD_mp, mlabel(CountryCode) || lfit out_loss_mp CAGDP_mp

          instead of
          CAGDP_mp should be TOTREXD_mp. Otherwise it works perfectly.

          Comment


          • #6
            I was copying your code from #1....

            Comment

            Working...
            X