Announcement

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

  • Transferring value labels from one variable to another

    Dear Stata Forum,
    I have a variable for "race" which has 7 different ethnicity coded as 1 - 7, and some negative numbers for missing etc.. I need to transfer the labels (e.g 1 = white) to another variable which is essentially the same (same coding). However I haven't had much success. I tried using labmask but couldn't figure out why the syntax wasn't working. Apologies if this question has already been asked but I couldn't find it in the forum.

    To give a bit more context, I have panel data in which ethnicity was obtained in one wave but not subsequent. So one person might have value 1 (white) in year 1998 but -8 (inapplicable) in 1999. I have created a variable which is the maximum value: "by pid: egen race1 = max(race) " and I need this new variable to have the same value labels.

    Many thanks,
    Euan

  • #2
    use the command describe to find out what the name of the label for race is, then sinply apply it to the new variable:
    Code:
    desc race
    label values othervar whateverlabel

    Comment


    • #3
      Dear Euan,

      in Stata, value labels are treated as separate objects of the data structure. This means you can apply a given set of value labels to several variables without a hassle. You can see which value label is applied to a variable using -describe-:
      Code:
      sysuse auto , clear
      describe foreign
      The result will tell you that the value label set "origin" is applied to the variable "foreign".
      If you want to also apply this set of value labels to any other variable using the -label values- command
      Code:
      label values price origin
      describe foreign price
      Now you can see that "origin" is also applied to "price" (which makes, in the auto example data set, absolutely no sense).

      Back to your question:
      Code:
      label values race1 <name of race's value label set>
      will do the trick.

      Regards
      Bela

      PS: With a small Stata extended macro function, you also could abstract this command with
      Code:
      label values race1 `: value label race'

      Comment


      • #4
        That's great, thanks a lot!

        Comment


        • #5
          One caveat: by applying the same value label to both variables, you are permanently committing to retaining the same coding system for both (or face chaos if you don't.) In my experience, variables encoding race and ethnicity frequently get re-grouped and re-coded in the course of multiple analyses. So it might be safer to make a new label that is, initially, identical to the first using the -label copy- command and apply the new label to the second variable. Then if you need to make changes to the coding of either variable without doing so to the other, you can do so with no complications.

          Comment


          • #6
            Hi,

            I'm using a particular data set from the world values survey and they have set labels for some of their variables as such : -5 , Missing;Unknown , -1 Don't know ....etc. as responses to questions asked in the survey. When i attempted to do the same, stata changed my value labels to 1 , 2, 3 ... etc . and for a variable such as age, it added value labels to certain ages when I didn't originally insert any!
            Help please?

            Thanks

            Comment


            • #7
              Stata changed ...
              If you mean that you just sat there, and that Stata unilaterally and without prompting applied certain commands --- we still need to know what they were.

              More plausibly, you applied certain commands -- and we need to know what they are.

              Please back up and read once more https://www.statalist.org/forums/help#stata for concrete suggestions on giving us data examples and code that will make your question precise.

              Comment


              • #8
                You need to (a) review the FAQs on asking questions, (b) show what you have types in Stata - exactly, (c) show what Stata gave you back - exactly [using code delimiters] and (d) explain how what you got differs from what you expected and/or want to have.

                Best
                Daniel

                Comment


                • #9
                  This is what i typed in stata :

                  label define x003v2 -5 "Missing;Unknown" -4 "Not asked in survey" -3 "Not applicable" -2 "No answer" -1 "Don't know"

                  Alternatively, i accessed the screen to make these changes via the data editor (edit mode). the picture below if for the variable "Age". This is how i originally defined it.


                  Click image for larger version

Name:	age original.JPG
Views:	1
Size:	184.4 KB
ID:	1402034



                  After saving, logging off stata, and resuming my work today, this is what I could see for the same variable. I did not make any other changes or run any commands in between.


                  Click image for larger version

Name:	age data.JPG
Views:	1
Size:	146.8 KB
ID:	1402035


                  and:
                  Click image for larger version

Name:	age data v2.JPG
Views:	1
Size:	57.0 KB
ID:	1402036

                  Comment


                  • #10
                    Obviously you did not read the FAQ (that are really intended to help you) very carefully, as section 12.5 explicitly states

                    In particular, please do not post screenshots.
                    Anyway, I have never worked with the value label manager, so I cannot much comment on that. What i can comment on is that

                    Code:
                    label define x003v2 ...
                    does not attach value labels to any variables. You need an extra step to do this

                    Code:
                    label values varlist x003v2
                    This is documented in

                    Code:
                    help label
                    Best
                    Daniel

                    Comment


                    • #11
                      Originally posted by Daniel Bela View Post
                      Dear Euan,

                      in Stata, value labels are treated as separate objects of the data structure. This means you can apply a given set of value labels to several variables without a hassle. You can see which value label is applied to a variable using -describe-:
                      Code:
                      sysuse auto , clear
                      describe foreign
                      The result will tell you that the value label set "origin" is applied to the variable "foreign".
                      If you want to also apply this set of value labels to any other variable using the -label values- command
                      Code:
                      label values price origin
                      describe foreign price
                      Now you can see that "origin" is also applied to "price" (which makes, in the auto example data set, absolutely no sense).

                      Back to your question:
                      Code:
                      label values race1 <name of race's value label set>
                      will do the trick.

                      Regards
                      Bela

                      PS: With a small Stata extended macro function, you also could abstract this command with
                      Code:
                      label values race1 `: value label race'
                      Sorry for the very late commentary on this, but the abstraction of this command (i.e., `: value label race') is very useful. Where does it appear in the user documentation? I would love to be more versed with these types of macros.

                      Comment


                      • #12
                        For the specific application

                        Code:
                        label values race1 `: value label race'
                        see also elabel (SSC) for convenience

                        Code:
                        elabel values race1 (race)

                        More generally, macro's are documented in various places; you seek [U] 18.3.8 Macro expressions

                        Comment


                        • #13
                          It does not get much attention in the documentation, not as much as I think it deserves. If you have an updated version 17, in the PDF documentation you will find it mentioned briefly on page 300 of the [P] volume under the heading "Macro expansion operators and function." More broadly, if you read the whole chapters for macro and macro lists in [P] you will be exposed to everything you can do with macros.

                          While the power of this particular abstraction is great, I do urge some caution in using it. These expressions tend to be fairly long, and if you use several of them in the same command, the command begins to extend over many lines, and becomes very difficult for humans to read and understand. Simply defining separate macros for each of those and referencing those can lead to cleaner, more transparent code in some cases.
                          Last edited by Clyde Schechter; 31 Jul 2021, 13:40.

                          Comment

                          Working...
                          X