Announcement

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

  • Loops with replacing and relabeling

    Hi!
    I am currently working on a fairly large dataset (with 700 + observations). I want to give different values for some of the variables in the data set. The data is based on a questionnaire, and is currently coded as for example:
    x1=-2 for "refused"
    x1=-1 for "don't know"
    x1=1 for "yes"
    x1=5 for "no"
    I want to replace the variables' values, without generating new variables, and have tried the following command:

    Code:
    foreach v of varlist rh040_ rh042_ rh780_ rh792_ br002_ {
      replace `v'=1 if `v'==1 
      replace `v'=0 if `v'==5 | `v'==-2 | `v'==-1
    }
    This seems to work, however, I get the labels "0" and "yes" instead of 0 and "no" and "yes" - as seen below
    Code:
    tab rh040_ 
    
    
        
         x1      |      Freq.     Percent        Cum.
    ------------+-----------------------------------
              0   |      1,185       60.58       60.58
            Yes |        771       39.42      100.00
    ------------+-----------------------------------
          Total |      1,956      100.00

    I've tried adding the following:

    Code:
    foreach v of varlist rh040_ rh042_ rh780_ rh792_ br002_ {
      replace `v'=1 if `v'==1 
      replace `v'=0 if `v'==5 | `v'==-2 | `v'==-1
      rename `v'="No" if `v'=0
    }
    but then i get "Syntax error".

    I hoped I expressed myself in an understandable way.

    Thank you

  • #2
    Let's split this into parts. First,

    Code:
     replace `v'=1 if `v'==1
    does no harm but is not needed. There is no point to replacing 1 with 1.

    Second, changing the values has no side-effects for value labels. You need to change them directly.

    Third, your rename command is misdirected. It has nothing to do with value labels, but is only to do with variable names.

    Fourth, rename does not allow if qualifiers and if it did yours would be illegal as == is essential to test for equality.

    All that said, you would probably find recode a one-stop shop for all you need here.


    a fairly large dataset (with 700 + observations)
    would cause some good-natured amusement among many readers.

    Comment


    • #3

      would cause some good-natured amusement among many readers.
      Haha, my bad, I meant 7000. I just missed a zero


      I tried using the recode command, however I seem to run into the same problem as before, where I get "0" and "yes".

      Code:
      recode rh040_ (-2 -1 5=0)
      When I try this instead
      Code:
       recode rh040_ (-2 -1 5=0 "No")
      Stata reports the error "Rules defining value labels not allowed when overwriting a variable"




      Comment


      • #4
        Putting missing answers ("refused" and "don't know") in the same category as a "no" answer is almost always a very bad idea.

        You can modify the respective value labels with label define. Be careful, though. Value labels can be attached to more than one variable and modifying them naturally affects the output for all variables with the same value label.

        By the way, elabel (SSC) can recode value labels (and/or modify them in a systematic way).

        Here are examples.

        Best
        Daniel
        Last edited by daniel klein; 25 Feb 2020, 15:24.

        Comment


        • #5
          Thank you for your help, Daniel. I will have a look at the examples

          Comment


          • #6
            Here is a sketch:

            Code:
            // variables to be recoded
            local vars rh040_ rh042_ rh780_ rh792_ br002_
            
            // recode value label of one variable using -elabel- (SSC)
            elabel recode (rh040_) /// refers value label of rh040_
                (5 = 0)            /// change 5 to 0; keep label
                (-1 -2 = .a .b)    /// change missing values; keep labels
                , define((`vars'):yesno) // define new value label -yesno-
                                         // attach to all variables
            
            // recode the variables that now have -yesno- attached
            recode `vars' `r(rules)' // r(rules) is (5=0) (-1=.a) (-2=.b)
            Obviously, you want to make sure that those variables all have the same coding that you want to change in the same way.

            Best
            Daniel
            Last edited by daniel klein; 26 Feb 2020, 00:14.

            Comment


            • #7
              Once again; thank you very much - the sketch helped a lot

              Comment


              • #8
                I recommended recode in #2 against my personal tastes. I did not know -- or did not remember -- its rule as reported in #3. Sorry about that detail.

                Comment

                Working...
                X