| |
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]
st: accessing a variable's value WRT its attached value label
Suppose I have a numeric variable y, and suppose it has an attached
value label which maps 1 -> "yes". Now, suppose I want to count
those cases for which y "is" yes (I'm clearly using "is" in the loose
sense here, but I think the meaning is clear from the context). I
could of course type:
count if y == 1
however if the encoding of the variable changes (i.e., if the value 1
no longer refers to "yes"), this will (silently) no longer give me
what I want. Alternatively (and in many cases better), I could write:
count if y == "yes":y_label
where y_label is the name of the corresponding value label. In this
case, if the encoding changes I'm still ok (assuming that "yes" is
still used in the label). But what if the *name* of the value label
changes? I could do this:
count if y == "yes":`:val l y'
but that's a bit ugly. So, my question is, is there a more readable
alternative to the expression above?
For those interested in the use case, I am working on a large project
involving a complicated data set and a lot of code to manage the
data. Right now, most variables have a dedicated value label with
the same name as the variable itself. However, in the future, we may
wish to economize by replacing multiple labels that have equivalent
definitions with a single label. This new label will clearly have a
different name than the previous label(s) for at least one of the
variables involved. What I'm trying to do is to make sure that the
code downstream will not break if such changes are made.
-- Phil
*
* 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/