Announcement

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

  • Renaming every value label to match the variable name

    Hi everyone,
    I'm using the following code to rename every value label in my data set to be the same as the variable name. EG: for variable respage the value label would also be called respage.

    Code:
    |
    *Get all vars with value labels
    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 of variable
        label copy `labname' `var', replace
    
    
        // assign that copy to variable `var'
        label value `var' `var'
    
    }
    I modified code I found from this old statalist post: https://www.stata.com/statalist/arch.../msg01077.html


    When I run this code, I get the error "insufficient memory." I've dropped every variable I can and unused labels, but still no luck. Is there any way I can rename all the value labels that currently exist in my code to match the variable names? Thanks!

  • #2
    The error that you are getting seems weird but we cannot say much more without more information. Show us the output of

    Code:
    version
    describe , short
    memory
    In general, the code that you suggest is also implemented in valtovar (SSC). The code will fail when a variable, say never_married, has a value label with the name of another variable, say married, attached and if never_married appears in order after married. Here is this situation

    Code:
    // example data
    sysuse nlsw88 , clear
    
    // demonstrate the problem
    label define married 0 "got" 1 "cha"
    label values never_married married
        /*
            note that nver_married appears after married
            if that was not the case, there would not be a problem
        */
    describe married never_married
    label list marlbl married
    
    //  now apply your code
    // (or use -valtovar-)
    
    *Get all vars with value labels
    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 of variable
        label copy `labname' `var', replace
    
    
        // assign that copy to variable `var'
        label value `var' `var'
    
    }
    
    // demonstarte the problem
    describe married never_married
    label list married never_married
    The problem might be even less apparent when you have a multilingual dataset.

    Why do you want this in the first place?

    Best
    Daniel

    Comment

    Working...
    X