Announcement

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

  • How to recode categorial variables from 1,2,3 to 0,1,2?

    Hi there,

    I have a categorial variable that equals 1 if 'No', 2 if 'Yes, sometimes' and 3 if 'Yes, often'. Value 0 is not in use.

    Now I want to recode this so that it equals 0 if 'No', 1 if 'Yes, sometimes' and 2 if 'Yes, often'.

    Is there a certain command that obtains this goal?

    Thanks,
    Ilse

  • #2
    Ilse:
    see -help recode-.
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3
      The recoding is simple:
      Code:
      replace var = var -1
      You will need to adept the the value labels, but that is just standard value label stuff. If you need help with that, please tell us.


      ---------------------------------
      Maarten L. Buis
      University of Konstanz
      Department of history and sociology
      box 40
      78457 Konstanz
      Germany
      http://www.maartenbuis.nl
      ---------------------------------

      Comment


      • #4
        Thank you Carlo and Maarten, this is helpful!

        Maarten, I indeed might need some help with the value labels. This is what my data looked like before replace var = var-1:

        Code:
        halluc_freq |      Freq.     Percent        Cum.
        ------------+-----------------------------------
              never |     72,785       99.79       99.79
          sometimes |        137        0.19       99.98
          regularly |         16        0.02      100.00
        ------------+-----------------------------------
              Total |     72,938      100.00
        And this is after:

        Code:
        halluc_freq |      Freq.     Percent        Cum.
        ------------+-----------------------------------
                  0 |     72,785       99.79       99.79
              never |        137        0.19       99.98
          sometimes |         16        0.02      100.00
        ------------+-----------------------------------
              Total |     72,938      100.00

        Comment


        • #5
          supplement to my previous post:

          Code:
          label define halluc_freq 0 "never" 1 "sometimes" 2 "regularly"
          did not work.

          I would be grateful for other tips!

          Comment


          • #6
            See elabel (SSC, or https://github.com/kleindaniel81/elabel) for a canned solution

            Code:
            elabel adjust : replace var = var - 1
            Done. Value labels are adjusted automatically.

            Comment


            • #7
              #5 You need the modify option too.

              Comment


              • #8
                If you want to stay away from community-contributed commands, you first need to know the name of the value label attached to the variable halluc_freq. One way to find out is

                Code:
                describe halluc_freq
                Before modifying or replacing the value label, I suggest making sure that no other variables are using the same value label. Perhaps something like:

                Code:
                ds , has(vallabel lblname)
                But that still would not catch value labels in a different label language. elabel would run all these checks for you and tell you if there is something wrong. Anyway, sticking with core Stata, it might be better to create a new label from scratch:

                Code:
                label define halluc_freq_0_to_2 0 "never" 1 "sometimes" 2 "regularly"
                then attach it to the (recoded) variable

                Code:
                label values halluc_freq halluc_freq_0_to_2

                Comment


                • #9
                  Daniel: Thank you for the elabel command, this was very helpful for variables coded as mentioned above. Your explanation in #8 was especially helpful for a categorial variable that equaled values 0; 101; 102; 201 and 202 (and now 0; 1; 2; 3; 4).

                  Comment


                  • #10
                    Originally posted by Ilse van Vliet View Post
                    Your explanation in #8 was especially helpful for a categorial variable that equaled values 0; 101; 102; 201 and 202 (and now 0; 1; 2; 3; 4).
                    Here is one way to this with elabel:

                    Code:
                    elabel adjust : recode varlist (101 = 1) (102 = 2) (201 = 3) (202 = 4)

                    Comment

                    Working...
                    X