Announcement

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

  • Problem with value labels

    I am facing a problem regarding labeling of a particular variable in my dataset. I have labelled the variable on industry code - different labels for different groups of industries (consolidated into a single code). However, after I have defined the value labels, the dataset shows wrong labels for the industry codes. I am unable to understand where I am going wrong.

  • #2
    Your question really isn't clear without more detail, or at a minimum it is too difficult to guess at a good answer from what you have shared. Please help us help you. Show example data. Show your code. Show us what Stata told you. Tell us what precisely is wrong. The Statalist FAQ provides advice on effectively posing your questions, posting data, and sharing Stata output.

    You have told us what you think you have done, but without seeing what you have actually done, it is difficult to tell you what you have done that was wrong.

    Comment


    • #3
      I am attaching the codes I have used regarding value labels for a particular variable (industry codes) in the dataset. NIC_Pr is the industry code given.


      Code:
      gen sect=NIC_Pr
      destring sect, replace
      **Food manufacturing
      replace sect=10 if sect==10|sect==11|sect==12
      *textile and leather manuf
      replace sect=13 if sect==13|sect==14|sect==15
      ***Wood and paper manuf
      replace sect=16 if sect==16|sect==17|sect==18
      **Chemicals manuf
      replace sect=19 if sect==19|sect==20|sect==21|sect==22
      *** metals and non-metallic minerals
      replace sect=23 if sect==23|sect==24|sect==25
      *** electrical and electronic equipment
      replace sect=26 if sect==26|sect==27
      ***Equipmt manuf
      replace sect=28 if sect==28|sect==29|sect==30
      Others
      replace sect=31 if sect==31|sect==32|sect==33
      
      label define seclabel " 10 "Food manuf" 13 "Textile and leather manuf" 16 "Wood Manuf" 19 "Chemicals Manuf" 23 "Metals and nonmetallic minerals" 26 "Electrical and electronic" 28 "Equipt Manuf" 31 "Other manuf"
      
      label values sect seclabel
      However, when I look at the dataset, the label values do not correctly correspond to the sect codes

      Code:
      label list seclabel
      namesec:
                 
                10 Food manuf
                13 Textile and leather manuf
                16 Wood Manuf
                18 Media Printing and Records
                19 Chemicals Manuf
                23 Non-metal Manuf.
                24 Basic Metal Manuf
                25 Machinery manuf
                26 Electronics Manuf
                28 Equipt Manuf
                31 Furniture Manuf
                32 Manuf Others
                33 Repair & install machinery
      The labels I have defined under seclabel, and the list which is being shown are not tallying. It would be really helpful if I could get any suggestion on this issue.

      Comment


      • #4
        Originally posted by Sayoree Gooptu View Post
        Code:
        label list seclabel
        namesec:
        
        10 Food manuf
        [...]
        Is this really copied and pasted? It is hard to believe that Stata would list the contents of value label namesec when you request to list the contents of value lable seclabel.

        Comment


        • #5
          Sorry for this. I actually copied and pasted. I changed the name of label from namesec to seclabel just for convenience. The output is
          Code:
          label list seclabel
          seclabel:
                     1 Angriculture
                     2 Forestry and Fishing
                     5 Mining
                    10 Food manuf
                    13 Textile and leather manuf
                    16 Wood Manuf
                    18 Media Printing and Records
                    19 Chemicals Manuf
                    23 Non-metal Manuf.
                    24 Basic Metal Manuf
                    25 Machinery manuf
                    26 Electronics Manuf
                    28 Equipt Manuf
                    31 Furniture Manuf
                    32 Manuf Others
                    33 Repair & install machinery
          Very sorry for the inconvenience.

          Comment


          • #6
            Still the commands pasted in #3 can't run: Your -label define- command should issue an error because you start the definition of value labels with a double quote and thus the -label list- command that follows can't show you the labels you tried to define.

            By the way: To me your way of recoding seems inefficient and not very userfriendly. Here is an alternative
            Code:
            tempvar sect
            gen `sect' = NIC_Pr
            destring `sect', replace
            
            recode `sect' (10/12=10 "Food Manuf")                      ///
                          (13/15=13 "Textile and leather manuf")       ///
                          (16/18=16 "Wood manuf")                      ///
                          (19/22=19 "Chemicals Manuf")                 ///
                          (23/25=23 "Metals and nonmetallic minerals") ///
                          (26/27=26 "Electrical and electronic")       ///
                          (28/30=28 "Equipt Manuf")                    ///
                          (31/33=31 "Other"), gen(sect)
            
            label list sect

            Comment


            • #7
              Thank you for the suggestion. But, I am getting following errors from this code

              Code:
              gen `sect' = NIC_Pr
              (277,414 missing values generated)
              
              . 
              . destring `sect', replace
              __000000: all characters numeric; replaced as byte
              (277414 missing values generated)
              
              . 
              . 
              . 
              . recode `sect' (10/12=10 "Food Manuf")                      ///
              Rules defining value labels not allowed when overwriting a variable
              r(198);
              
              . 
              .               (13/15=13 "Textile and leather manuf")       ///
              ( is not a valid command name
              r(199);
              
              . 
              .               (16/18=16 "Wood manuf")                      ///
              ( is not a valid command name
              r(199);
              
              . 
              .               (19/22=19 "Chemicals Manuf")                 ///
              ( is not a valid command name
              r(199);
              
              . 
              .               (23/25=23 "Metals and nonmetallic minerals") ///
              ( is not a valid command name
              r(199);
              
              . 
              .               (26/27=26 "Electrical and electronic")       ///
              ( is not a valid command name
              r(199);
              
              . 
              .               (28/30=28 "Equipt Manuf")                    ///
              ( is not a valid command name
              r(199);
              
              . 
              .               (31/33=31 "Other"), gen(sect)
              ( is not a valid command name
              r(199);
              Could you please help me with this?

              Also, in #3, the double quotes are for the line of code with the description of industry, not for the rename code. So the code is running properly. However, the labels are wrong.

              Comment


              • #8
                My example follows the commands you showed us in #1. The first error message shows that obviously you did not run the commands from the do-file editor but tried to copy them into you command-window. This will not work because the command window does not allow to break command lines using "///".

                Comment


                • #9
                  Okay. Thanks a lot for the suggestion.
                  I would just like to ask one more thing. If I want to club the code 18 of 'sect' with the "Other" category of sect -31/33, how do I incorporate that?

                  Comment


                  • #10
                    I believe that when you copied the code from post #7 and pasted it into your Do-file Editor window to run, an extra blank line was added between each line of the orignal. The initial commands worked, because they were each one-line commmands. But the recode failed, because it consists of a number of continued lines (indicated by the /// at the end of each line), but the blank lines interrupted the continuation.

                    Here is what you should have seen, using made-up data.
                    Code:
                    . tempvar sect
                    
                    . gen `sect' = NIC_Pr
                    (9 missing values generated)
                    
                    . destring `sect', replace
                    __000001: all characters numeric; replaced as byte
                    (9 missing values generated)
                    
                    .
                    . recode `sect' (10/12=10 "Food Manuf")                      ///
                    >               (13/15=13 "Textile and leather manuf")       ///
                    >               (16/18=16 "Wood manuf")                      ///
                    >               (19/22=19 "Chemicals Manuf")                 ///
                    >               (23/25=23 "Metals and nonmetallic minerals") ///
                    >               (26/27=26 "Electrical and electronic")       ///
                    >               (28/30=28 "Equipt Manuf")                    ///
                    >               (31/33=31 "Other"), gen(sect)
                    (16 differences between __000001 and sect)
                    
                    .
                    . label list sect
                    sect:
                              10 Food Manuf
                              13 Textile and leather manuf
                              16 Wood manuf
                              19 Chemicals Manuf
                              23 Metals and nonmetallic minerals
                              26 Electrical and electronic
                              28 Equipt Manuf
                              31 Other
                    Added in edit: I started with the explanation given in post #8, but when I do that it does not produce the extra blank lines shown in Post 7 as ".". I'm willing to believe though that there's some wrong way of copying from post #6 and pasting into the command window that would explain those extra blank lines.
                    Last edited by William Lisowski; 29 May 2021, 11:43.

                    Comment


                    • #11
                      Originally posted by Sayoree Gooptu View Post
                      Also, in #3, the double quotes are for the line of code with the description of industry, not for the rename code. So the code is running properly.
                      There is no rename code; also, the code you show in #3 does not run properly:

                      Code:
                      . label define seclabel " 10 "Food manuf" 13 "Textile and leather manuf" 16 "Wood Manuf" 19 "Chemicals Manuf" 23 "Metals and nonmetallic minerals" 26 "Electrical and electronic" 28 "Equipt Manuf"
                      > 31 "Other manuf"
                      invalid syntax
                      r(198);
                      for the reasons that Dirk has pointed out.

                      You need to show us what you have typed, exactly. Also show what Stata did in response, exactly. Ideally, post a reproducible example using dataex.

                      Comment


                      • #12
                        Answer to #9:

                        Read the help for the -recode- command, especially how to use "rule" that allows combinations of single values, lists of values and sequences of values to be recoded.

                        Note, however, that the sequence of the recode statements is important: A value that has been already recoded will not be recoded again in the same -recode- command. Therefore you can recode all values that should be recoded into the "other" category (in your case 31) first (not last as in my example in #6), followed by the remaining values to recode. Because 18 will be recoded into 31, it will not be recoded again into 16 (Wood manuf), although it is still specified in the rule to be recoded:
                        Code:
                        tempvar sect
                        gen `sect' = NIC_Pr
                        destring `sect', replace
                        
                        recode `sect' (18 31/33=31 "Other")                           ///  <-- 18 specified the first time will be recoded
                                      (   10/12=10 "Food Manuf")                      ///
                                      (   13/15=13 "Textile and leather manuf")       ///
                                      (   16/18=16 "Wood manuf")                      ///  <-- 18 specified here will not be recoded again
                                      (   19/22=19 "Chemicals Manuf")                 ///
                                      (   23/25=23 "Metals and nonmetallic minerals") ///
                                      (   26/27=26 "Electrical and electronic")       ///
                                      (   28/30=28 "Equipt Manuf"), gen(sect)
                        Last edited by Dirk Enzmann; 29 May 2021, 11:59.

                        Comment


                        • #13
                          Thank you for all the suggestions. They really worked out this time.

                          Comment

                          Working...
                          X