Announcement

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

  • Categorical Variable error

    Hi,

    I'm new to STATA and getting the hang of it. I have a list of 10k directors who hold board positions in one or multiple countries. I'm trying to create a categorical variable where in my command is "recode uboards_fe (1 = 0) (2/8 = 1), generate(Prominent Director)" but every time I enter this command I get an error: the number of new and transformed varnames should be the same. Can someone please explain what I am doing wrong?

    Regards,
    Raghavendra

  • #2
    Raghavendra:
    welcome to this forum.
    What you experienced is an example of trivial but nasty issues that make your banging your head against the wall (it happened to me many times in the past):
    Code:
    recode uboards_fe (1 = 0) (2/8 = 1), generate(Prominent_Director)
    
    
    . list
    
         +---------------------+
         | uboard~e   Promin~r |
         |---------------------|
      1. |        0          0 |
      2. |        1          0 |
      3. |        2          1 |
      4. |        3          1 |
      5. |        4          1 |
         |---------------------|
      6. |        5          1 |
      7. |        6          1 |
      8. |        7          1 |
      9. |        8          1 |
         +---------------------+
    
    .


    A trivial but mean underscore was missing in your previous code between Prominent and Director. The missing underscore makes Stata think that you have two new variables (which is not correct) and complains about that.
    Kind regards,
    Carlo
    (StataNow 18.5)

    Comment


    • #3
      Hi Carlo,

      Thanks a lot for the help! It worked.
      Now I understood the mistake I was doing

      Regards,
      Raghavendra

      Comment


      • #4
        Hi,

        In the same data, I've 3 separate variables for the education of the directors(namely, Bachelors, Masters and PhD). In case of only bachelor's I want my data to show 0, in case of either a master's or a PhD or both, I want it to show as 1. How can I merge the 3 variables and combine them into one? What command should I use?

        Regards,
        Raghavendra

        Comment


        • #5
          Raghavendra:
          Code:
          . set obs 3
          number of observations (_N) was 3, now 3
          
          . g bachelor=1
          
          . g master=1 in 1/2
          
          . replace master=0 if master==.
          
          . g PhD=1 in 1
          
          . replace PhD=0 if PhD==.
          
          . egen wanted=group( bachelor master PhD)
          
          . recode wanted (3 2 = 1) (1=0), gen(Overall_education)
          (3 differences between wanted and Overall_education)
          
          . label define Overall_education 0 "Bachelor_only" 1 "Bachelor_Master_Backelor_Master_PhD"
          
          . label val Overall_education Overall_education
          
          . list
          
               +-----------------------------------------------------------------------------+
               | id   bachelor   master   PhD   wanted                     Overall_education |
               |-----------------------------------------------------------------------------|
            1. |  1          1        1     1        3   Bachelor_Master_Backelor_Master_PhD |
            2. |  2          1        1     0        2   Bachelor_Master_Backelor_Master_PhD |
            3. |  3          1        0     0        1                         Bachelor_only |
               +-----------------------------------------------------------------------------+
          
          . drop wanted
          
          .
          Kind regards,
          Carlo
          (StataNow 18.5)

          Comment


          • #6
            Hi Carlo,

            I tried using your code but it isn't working. In the new var "Overall_education" it shows Bachelor_only for all the line items. I've attached the screenshot of the data.
            Attached Files

            Comment


            • #7
              Raghavendra:
              as reported in the FAQ, please use -dataex- instead of screenshots (completely unhelpful, as it is impossible to elaborate on them). Thanks.
              Kind regards,
              Carlo
              (StataNow 18.5)

              Comment


              • #8
                Carlo: Sorry, my bad. I had not read the FAQ section completely. As suggested, I used dataex and this is how the data looks:

                . dataex bachelors phd masters Overall_education

                ----------------------- copy starting from the next line -----------------------
                Code:
                * Example generated by -dataex-. To install: ssc install dataex
                clear
                input float(bachelors phd masters Overall_education)
                1 0 0 1
                1 0 0 1
                1 0 1 0
                1 0 0 0
                1 1 0 0
                0 0 0 0
                1 0 1 0
                1 0 0 0
                1 0 0 0
                0 0 0 0
                1 1 1 0
                1 1 0 0
                1 0 1 0
                0 0 0 0
                1 0 1 0
                0 1 0 0
                1 1 0 0
                1 0 0 0
                1 0 0 0
                1 1 0 0
                1 1 1 0
                1 0 1 0
                1 0 0 0
                1 0 1 0
                0 1 1 0
                1 0 0 0
                1 1 0 0
                1 0 0 0
                0 0 0 0
                1 0 1 0
                1 0 0 0
                1 0 1 0
                0 0 0 0
                0 0 0 0
                1 0 0 0
                1 1 0 0
                1 0 1 0
                1 0 1 0
                0 0 1 0
                1 0 0 0
                1 1 1 0
                1 1 0 0
                0 0 0 0
                1 0 1 0
                0 1 0 0
                1 0 1 0
                1 0 0 0
                0 1 0 0
                1 1 0 0
                1 0 0 0
                1 0 1 0
                1 0 1 0
                1 0 1 0
                1 0 1 0
                1 0 0 0
                1 0 0 0
                0 0 0 0
                0 0 0 0
                1 0 1 0
                1 1 0 0
                1 0 0 0
                1 1 0 0
                1 0 0 0
                1 0 1 0
                1 1 0 0
                1 1 0 0
                1 0 1 0
                1 0 1 0
                1 1 1 0
                1 0 1 0
                1 0 0 0
                1 1 1 0
                1 1 0 0
                1 0 0 0
                1 0 1 0
                1 0 1 0
                1 0 1 0
                0 0 0 0
                0 1 0 0
                1 1 0 0
                1 1 0 0
                1 1 0 0
                1 1 1 0
                0 0 1 0
                1 1 0 0
                1 0 1 0
                1 0 1 0
                1 0 1 0
                1 1 0 0
                0 1 0 0
                0 0 0 0
                1 0 0 0
                0 0 0 0
                1 0 1 0
                0 0 0 0
                1 0 0 0
                0 1 0 0
                0 0 0 0
                1 0 0 0
                1 1 1 0
                end
                label values Overall_education Overall_education
                label def Overall_education 0 "Bachelor_only", modify
                label def Overall_education 1 "Bachelor_Master_Bachelor_Master_PhD", modify
                Regards,
                Raghavendra

                Comment


                • #9
                  Raghavendra:
                  thanks for -dataex- exmple.
                  The following shoud work, with the caveat that you also have no educated ids (=0) in your dataset.
                  Moreover, master and PhD imply bachelor:
                  Code:
                  egen wanted=group(bachelors masters phd)
                  g Overall_education=0 if wanted==1
                  replace Overall_education=1 if wanted==5
                  replace Overall_education=2 if wanted!=1 & wanted!=5
                  label define Overall_education 0 "No education" 1 "Bachelor" 2 "Master_PhD_or_both", modify
                  label val Overall_education Overall_education
                  drop wanted
                  Kind regards,
                  Carlo
                  (StataNow 18.5)

                  Comment

                  Working...
                  X