Hi all,
I am new here and somewhat new to Stata. I have a panel dataset for 31 countries with 14 indicators for each. I have a variable "countrycode" and want to create a new dataset for each "countrycode" keeping the 14 variables that are relevant to each using a loop. I tried to save each as a separate dta but want I really want is to save each country's file as a separate excel sheet in the same excel. Here is what I attempted to do but only got a blank dataset with just the correct headings. All help is welcomed! Huge thanks!
import excel using "${output}/dataset.xlsx", clear first sheet("ALLCOUNTRIES") /*I already set a global "output"/
#delimit ;
local countrylist "
ARG
ATG
BLZ
BHS
BOL
BRA
CHL
COL
CRI
DMA
DOM
ECU
GRD
GTM
GUY
HTI
HND
JAM
KNA
LCA
MEX
NIC
PAN
PER
PRY
SLV
SXM
SUR
TTO
URY
VCT
" ;
#delimit cr
foreach var of varlist countrycode {
forvalues i = 1/31 {
keep if countrycode == "`i'"
}
}
save "${output}/`i'_countrydata.dta", replace
I am new here and somewhat new to Stata. I have a panel dataset for 31 countries with 14 indicators for each. I have a variable "countrycode" and want to create a new dataset for each "countrycode" keeping the 14 variables that are relevant to each using a loop. I tried to save each as a separate dta but want I really want is to save each country's file as a separate excel sheet in the same excel. Here is what I attempted to do but only got a blank dataset with just the correct headings. All help is welcomed! Huge thanks!
import excel using "${output}/dataset.xlsx", clear first sheet("ALLCOUNTRIES") /*I already set a global "output"/
#delimit ;
local countrylist "
ARG
ATG
BLZ
BHS
BOL
BRA
CHL
COL
CRI
DMA
DOM
ECU
GRD
GTM
GUY
HTI
HND
JAM
KNA
LCA
MEX
NIC
PAN
PER
PRY
SLV
SXM
SUR
TTO
URY
VCT
" ;
#delimit cr
foreach var of varlist countrycode {
forvalues i = 1/31 {
keep if countrycode == "`i'"
}
}
save "${output}/`i'_countrydata.dta", replace
Comment