Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: How to attach multiple labels/notes to variables?


From   David Airey <[email protected]>
To   [email protected]
Subject   st: RE: How to attach multiple labels/notes to variables?
Date   Fri, 9 Apr 2004 19:51:46 -0500

I missed that Michael Blasnik replied to my inquiry too. Thank you for posting your program!

-Dave



Your description does not make it clear precisely
how your extra file is organised, but from the
information you provide the problem resembles
that posed by David Airey on 27 March.

> I was sent two files, one containing data that could be
> imported using
> insheet, with variable names in the first row, and the other
> containing
> variable names and a short explanation of each variable. Is there an
> easy way to label the variables with the explanations without typing
> them in by hand using the label variable command?

I suggested

> The easiest way, I guess, is to copy the
> second file to make a do file. Each line
> should then be prefixed with
>
> label var
>
> and each explanation should be bracketed
> within " ". With a decent text editor it
> should be fairly straightforward.

and Michael Blasnik suggested

> You can do this within Stata -- Here's a program:

> program define varlabmake
> version 8.2
> syntax varlist (max=2), saving(str)
> if substr("`saving'",-3,.)!=".do" local saving "`saving'.do"
> tempname f
> tokenize `varlist'
> file open `f' using "`saving'", write text replace
> local N=_N
> forvalues i=1/`N' {
> local var=`1'[`i']
> local lab=`2'[`i']
> if "`var'"!="" & "`lab'"!="" {
> file write `f' "label var `var' " _char(34) "`lab'" _char(34) _n
> }
> }
> file close `f'
> end
>
> *note:  the -file write- command may wrap, depending on email issues

> Then you can just

> varlabmake varname varlabel, saving(varlabels.do)

> to create your do file.  It doesn't have much data checking (does the
> varname variable contain valid varnames? etc..) but it should work.

Michael's program assumes that you have already read into Stata your
names, as one variable, and your labels as another variable. That
will in most cases depend on some prior editing by you.
*
*   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