Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: Importing variable and value labels from txt file


From   "Eva Poen" <eva.poen@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Importing variable and value labels from txt file
Date   Tue, 20 May 2008 21:53:21 +0100

2008/5/20 Guillermo Villa <guillermo@baphealth.com>:
> I would appreciate it if you could tell me more on your previous
> experience with Access tables, the format they should display, and the
> code you employed to match data and labels in Stata.

Guillermo,

for the variable labels, it's quite easy, really. I have an Access
table with two columns: VarName and VarLabel, holding the variable
names and their labels, respectively. I then wrote a little do-file
which reads this table into memory in via -odbc-. It takes each
observation (i.e. a variable name and its label) and creates a row in
new do-file:

******************************

qui count
local n = r(N)
qui compress

file open _VarlabelsFile  using _VarlabelsFile.do, write replace

file write `handle' "*** This file was created by VarLabels.do" _n
file write `handle' `"*** on `= c(current_date)' at `= c(current_time)'. "' _n
file write `handle' "*** " _n
file write `handle' "********************************************** " _n
file write `handle' _n
file write `handle' _n


forvalues x = 1/`n' {
    file write `handle' "capture label variable `=VarName[`x']' \`"
    file write `handle' `"" `=VarLabel[`x']' ""'
    file write `handle' "'" _n
}

file close _VarlabelsFile

******************************

This results in a file _VarlabelsFile.do wich reads

capture label variable SessionID "Unique session ID"
capture label variable Sequence "Number in sequence of the current treatment"

etc.

It is then a matter of running this do-file over the data, and all
labels will be applied accordingly.
The reason I put -capture- in front of each line has to do with the
project I am working on; in some cases, I work on subsets of the data
where some variables might not exist, and I want this do-file to run
smoothly anyhow.


With value labels, it's a bit more complicated. You could have a
different Access table for each label. Each table would have two
columns, Value and label. This version is the easiest. Alternatively,
you could have just one table for all your labels, and have an
additional column that states the name of each label. I actually have
a mixture of the two.

What you then have to do is read in these tables via -odbc- one after
the other (the best is to use a loop), and, for each table,
observation by observation, read in the value and label, and construct
a label in Stata syntax from them. Here is an example for the case of
different Access tables for different labels:

foreach table of local list {
    drop _all
    qui odbc query "RawData"
    odbc load, table("`table'") dialog(complete) dsn("RawData")

    /* get number of label items */
    qui count
    local n = r(N)
    /* get variables */
    qui ds
    local v`table' = r(varlist)
    * important: Code needs to be in position 1, label in position 2
    local c`table': word 1 of `v`table''
    local l`table': word 2 of `v`table''

    forvalues x = 1/`n' {
        /* loop over all obs and fetch var[obs] */
        local a`table' = `c`table''[`x']
        local b`table' = `l`table''[`x']
        local b`table' `" "`b`table''" "'
        /* combine to a label */
        local label`table' `" `label`table'' `a`table'' `b`table'' "'
    }
    capture label drop `table'
    label define `table' `label`table''
*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index