Saving variable labels before -collapse-


collapse literally collapses the dataset  into a dataset of summary statistics. After collapse, the dataset in memory is lost unless -preserve- was declared. Also, the labels of all variables in clist are replaced with (stat) variable name, where stat can be mean, sum, etc. (see help collapse).

Instead of retyping all the variable labels, you can use the extended macro function var label (see help extended_fcn) to  save the variable label of each variable in the varlist into a local macro before collapse. Restore the labels using label var (see help label). Example:
sysuse auto, clear

foreach var of varlist * {
    local vlab`var': var label `var'
    }

collapse price - gear_ratio, by(foreign)

foreach var of varlist * {
    label var `var' "`vlab`var''"
    }   

File names in a local macro


Last week I needed to convert a number of Stata data files into text files so that they can be uploaded to Googledocs (why Googledocs is another story).  If my file names have a specific pattern, such as:

data2001.dta
data2002.dta
.
.
.
data2009.dta

-forvalues- would have done the trick.

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
forvalues y=1/9{
use “data200y'.dta", clear
outfile using "data
200y’.txt”, dictionary replace
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

My file names, however, look like the file names you get when you type -sysuse dir-:

auto.dta
autornd.dta
bplong.dta
bpwide.dta
cancer.dta

If there were few of them, it would have been alright to place them in a local macro by listing all the file names.

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
local datafiles auto autornd bplong bpwide cancer
foreach file of local datafiles{
use <em>file</em>'.<em>dta</em>, clear
<strong>outfile using </strong>"
file.txt“, dictionary replace
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

The latter, however, is not efficient since I have to manually type so many file names so that they can be stored in a macro. Here is where Stata’s extended macro functions (-help extended_fcn-) comes to the rescue. Stata has exactly the right function for what I wanted to do.

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
local datafiles : dir . files  “*.dta”
foreach file of local datafiles{
local filenew : subinstr local file “.dta” “.txt”
use file', clear
<strong>outfile using </strong>
filenew‘, dictionary replace
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

local datafiles : dir . files  “*.dta”— makes a list, named datafiles, of all files with the extension .dta in the current directory .

local filenew : subinstr local file “.dta” “.txt”— replaces the file extensions of the file names from .dta to .txt and makes a new list, named filenew, of the new file names.

Generating scalars for coefficients or standard errors after regression


Besides displaying output in the results window, Stata stores results that you can use as inputs to subsequent commands. We have shown examples of using saved results in Writing Greek letters and other symbols in graphs and Ways to count the number of unique values in a variable where we used results stored in r(). In this post, we will use estimation results saved in e() after -regress- to generate a scalar (or a local macro) for coefficients and standard errors. (See note below)

sysuse auto /* opens example data auto.dta */

reg price mpg /* estimates the equation price = b0 + b1*mpg + e ; to display all saved results after -regress-, type “ereturn list” */

matrix b=e(b)
matrix V=e(V)
/* defines matrix b equal to the row vector of estimated coefficients, e(b); and  matrix V equal to the variance-covariance matrix, e(V). */

matrix list b // or matrix list e(b)
matrix list V // or matri list e(V)
/* displays b and V */

scalar c_mpg=b[1,1]
scalar se_mpg=sqrt(V[1,1])
/* defines scalar c_mpg equal to element (1,1)  of vector b; and defines scalar se_mpg equal to the square root of element (1,1) of matrix V */

scalar list /* displays c_mpg and se_mpg */

Alternatively, you may define c_mpg and se_mpg as local macros instead of scalars:

local c_mpg=b[1,1]
local se_mpg=sqrt(V[1,1])
display “c_mpg= “c_mpg' " ; se_mpg= "se_mpg’


Note: Where Stata saves the results depends on the type of command executed. Stata commands can be classified into 5 classes—r-, e-, s-, n-, and c-class commands:

r-class: general commands that do not require parameter estimation (example: -summarize-); results are stored in r()
e-class: parameter estimation commands (example: -regress-); results are stored in e()
s-class: programming commands that assist in parsing; results are stored in s()
n-class: commands that do not save other results except those that are explicitly generated (example: -generate-); no results stored
c-lass: stores system parameters and some constants (example: c(pi) returns the value of pi); values are stored in c() (try typing “creturn list”)