Announcement

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

  • How to assign a the numeric value of a variable to another variable

    Dear Stata user, i need your help!

    I'm quite a new user of Stata, and I'm struggling with a problem that certainly for you will be super easy, could you help me with that?

    I'm working with a dataset of 67 cases, those are 67 different occupations. Very basically, I need to "attach" the value of a numeric variable to another numeric variable. In other words, I need to substitute the numerical value of each occupation (that is meaningless, because it depends on the classification system of occupations) with the numerical variable of another variable ("rank", that indicates for each occupation its positions in the ranking of the 'best payed occupations'). So, let's say, in my original data "lawyers" is classified with value = 34, now I want to attach to it the value 1 (because it is the most payed), and this for every occupations.

    Could you suggest me how to improve this?
    Thank you very much in advance.
    Best, G.P.

  • #2
    Giorgio:
    welcome to the list.
    Usually, posting an example concerning what you're intending to do increases your chances of getting helpful replies (please, see -search dataex-).
    That said, perhaps -help recode- can give you some clues on how to perform what you're after.
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3
      Dear Carlo,
      thank you very much for your suggestion, and I sorry if somehow I was wrong in my first approach to this forum.

      Anyway, I tried looking at the different options of the command recode, but I did not succed.
      If I type: recode prof = de_rank (prof if the variable of occupations, de_rank is the ranking value for each occupation), Stata says "transformation rule without "condition" part". How can I ask it to substitute, for each case, the value of prof with the value of de_rank?

      I hope you can help me, and sorry if I'm missing something.

      Thank you very much, G.P.

      Comment


      • #4
        Hello Giogio,

        Carlo gave you the excellent suggestion so as to get the appropriate commands.

        That said, just hazarding a guess, if you wish to recode and create a new variable at once, a solution - maybe not so elegant - could be something like:

        Code:
        recode occupation (34  = 1) (33 = 2) (1/32 = 3), gen(ranked_oc)
        By doing so, you'd create a new variable, recoded as 1, 2 and 3, for example.


        Best,

        Marcos
        Best regards,

        Marcos

        Comment


        • #5
          Giorgio: Carlo is right. A data example would help here and you have yet to give one,

          I don't yet understand either #1 or #2.

          I am confident that when you explain your problem so that we can understand there will indeed be a simple solution.

          Please do read http://www.statalist.org/forums/help#stata and the rest of the FAQ Advice.

          Comment


          • #6
            Dear all,
            thank you for your feedbacks and sorry if I was not clear enough.

            I don't know how to provide you a data example, because actually mine is not a problem with the code, but a problem on how to solve a proceedure.

            I will try to explain me at my best, and to do that I should attach a screenshot (I read that this is forbidden by the rules of the forum, but how can I attach you some image without screen-shoting?). I beg your pardon if this is not allowed, but I don't really know what kind of support could be more useful to make it clear the problem.

            What I would like to do is attaching the value of variable r_d6 (in my screenshot example), that indicates the position into the ranking of that occupation, to the correspondent occupation (var. prof): so I will end with a variable labeled as the occupations that I have, but with a numerical value that is the position in the rank of that occupation (while normally the numerical value of the variable prof is meaningless, because it is only just a numerical value assigned on the base of the system of classification; in the screenshot, for instance, "Sawyers and lumber inspectors" are coded with 65, that is absolutely aribtrary).

            I hope this is clearer, and if not, please excuse me again.

            Click image for larger version

Name:	screen.png
Views:	1
Size:	93.7 KB
ID:	1360523



            Attached Files

            Comment


            • #7
              To say that screenshots are "forbidden" is stronger wording than we ever use. But screenshots really don't help. I can't read your screenshots easily and they are certainly not useful for copy and paste. With the exception of a few StataCorp employees, we are all volunteers here and a question that is not easy to understand is likely to fail.

              You really should try what we say!

              Carlo said in #2

              Code:
              ssc install dataex
              Running the code installs the program after which

              Code:
              help dataex
              tells you how to use. The link I gave in #3 explains what you are finding difficult to understand.

              You said

              I don't know how to provide you a data example, because actually mine is not a problem with the code, but a problem on how to solve a proceedure.
              This distinction has no meaning to me. We want to give you code that you can use with your variables. Anything otherwise would have to make use of other datasets, but until anyone understands the conceptual difficulty, we can't do that either.

              Perhaps someone else can follow your last paragraph, and if so they will reply.

              Comment


              • #8
                Dear Nick, I try with dataex:


                Code:
                * Example generated by -dataex-. To install: ssc install dataex
                clear
                input float(prof r_d6)
                65  1
                66  2
                79  3
                67  4
                78  5
                33  6
                70  7
                77  8
                30  9
                73 10
                48 11
                38 12
                64 13
                 . 14
                74 15
                58 16
                34 17
                23 18
                69 19
                22 20
                45 21
                10 22
                54 23
                82 24
                75 25
                55 26
                25 27
                26 28
                59 29
                52 30
                35 31
                57 32
                 1 33
                46 34
                68 35
                39 36
                53 37
                18 38
                41 39
                32 40
                72 41
                37 42
                16 43
                50 44
                 5 45
                11 46
                60 47
                36 48
                28 49
                47 50
                27 51
                17 52
                31 53
                 7 54
                 8 55
                13 56
                 3 57
                 9 58
                12 59
                14 60
                19 61
                 2 62
                42 63
                51 64
                24 65
                 6 66
                61 67
                end
                label values prof prof
                label def prof 1 "Officials, government and non-profit organisations", modify
                label def prof 2 "Managers", modify
                label def prof 3 "Commercial Managers", modify
                label def prof 5 "Jurists", modify
                label def prof 6 "Health professionnels", modify
                label def prof 7 "Professors and instructors", modify
                label def prof 8 "Natural scientiste", modify
                label def prof 9 "Statistical and social scientists", modify
                label def prof 10 "Architects", modify
                label def prof 11 "Accountants", modify
                label def prof 12 "Journalists, authors, and related writers", modify
                label def prof 13 "Engineers", modify
                label def prof 14 "Systems analysts and programmers", modify
                label def prof 16 "Personnel and labor relations workers", modify
                label def prof 17 "Elementary and secondary school teachers", modify
                label def prof 18 "Librarians", modify
                label def prof 19 "Creative artists", modify
                label def prof 22 "Professional, technical, and related workers, n.e.c.", modify
                label def prof 23 "Social and welfare workers", modify
                label def prof 24 "Workers in religion", modify
                label def prof 25 "Nonmedical techniciens", modify
                label def prof 26 "Health semiprofessionals", modify
                label def prof 27 "Health service workers", modify
                label def prof 28 "Nursery school teachers and aides", modify
                label def prof 30 "Real estate agents", modify
                label def prof 31 "Other agents", modify
                label def prof 32 "Insurance agents", modify
                label def prof 33 "Cashiers", modify
                label def prof 34 "Sales workers and shop assistants", modify
                label def prof 35 "Telephone operators", modify
                label def prof 36 "Bookkeepers and related workers", modify
                label def prof 37 "Office and clerical workers", modify
                label def prof 38 "Postal and mail distribution clercs", modify
                label def prof 39 "Craftsmen and kindred workers, n.e.c.", modify
                label def prof 41 "Electronics service and repair workers", modify
                label def prof 42 "Printers and related workers", modify
                label def prof 45 "Tailors and related workers", modify
                label def prof 46 "Vehicle mechanics", modify
                label def prof 47 "Blacksmiths and machinists", modify
                label def prof 48 "Jewelers, opticians*, and precious metal workers", modify
                label def prof 50 "Plumbers and pipe-fitters", modify
                label def prof 51 "Cabinetmakers", modify
                label def prof 52 "Bakers", modify
                label def prof 53 "Welders and related metal workers", modify
                label def prof 54 "Painters", modify
                label def prof 55 "Butchers", modify
                label def prof 57 "Bricklayers, carpenters, and related construction workers", modify
                label def prof 58 "Heavy machine operators", modify
                label def prof 59 "Truck drivers", modify
                label def prof 60 "Chemical processors", modify
                label def prof 61 "Miners and related workers", modify
                label def prof 64 "Textile workers", modify
                label def prof 65 "Sawyers and lumber inspectors", modify
                label def prof 66 "Metal processors", modify
                label def prof 67 "Operatives and kindred workers, n.e.c.", modify
                label def prof 68 "Forestry workers", modify
                label def prof 69 "Protective service workers", modify
                label def prof 70 "Transport conductors", modify
                label def prof 72 "Food service workers", modify
                label def prof 73 "Mass transportation operators", modify
                label def prof 74 "Physical Oriented service work", modify
                label def prof 75 "Interpersonal oriented service work", modify
                label def prof 77 "Launderers and dry-cleaners", modify
                label def prof 78 "Housekeeping workers", modify
                label def prof 79 "Janitors/caretakers and cleaners", modify
                label def prof 82 "Farmers and farm managers", modify
                Basically, I want to assign the value of the second column (var: r_d6) to the correspondant case (var: occupation), so to have 1 instead of 65, 2 instead of 66, and so on and so forth. I could have done this by following the suggestion of Marcos (#4), but in this case I would have obtained just a new variable in which the case have a new numerical value, but not anymore the labels (and in this case I would have missed all the substantive part, namely the name of occupations).

                What do you think? Thanks a lot, G.

                Comment


                • #9
                  Thanks for the data example, which certainly helps.

                  I find your wording a little backwards, which is I think why it was too hard for me to follow in abstraction. You want to apply the value labels of one variable to the values of another. (If that is wrong, then I don't understand still.) Try this

                  Code:
                   
                  labmask r_d6, values(prof) decode
                  But you must install labmask first. Type

                  Code:
                  search labmask
                  and then download from the Stata Journal site. To do that click on gr0034 when a new window opens.

                  Comment


                  • #10
                    Hello Giorgio,

                    With regards to your remarks below:

                    I could have done this by following the suggestion of Marcos (#4), but in this case I would have obtained just a new variable in which the case have a new numerical value, but not anymore the labels (and in this case I would have missed all the substantive part, namely the name of occupations).
                    Now, I'm far away from "my Stata", therefore I cannot provide an example, but I believe we may -recode - followed by - generate -and, yet, we wouldn't lose the "substantive part" of the labels.

                    You may wish to test these commands:

                    Code:
                     recode occupation (34  = 1 "Lawyers") (33 = 2 "Managers" ) (1/32 = 3 "Physicians"), gen(ranked_oc)
                    Hopefully that helps.

                    Best,

                    Marcos
                    Last edited by Marcos Almeida; 17 Oct 2016, 09:17. Reason: Edited: sorry, I wrote my comments before having read Nick's elegant in-one-line commands under SSC labmask. Mine, above, would take several lines...
                    Best regards,

                    Marcos

                    Comment


                    • #11
                      Dear Nick,
                      thanks a lot. Probably you are right, my verbal reasoning was quite confuse and this made everything more complicated.

                      This command is exactly what I was looking for.
                      Thanks again to you and to all other people in the discussion, I reserve the possibility to benefit again from your kindness in the future

                      Best, G.P.

                      Comment


                      • #12
                        I think that moment is already arrived :D!

                        The step forward it should be to save the new variable obtained and export it in a new dataset, in order use it for some calculations.
                        If I try to export the variable r_d6 (in my case), Stata will show me in the new dataset only the numerical value of the variable, missing the labels associated to each number.
                        Is there some option of the command that Nick showed me (maybe creating a new variable saving the value of r_d6 and the labels of prof), or should I go (more slowly) as suggested by Marcos, by recoding one-by-one?

                        Thanks again, and sorry id I bother you.
                        Best, G.

                        Comment


                        • #13
                          Someone can suggest me something? I'm coming from a night without sleeping (more or less) to consider this problem

                          Comment


                          • #14
                            We have the same problem as before, as you don't give any exact code that you are trying.

                            labmask attaches value labels to an existing variable; there is no new variable and no need for a new variable. But once that variable has been labelled, you can use any or all of

                            Code:
                            save
                            
                            export excel
                            (others are available) to put the dataset in a file as desired. I don't understand what your difficulty is as you don't say exactly what you did.

                            It's seemingly a hard lesson for new users who can't believe that more experienced users have no extra-sensory skills to decode what you did from some vague wording, only more experience of Stata.

                            Every word in

                            Help us to help you by producing self-contained questions with reproducible examples that explain your data, your code, and your problem. This helps yet others too, as they will find it easier to learn from your questions and the answers to them. Say exactly what you typed and exactly what Stata typed (or did) in response. N.B. exactly!

                            means what it says.

                            Incidentally, Marcos Almeida referring to labmask as being from SSC. There is a version on SSC, but I deliberately mentioned the Stata Journal version, because of the associated discussion in

                            SJ-8-2 gr0034 . . . . . . . . . . Speaking Stata: Between tables and graphs
                            (help labmask, seqvar if installed) . . . . . . . . . . . . N. J. Cox
                            Q2/08 SJ 8(2):269--289
                            outlines techniques for producing table-like graphs

                            Comment

                            Working...
                            X