Announcement

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

  • clone, recode and label in loop

    Dear all,

    I would like to create a loop for my gender variable that goes from 1 to 20. In this loop I would like to clone these 20 gender variables and recode them 1 and 0 (1 for female, 0 for male).
    You can see the code I wrote below. However, when I run this Stata says 'variable ch_gender_1 already defined'. I don't know what it means. Can you maybe see where I do mistakes in the code?

    Thanks in advance!

    foreach var of varlist ch_gender_1- ch_gender_20 {
    clonevar `var' = clone_`var'
    recode clone_`var' = (2=1) (1=0)
    label define clone_`var' 1 "Female" 0 "Male"
    }

  • #2
    There is no need for a loop:

    Code:
    recode ch_gender1-ch_gender_20 (2=1 "Female") (1=0 "Male"), prefix(clone_)
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      Martin's advice is the appropriate way of creating the new variables.

      The reason your code failed is because the output of help clonevar tells us the syntax is
      Code:
      Syntax
      
              clonevar newvar = varname [if] [in]
      That is, just like generate, but opposite of rename, the new variable name comes first and the existing variable name comes second.

      You have them backwards. Take heart, I often get them backwards when using rename.

      Comment


      • #4
        The code in #2 misses a tiny detail

        Code:
        recode ch_gender1-ch_gender_20 (2=1 "Female") (1=0 "Male"), prefix(clone_) label(gender)
        When you define a value label for multiple variables, recode forces you to specify one name for that value label, which is usually a good idea. recode will not copy the variable labels and characteristics, either. I believe that recode will also not work well with multilingual datasets. If any or all of this is wanted, clonevar is a suitable solution.

        Best
        Daniel
        Last edited by daniel klein; 26 Feb 2020, 09:41.

        Comment


        • #5
          Thanks a lot guys. Your answers were really helpful!

          Best
          Martin

          Comment


          • #6
            Here is another way to do it.

            (The main point of clonevar here is just to copy variable labels too, as you're changing the values and the value labels. If not then generate is fine.)

            Code:
            foreach var of varlist ch_gender_1- ch_gender_20 {
                clonevar clone_`var' = `var'
             
                * note that missings will remain missings!
                replace clone_`var' = clone_`var' - 1
            }
            
            label define female 1 "Female" 0 "Male"
            label val clone_* female

            Comment

            Working...
            X