Announcement

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

  • Rename variable labels containing "."

    Based on this post in the Stata forum, I have been able to rename all variables containing a dot "." with an underscore thanks to Joseph Coveney. Funnily enough for the same dataset, the Latinobarometer 2018, I now want to repeat the command, this time for the labels as they also contains dots.
    I have tried to modify the code from the original post, unfortunately unsuccessful.
    Original code:
    Code:
    foreach varname of varlist * {
        local ++i
        if ( "`varname'" != ustrtoname("`varname'") ) {
            mata : st_varrename(`i', ustrtoname("`varname'") )
        }
    }
    My attempt, which does not work:
    Code:
    foreach label of label dir * {
        local ++i
        if ( "`varname'" != ustrtoname("`label'") ) {
            mata : st_varrename(`i', ustrtoname("`label'") )
        }
    }
    Last edited by Felix Kaysers; 23 Jun 2023, 15:03.
    Cheers,
    Felix
    Stata Version: MP 18.0
    OS: Windows 11

  • #2
    -foreach label of label dir * {- is not valid Stata syntax. In -foreach ... of ... {- commands, the token that immediately follows -of- must be either varlist, numlist, or local. Nothing else is allowed.

    Also local ++i will fail because local macro i itself has not been defined previously in the code.

    Try this:
    Code:
    ds, has(varlabel)
    foreach v of varlist `r(varlist)' {
        local newname = ustrtoname(`"`:var label `v''"')
        rename `v' `newname'
    }

    Comment


    • #3
      Clyde Schechter 's little list of what is allowed after of in a foreach call omits global and newlist. Long-time members here will know that Clyde wants to discourage the use of globals, as do I, but asking to loop through the elements of a global macro is allowed syntax.

      The main point is well taken: the syntax in #1 is quite wrong.

      Comment


      • #4
        On a more fundamental level, it is not clear what the goal is here. The tile and code in #1 target variable names; the description asserts that names have already been changed and the goal is to change labels. Which labels? Variable labels? Value labels? Value label names? We do not know. Either way, st_varrename() will not change anything but variable names.


        On variable labels: There is no need to remove dots from variable labels. Dots, just like any other character, are allowed in labels. If you wanted to change variable labels, something like this should do:

        Code:
        foreach var of varlist * {
            
            local oldlabel : variable label `var'
            local newlabel : subinstr local oldlabel "." "_" , all
            label variable `var' `"`newlabel'"'
            
        }

        On value labels (i.e., integer-to-text mappings): Again, the text associated with values (i.e. the value labels) may contain any character, including dots. Changing (all) value labels with standard Stata commands is a bit cumbersome; thus, I use elabel (from SSC) here:

        Code:
        elabel define * (=#) (=subinstr(@, ".", "_", .)) , modify

        On value label names: Those cannot contain dots. If they do, there is a way to fix that, too. Get back if that is what you want.
        Last edited by daniel klein; 26 Jun 2023, 00:56.

        Comment


        • #5
          Thank you to eveyone. I was indeed referring to the variable labels!
          Clyde Schechter , your code worked for my purposes. Thank you!
          Cheers,
          Felix
          Stata Version: MP 18.0
          OS: Windows 11

          Comment

          Working...
          X