Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Changing many value labels at once to make datasets fit for appending (if value labels contain dots)

    Dear all,

    I am trying to append several datasets which contain a large number of variables.
    In some datasets, the value labels have the same name even though the underlying information is not the same.

    Thus, I am looking for a way how to "rename" the value labels in one go such that afterwards I can append without problems.

    In a previous thread (https://www.stata.com/statalist/arch.../msg00879.html), I found this code:
    Code:
    desc
    ds, has(vallabel)
    local vars `r(varlist)'
    foreach var of local vars {
        // get the name of the value label for variable `var'
        local labname : value label `var'
        // create a copy with name prefix + oldname
        label copy `labname' W1_`labname', replace
        // assign that copy to variable `var'
        label value `var' W1_`labname'
    }
    // see the new label names
    desc
    This is immensely helpful.

    However, some of my value labels are called for example "A01.2". I suspect that the dot in the value label causes troubles, as the code above does not work in these cases.

    Do you have any suggestions how to circumvent this problem and rename the value labels such that I can append the datasets (afterwards the manual work starts, but then at least I have all info already in one dataset and can use lookfor etc)

    Thank you very much in advance!!

    Best
    Last edited by Johannes Muller; 16 May 2018, 18:11. Reason: value labels

  • #2
    Welcome to Statalist.

    It would be interesting to know how the value labels were created originally. Value labels names are supposed to satisfy the Stata rules for names, for which section 11.3 of the Stata User's Guide PDF tells us

    A name is a sequence of 1 to 32 letters (A–Z, a–z, and any Unicode letter), digits (0–9), and underscores ( ).
    The challenge to solving your problem is that
    Code:
    label copy A01.2 W1_A01.2, replace
    will fail not only because the new name is not allowed, but also because the current name will not be recognized as an allowable name, and will throw a syntax error. For example, I believe you will find that
    Code:
    label list A01.2
    will not run, but will report that A01.2 is an invalid name. I would be interested in knowing if
    Code:
    label dir
    notices the unallowable value label names.

    I hope someone else can rise to this interesting challenge for working around Stata. When I started typing I thought I had an answer, but it was all downhill from there.


    Comment

    Working...
    X