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" <[email protected]>
To   [email protected]
Subject   Re: st: Importing variable and value labels from txt file
Date   Tue, 20 May 2008 22:01:35 +0100

Many apologies for sending an incomplete reply.
I'll just repeat, and complete, the last example.
(The local macro `list' holds the names of all tables to be processed.)

***********
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''
}

label save `list' using "_ValueLabelsFile", replace

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

The last bit saves all labels that were created in a do-file. Each
label has the name of the corresponding table. If you have the dataset
you are working on in memory, you only need to -run- this do-file, and
then apply the value labels to the variables. I have not automated
this last step as it is not practical for me to do so.

Hope this helps,
Eva
*
*   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–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index