Announcement

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

  • Assigning variable label when generating a new one

    Hi everyone,
    I am trying to generate a new variable named "new" using an existent variable named "old". However, wonder to know how can assign the label for the old variable to the new one. I am doing this using a loop across around 50 variables.
    Thanks,
    NM

  • #2
    I would use -clonevar-
    Code:
    clonevar newvar = oldvar
    in a loop something like:
    Code:
    foreach var of var varlist {
    clonevar `var'_new = `var'
    }

    Comment


    • #3
      if new not equal to old,
      Code:
      foreach v of var old* {
      gen new_`v' = `v' * 2
      local lab : var lab `v' 
      lab var new_`v' "`lab'"
      }

      Comment


      • #4
        There is also the non-documented _crcslbl (crc is a reference to Computing Resources Center, the original company name).

        Code:
        *! version 1.1.0  19dec1998
        program define _crcslbl /* varname varname */
                version 6
                args dst src
                local w : variable label `src'
                if `"`w'"' == "" {
                        local w "`src'"
                }
                label variable `dst' `"`w'"'
        end
        Example

        Code:
        . clear
        
        . set obs 1
        Number of observations (_N) was 0, now 1.
        
        . gen y = 42
        
        . label var y "interesting"
        
        . gen x = y
        
        . _crcslbl x y
        
        
        . d
        
        Contains data
         Observations:             1                  
            Variables:             2                  
        -----------------------------------------------------------------------------------------
        Variable      Storage   Display    Value
            name         type    format    label      Variable label
        -----------------------------------------------------------------------------------------
        y               float   %9.0g                 interesting
        x               float   %9.0g                 interesting
        -----------------------------------------------------------------------------------------
        That said, I was the first author of clonevar. I found I sometimes wanted a version of an existing variable that was almost the same, but not identical. So, the first step was to clone it, and then to make changes. It's now an official command, and I have no idea how anyone else uses it.

        Two variables having the same variable label smacks of redundancy if they really are identical and could be very confusing if they are not (e..g. there could be a problem in telling apart different graphs).


        Comment


        • #5
          Nick Cox I use -clonevar- quite often because while doing data cleaning/management I manipulate the new variable afterwards but want to keep the original in order to check correctness of my manipulation. Or because I still need it for other purposes...

          Comment


          • #6
            Marc Kaulisch

            Fine, but not different in principle. You want to change a variable, even if only temporarily. Apart from the "other purposes"....

            Comment

            Working...
            X