Announcement

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

  • Looping and Recoding

    looping and recoding
    Hello, I am trying to reverse code pt_bbq1_ - pt_bbq14_ below

    There are 5 possible response and I want to make transform a score of 5 into a score of 1 and a score of 4 into a score of 2, ect..
    I would like to use looping to do this rather than recode each variable.

    I have tried below but does not work. I believe it is my use of * but I am not sure how to make it work. Would sure appreciate help with a solution...


    foreach var of varlist ///
    pt_bbq1_ - pt_bbq14_ {
    gen `var' = 6 - pt_bbq*_
    }


    clear
    input int record_id byte(pt_bbq1_ pt_bbq2_ pt_bbq3_ pt_bbq4_ pt_bbq5_ pt_bbq6_ pt_bbq7_ pt_bbq8_ pt_bbq9_ pt_bbq10_ pt_bbq11_ pt_bbq12_ pt_bbq13_ pt_bbq14_)
    2 4 4 . 1 4 4 3 4 3 4 4 3 4 4
    2 . . . . . . . . . . . . . .
    4 1 1 2 1 3 2 2 2 2 4 1 3 3 3
    5 4 1 4 4 5 4 1 2 4 2 2 4 3 5
    5 . . . . . . . . . . . . . .
    5 2 2 4 2 5 3 1 1 4 1 2 4 3 4
    5 1 1 2 1 5 4 1 1 4 1 1 1 4 4
    8 4 3 5 1 3 5 3 3 3 3 2 5 4 5
    8 . . . . . . . . . . . . . .
    8 3 4 4 2 4 4 3 4 2 3 3 5 3 4
    9 4 3 4 3 4 3 1 1 3 3 3 3 4 4
    9 . . . . . . . . . . . . . .
    9 1 1 1 1 5 1 1 1 3 1 1 1 3 1
    9 1 1 3 2 5 2 1 1 5 1 1 3 4 3
    13 5 3 3 3 2 5 5 5 3 3 3 3 2 3
    13 . . . . . . . . . . . . . .
    13 3 1 1 1 4 1 1 1 1 1 1 1 4 1
    13 1 1 1 1 5 1 1 1 3 1 1 1 3 1
    14 3 3 4 3 3 5 3 2 3 4 2 4 3 4
    14 . . . . . . . . . . . . . .
    14 3 3 4 3 3 5 4 3 2 4 3 4 3 4
    14 2 3 3 2 3 5 4 3 2 3 3 4 4 4
    16 2 1 2 2 4 2 2 1 3 2 1 2 3 4
    16 . . . . . . . . . . . . . .
    16 2 1 2 2 5 1 1 1 1 1 1 1 3 1
    17 3 3 4 2 3 4 3 2 3 2 4 4 4 5
    19 3 1 1 2 3 5 1 1 4 2 1 1 4 1
    19 . . . . . . . . . . . . . .
    19 3 2 2 3 3 4 2 2 4 2 1 1 2 2
    19 3 1 1 2 3 4 3 1 4 2 1 3 3 3
    21 2 1 3 2 5 2 1 3 5 1 1 3 1 3
    21 . . . . . . . . . . . . . .
    21 1 1 2 3 5 3 1 3 3 1 3 3 3 3
    21 1 1 2 2 5 1 1 2 4 1 1 4 2 4
    22 1 1 3 2 4 5 1 2 5 2 2 4 4 4
    22 . . . . . . . . . . . . . .
    22 1 2 2 3 4 5 1 1 5 1 1 3 3 4
    22 1 3 2 1 4 4 1 1 5 1 2 3 4 4
    23 1 1 4 2 5 2 2 1 3 1 1 2 3 4
    23 . . . . . . . . . . . . . .
    23 1 1 3 1 5 1 3 1 2 1 1 3 3 4
    23 1 1 2 1 5 1 1 1 3 1 1 3 2 3
    25 3 2 2 2 5 2 1 1 4 2 1 3 2 3
    25 . . . . . . . . . . . . . .
    25 3 2 3 3 5 3 2 2 3 2 2 2 3 3
    25 4 3 3 2 5 3 2 1 4 2 2 2 3 2
    26 2 2 4 2 5 5 1 3 4 2 1 4 4 4
    26 . . . . . . . . . . . . . .
    26 2 2 2 2 5 4 1 1 4 2 1 3 4 4
    26 1 2 2 2 4 4 1 1 4 2 2 3 2 3
    27 1 1 1 1 4 3 3 1 3 1 1 3 3 4
    27 . . . . . . . . . . . . . .
    27 1 2 2 1 4 4 2 2 2 2 1 2 4 3
    27 1 2 2 1 4 2 2 2 3 2 2 2 4 3
    28 3 3 4 2 3 5 1 2 3 2 1 2 2 3
    28 . . . . . . . . . . . . . .
    28 3 4 4 3 3 5 1 2 3 3 1 5 3 5
    28 4 2 5 5 3 5 1 1 3 1 1 5 3 5
    29 2 2 4 2 4 3 2 3 2 3 2 3 3 3
    29 . . . . . . . . . . . . . .
    29 2 2 4 2 4 2 1 2 3 2 1 3 2 3
    29 1 1 2 1 3 2 2 1 3 2 1 3 3 2
    32 4 3 4 3 4 5 1 4 3 4 3 3 5 4
    32 . . . . . . . . . . . . . .
    32 4 5 5 4 3 4 1 2 3 4 2 2 5 4
    32 4 4 4 3 3 5 2 4 3 3 4 4 5 4
    33 1 4 2 1 4 4 1 2 5 3 1 3 5 3
    33 . . . . . . . . . . . . . .
    33 2 2 4 1 5 4 2 1 5 2 1 2 4 2
    34 1 1 1 1 3 1 1 1 1 1 1 1 3 2
    34 . . . . . . . . . . . . . .
    34 2 1 2 2 5 1 1 1 2 1 1 1 2 3
    34 1 1 1 1 5 1 1 1 3 1 1 1 1 1
    36 1 1 4 1 4 2 3 1 3 3 3 5 5 4
    36 . . . . . . . . . . . . . .
    36 1 1 4 1 2 5 2 1 3 2 2 4 3 3
    36 1 1 4 1 5 3 3 2 3 3 2 4 3 3
    38 2 2 4 2 5 4 2 3 4 3 2 2 4 4
    38 . . . . . . . . . . . . . .
    38 2 2 4 2 5 4 2 4 4 3 1 4 3 3
    38 4 3 4 2 5 5 3 4 4 3 2 4 4 3
    40 1 2 3 2 4 5 2 3 3 2 3 1 2 3
    42 2 3 4 2 3 5 4 2 4 2 2 4 2 3
    42 . . . . . . . . . . . . . .
    42 2 2 4 2 4 3 2 2 4 1 1 3 2 4
    42 1 1 2 1 5 4 1 1 4 1 1 2 1 4
    43 1 3 3 1 4 4 3 1 4 1 1 3 4 3
    43 . . . . . . . . . . . . . .
    43 1 1 2 1 4 2 2 1 4 2 1 3 4 3
    43 1 1 1 1 3 1 2 1 4 1 1 2 4 2
    45 1 1 3 1 4 3 3 2 4 2 1 2 4 2
    45 . . . . . . . . . . . . . .
    45 1 1 3 2 4 2 2 2 3 1 1 2 3 4
    45 2 1 3 1 4 2 1 3 3 2 1 2 2 3
    46 1 3 3 1 3 3 2 2 3 3 2 2 2 3
    46 . . . . . . . . . . . . . .
    46 1 2 3 1 4 4 2 1 4 2 1 2 3 2
    46 1 1 4 1 4 3 1 2 3 3 2 3 4 4
    47 1 1 1 1 1 5 3 1 5 1 1 3 5 5
    47 . . . . . . . . . . . . . .
    end
    label values pt_bbq1_ pt_bbq_1_
    label def pt_bbq_1_ 1 "Completely Disagree", modify
    label def pt_bbq_1_ 3 "Neutral", modify
    label def pt_bbq_1_ 5 "Completely Agree", modify
    label values pt_bbq2_ pt_bbq_2_
    label def pt_bbq_2_ 1 "Completely Disagree", modify
    label def pt_bbq_2_ 3 "Neutral", modify
    label def pt_bbq_2_ 5 "Completely Agree", modify
    label values pt_bbq3_ pt_bbq_3_
    label def pt_bbq_3_ 1 "Completely Disagree", modify
    label def pt_bbq_3_ 3 "Neutral", modify
    label def pt_bbq_3_ 5 "Completely Agree", modify
    label values pt_bbq4_ pt_bbq_4_
    label def pt_bbq_4_ 1 "Completely Disagree", modify
    label def pt_bbq_4_ 3 "Neutral", modify
    label def pt_bbq_4_ 5 "Completely Agree", modify
    label values pt_bbq5_ pt_bbq_5_
    label def pt_bbq_5_ 1 "Completely Disagree", modify
    label def pt_bbq_5_ 3 "Neutral", modify
    label def pt_bbq_5_ 5 "Completely Agree", modify
    label values pt_bbq6_ pt_bbq_6_
    label def pt_bbq_6_ 1 "Completely Disagree", modify
    label def pt_bbq_6_ 3 "Neutral", modify
    label def pt_bbq_6_ 5 "Completely Agree", modify
    label values pt_bbq7_ pt_bbq_7_
    label def pt_bbq_7_ 1 "Completely Disagree", modify
    label def pt_bbq_7_ 3 "Neutral", modify
    label def pt_bbq_7_ 5 "Completely Agree", modify
    label values pt_bbq8_ pt_bbq_8_
    label def pt_bbq_8_ 1 "Completely Disagree", modify
    label def pt_bbq_8_ 3 "Neutral", modify
    label def pt_bbq_8_ 5 "Completely Agree", modify
    label values pt_bbq9_ pt_bbq_9_
    label def pt_bbq_9_ 1 "Completely Disagree", modify
    label def pt_bbq_9_ 3 "Neutral", modify
    label def pt_bbq_9_ 5 "Completely Agree", modify
    label values pt_bbq10_ pt_bbq_10_
    label def pt_bbq_10_ 1 "Completely Disagree", modify
    label def pt_bbq_10_ 3 "Neutral", modify
    label values pt_bbq11_ pt_bbq_11_
    label def pt_bbq_11_ 1 "Completely Disagree", modify
    label def pt_bbq_11_ 3 "Neutral", modify
    label values pt_bbq12_ pt_bbq_12_
    label def pt_bbq_12_ 1 "Completely Disagree", modify
    label def pt_bbq_12_ 3 "Neutral", modify
    label def pt_bbq_12_ 5 "Completely Agree", modify
    label values pt_bbq13_ pt_bbq_13_
    label def pt_bbq_13_ 1 "Completely Disagree", modify
    label def pt_bbq_13_ 3 "Neutral", modify
    label def pt_bbq_13_ 5 "Completely Agree", modify
    label values pt_bbq14_ pt_bbq_14_
    label def pt_bbq_14_ 1 "Completely Disagree", modify
    label def pt_bbq_14_ 3 "Neutral", modify
    label def pt_bbq_14_ 5 "Completely Agree", modify

  • #2
    you say it does not work but don't tell us what does happen (read the FAQ re: "does not work"); at any rate, I don't think you have the rate issue; you are trying to -generate- variables that already exist; instead, try this:
    Code:
    . foreach var of varlist pt_* {
       gen `var'2=6 - `var'
       }
    of course, maybe you don't want a set of new variables; in that case, use -replace- instead of -gen-, and, of course, don't include the "2" that I added in the -gen- line

    Comment


    • #3
      Thank you, Rich. This worked perfectly. I will review the FAQ. Thanks for the suggestion.

      Comment


      • #4
        Rich has revised Jack's approach to the problem of reversing the values in variables. I believe that Jack wants to fix the value labels, too. I will show a convenient way to approach this problem, using elabel (from SSC).

        From a quick glance, it appears as if every variable in Jacks example has its own value label attached. These value labels, however, all define the same integer-to-text mappings (excluding the two cases where value 5 is not observed in the data and for some reason that leads to a missing value label definition). It might be a good idea to define just one set of value labels and attach that to all variables. We can define a new set of value labels, say agreelbl, that reverses the integer-to-text mappings in (the first) one of the existing sets of value labels.

        Code:
        elabel recode pt_bbq_1_ (1/5 = 5/1) , define(agreelbl)
        Now that we have created the appropriate value label, we can reverse the values in the variables, accordingly. We could use Rich's code, to do this. However as Rich points out, we could also use the recode command. recode accepts a varlist, so we do not have to set up the loop. recode also requires transformation rules. Since we have used elabel before, we do not need to type these rules (and possibly introduce some errors) because elabel stores them.

        Code:
        . return list
        
        macros:
                      r(rules) : "(1=5) (2=4) (3=3) (4=2) (5=1)"
        Instead of overwriting the existing variables, we will create new ones with names prefixed with r_

        Code:
        recode pt_bbq* `r(rules)' , prefix(r_)
        Last, we attach our new value label to the new variables

        Code:
        label values r_* agreelbl
        and verify the result.

        Code:
        . tabulate pt_bbq1_ r_pt_bbq1_ , missing
        
                            |                        RECODE of pt_bbq1_
                   pt_bbq1_ | Completel          2    Neutral          4  Completel          . |     Total
        --------------------+------------------------------------------------------------------+----------
        Completely Disagree |         0          0          0          0         35          0 |        35
                          2 |         0          0          0         16          0          0 |        16
                    Neutral |         0          0         12          0          0          0 |        12
                          4 |         0         10          0          0          0          0 |        10
           Completely Agree |         1          0          0          0          0          0 |         1
                          . |         0          0          0          0          0         26 |        26
        --------------------+------------------------------------------------------------------+----------
                      Total |         1         10         12         16         35         26 |       100
        Best
        Daniel

        Comment

        Working...
        X