Been quoit for awhile, here is a tip on using and getting variable and value labels
Labeling variables and values is useful for understanding what your underlying data represents. This is great while you are
in the STATA environment. But many times you may want to use this information in more dynamic ways.
For example, lets say that I am using the autos database and I want to output a simple table of frequencies that looks like this:
Car type Freq.
Domestic 52
Foreign 22
In order to do this I have to get the information stored in my variable labels and value label, so follow along:
clear
set more off
sysuse auto
label list
tempname my_table
file open `my_table' using ///
"c:\my_table.xls", write replace
** This is where I get the variable label **
** in long hand **
** local var_name : variable label foreign **
local var_name : var l foreign
** Now that this is in a local **
** I can use it anywhere **
** so let's write it to our file **
file write `my_table' ("`var_name'") _tab ("Freq.") _n
** Now lets get our frequencies **
** and value labels First get the **
** name of the label value **
local nm_label : val l foreign
forvalues x=0(1)1 {
quietly sum foreign if foreign == `x'
** Now to get the label values **
** for 0 "Domestic" and 1 "Foreign" **
** in the value label origin **
local val_name : label `nm_label' `x'
file write `my_table' ///
("`val_name'") _tab (r(N)) _n
}
file close `my_table'
This is a powerful way to export data in a meaningful fashion and can save you a lot of time. Recall that after the sum, there are a number
of values that we can recover. Type return list, if you need other descriptive statics use the detail option for the
sum command. Also you can get post regression estimates through ereturn list after you run a regression. If you
are familiar with using matrices in STATA then you can get all of your coefficents, etc.
More on that later
Happy coding monkeys...
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment