Dear Statalisters,
I am working with a set of raw panel data (.xls files), where sheet1 contains data on the following: country, year, and one variable-of-interest (i.e. gdp, aid received, etc.). I imported sheet1 of each raw dataset, cleaned, and merged them on the country and year variables so that they contain: country, year, and all variables-of-interest. I saved this as a tempfile: `latinamerica'. See below.
And sheet2 contains metadata, including a variable called INDICATOR_NAME whose single value is a string, which I would like to assign as the variable label to the variable-of-interest. See below.
I, then, imported sheet2 from each raw dataset and saved the first (and only) value of INDICATOR_NAME (i.e. "GDP (constant 2010 US$)") as a scalar. However, I’m having difficulty assigning this scalar value to the variable-of-interest (i.e. gdp).
I’ve attached my code below. It is with the second loop, that I’m having trouble. I would appreciate any help. Thanks!
I am working with a set of raw panel data (.xls files), where sheet1 contains data on the following: country, year, and one variable-of-interest (i.e. gdp, aid received, etc.). I imported sheet1 of each raw dataset, cleaned, and merged them on the country and year variables so that they contain: country, year, and all variables-of-interest. I saved this as a tempfile: `latinamerica'. See below.
country | year | gdp | aid | [...etc] |
Aruba | 2003 | 1.044e+08 | ||
Aruba | 2004 | -13900000 | ||
Bahamas | 1996 | 6.182e+09 | 1980000 | |
Bahamas | 1997 | 6.309e+09 | 5100000 | |
Bahamas | 1998 | 6.607e+09 | 34540000 |
INDICATOR_CODE | INDICATOR_NAME | SOURCE_NOTE |
NY.GDP.MKTP.KD | GDP (constant 2010 US$) | GDP at purchaser's prices is the sum of gross value... |
I’ve attached my code below. It is with the second loop, that I’m having trouble. I would appreciate any help. Thanks!
Code:
local variables gdp aid […etc] [lines omitted] tempfile latinamerica save `latinamerica' * store variable label as a local (from original excel data) foreach var in `variables' { import excel "$data/wb_`var'", sheet(sheet2) firstrow clear scalar define `var'_l = INDICATOR_NAME[1] di `var'_l } * apply labels use `latinamerica', clear foreach var of varlist `variables' { label var `var' `=scalar(`var'_l)’ save `latinamerica’, replace }
Comment