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)



Array