Announcement

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

  • creating a new var: Type mismatch

    Hi

    I am going to classify firms into regions. I have a variable that specifies the firm's locations: "nut2_emp", When I tried to generate the region variable it keeps saying type mismatch. Is there any of specialist to assist me what is the possible problem? Thank you so much.

    Cheers,

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input double nut2_emp
    20
    16
    11
    16
    16
    16
    16
    16
    16
    16
    17
    17
    15
    15
    11
    11
    11
    11
    16
    16
    16
    15
    11
    15
    17
    15
    18
    15
    17
    16
    16
    16
    17
    16
    17
    17
    17
    17
    16
    15
    17
    16
    17
    17
    11
    17
    16
    11
    17
    17
    11
    17
    11
    16
    17
    17
    17
    17
    15
    11
    16
    18
    17
    17
    16
    16
    11
    11
    17
    17
    11
    17
    17
    17
    17
    17
    16
    17
    11
    18
    11
    30
    11
    11
    30
    16
    17
    17
    17
    15
    18
    16
    16
    15
    11
    16
    16
    17
    17
    17
    end
    label values nut2_emp nut2_emp
    label def nut2_emp 11 " Norte", modify
    label def nut2_emp 15 " Algarve", modify
    label def nut2_emp 16 " Centro", modify
    label def nut2_emp 17 " Lisboa", modify
    label def nut2_emp 18 " Alentejo", modify
    label def nut2_emp 20 " Azores", modify
    label def nut2_emp 30 " Madeira", modify
    
    
    
    gen region=0
    replace region=1 if nut2_emp=="Norte"
    type mismatch
    
    replace region=2 if nut2_emp=="Centro"
    replace region=3 if nut2_emp=="Lisboa"
    replace region=4 if nut2_emp=="Algarve"
    replace region=5 if nut2_emp=="Alentejo"
    replace region=6 if nut2_emp=="Azores"
    replace region=7 if nut2_emp=="Madeira"
    replace region=8 if nut2_emp=="Estrangeiro"
    Last edited by Paris Rira; 01 Feb 2023, 13:45.

  • #2
    There are a number of problems here.

    The first is the fact that all of the labels in value label nut2_emp begin with a leading blank. That's legal, but it's also not compatible with then referring to those values without the leading blank. So you need to be consistent. It's either " Norte" or "Norte" all the time, but not mixing and matching.

    Next, since nut2_emp is a numeric variable, you cannot ask Stata whether or not it is equal to a string like "Norte". You have to ask whether it corresponds to the numeric value paired with "Norte" in the value label. Finally, I notice that for regions 1 and 2 you refer to nut2_emp, but then for regions 3 and higher you are referring to a different variable nut2_est. There is no nut2_est variable in your example data, and I don't know if such a variable exists in your full data set or whether that is just typographical error. I'm assuming typographical error. In that case your code gets fixed with:
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input double nut2_emp
    20
    16
    11
    16
    16
    16
    16
    16
    16
    16
    17
    17
    15
    15
    11
    11
    11
    11
    16
    16
    16
    15
    11
    15
    17
    15
    18
    15
    17
    16
    16
    16
    17
    16
    17
    17
    17
    17
    16
    15
    17
    16
    17
    17
    11
    17
    16
    11
    17
    17
    11
    17
    11
    16
    17
    17
    17
    17
    15
    11
    16
    18
    17
    17
    16
    16
    11
    11
    17
    17
    11
    17
    17
    17
    17
    17
    16
    17
    11
    18
    11
    30
    11
    11
    30
    16
    17
    17
    17
    15
    18
    16
    16
    15
    11
    16
    16
    17
    17
    17
    end
    label values nut2_emp nut2_emp
    label def nut2_emp 11 "Norte", modify
    label def nut2_emp 15 "Algarve", modify
    label def nut2_emp 16 "Centro", modify
    label def nut2_emp 17 "Lisboa", modify
    label def nut2_emp 18 "Alentejo", modify
    label def nut2_emp 20 "Azores", modify
    label def nut2_emp 30 "Madeira", modify
    
    
    
    gen region=0
    replace region=1 if nut2_emp=="Norte":nut2_emp
    replace region=2 if nut2_emp=="Centro":nut2_emp
    replace region=3 if nut2_emp=="Lisboa":nut2_emp
    replace region=4 if nut2_emp=="Algarve":nut2_emp
    replace region=5 if nut2_emp=="Alentejo":nut2_emp
    replace region=6 if nut2_emp=="Azores":nut2_emp
    replace region=7 if nut2_emp=="Madeira":nut2_emp
    replace region=8 if nut2_emp=="Estrangeiro":nut2_emp
    If you really do wish to refer to a variable nut2_est, then post back with a new -dataex- that includes both nut2_emp and nut2_est.

    Comment


    • #3
      Prof, thank you for getting back to me.
      nut2_est was a typo, although there exist such a variable in the data which is locations at the establishment level. I already edited
      Code:
      . gen region=0
      
      . replace region=1 if nut2_emp=="Norte":nut2_emp
      (value label dereference "Norte":nut2_emp not found)
      (0 real changes made)
      
      . replace region=2 if nut2_emp=="Centro":nut2_emp
      (value label dereference "Centro":nut2_emp not found)
      (0 real changes made)
      
      . replace region=3 if nut2_emp=="Lisboa":nut2_emp
      (value label dereference "Lisboa":nut2_emp not found)
      (0 real changes made)
      
      . replace region=4 if nut2_emp=="Algarve":nut2_emp
      (value label dereference "Algarve":nut2_emp not found)
      (0 real changes made)
      
      . replace region=5 if nut2_emp=="Alentejo":nut2_emp
      (value label dereference "Alentejo":nut2_emp not found)
      (0 real changes made)
      
      . replace region=6 if nut2_emp=="Azores":nut2_emp
      (value label dereference "Azores":nut2_emp not found)
      (0 real changes made)
      
      . replace region=7 if nut2_emp=="Madeira":nut2_emp
      (value label dereference "Madeira":nut2_emp not found)
      (0 real changes made)
      
      . replace region=8 if nut2_emp=="Estrangeiro":nut2_emp
      (value label dereference "Estrangeiro":nut2_emp not found)
      (0 real changes made)
      Last edited by Paris Rira; 01 Feb 2023, 13:53.

      Comment


      • #4
        Yes, but you also have to edit the value label nut2_emp change. Notice from your original code in #1:

        Code:
        label def nut2_emp 11 " Norte", modify
        label def nut2_emp 15 " Algarve", modify
        label def nut2_emp 16 " Centro", modify
        label def nut2_emp 17 " Lisboa", modify
        label def nut2_emp 18 " Alentejo", modify
        label def nut2_emp 20 " Azores", modify
        label def nut2_emp 30 " Madeira", modify
        But from my code in #2:
        Code:
        label def nut2_emp 11 "Norte", modify
        label def nut2_emp 15 "Algarve", modify
        label def nut2_emp 16 "Centro", modify
        label def nut2_emp 17 "Lisboa", modify
        label def nut2_emp 18 "Alentejo", modify
        label def nut2_emp 20 "Azores", modify
        label def nut2_emp 30 "Madeira", modify
        Notice that I have removed the leading blanks from the labels. You have to do that, too.

        Comment


        • #5
          You are right. First, I have to remove the leading blanks.
          It solved. Really appreciated.

          Code:
          label def nut2_emp 11 "Norte", modify
          label def nut2_emp 15 "Algarve", modify
           label def nut2_emp 16 "Centro", modify
          label def nut2_emp 17 "Lisboa", modify
          label def nut2_emp 18 "Alentejo", modify
          label def nut2_emp 20 "Açores", modify
          label def nut2_emp 30 "Madeira", modify
          label def nut2_emp 90 "Estrangeiro", modify
          
          
          . gen region=0
          
          . replace region=1 if nut2_emp=="Norte":nut2_emp
          (107,724 real changes made)
          
          . replace region=2 if nut2_emp=="Centro":nut2_emp
          (66,286 real changes made)
          
          . replace region=3 if nut2_emp=="Lisboa":nut2_emp
          (72,013 real changes made)
          
          . replace region=4 if nut2_emp=="Algarve":nut2_emp
          (16,278 real changes made)
          
          . replace region=5 if nut2_emp=="Alentejo":nut2_emp
          (21,010 real changes made)
          
          . replace region=6 if nut2_emp=="Açores":nut2_emp
          (5,492 real changes made)
          
          . replace region=7 if nut2_emp=="Madeira":nut2_emp
          (6,229 real changes made)
          
          . replace region=8 if nut2_emp=="Estrangeiro":nut2_emp
          (8 real changes made)
          
          . 
          end of do-file
          
          . tab region
          
               region |      Freq.     Percent        Cum.
          ------------+-----------------------------------
                    1 |    107,724       36.51       36.51
                    2 |     66,286       22.47       58.98
                    3 |     72,013       24.41       83.39
                    4 |     16,278        5.52       88.90
                    5 |     21,010        7.12       96.02
                    6 |      5,492        1.86       97.89
                    7 |      6,229        2.11      100.00
                    8 |          8        0.00      100.00
          ------------+-----------------------------------
                Total |    295,040      100.00

          Comment

          Working...
          X