Announcement

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

  • error generating a new variable

    Hi!

    I am trying to run thw following code, but it displays an error: type mismatch

    gen birthreg= 2 if lugnac_departdos==15 | lugnac_departdos==17 |/*
    **/ lugnac_departdos==18 | lugnac_departdos==25 |/*
    **/ lugnac_departdos==41 | lugnac_departdos==50 | /*
    **/ lugnac_departdos==54 | lugnac_departdos==63 |/*
    **/ lugnac_departdos==66 | lugnac_departdos==68 | /*
    **/ lugnac_departdos==73 | lugnac_departdos==76
    replace birthreg= 3 if lugnac_departdos==1 | lugnac_departdos==88| /*
    **/ pais_nacio==1 | pais_nacio==2 |/*
    **/ pais_nacio==3 | pais_nacio==4 | /*
    **/ pais_nacio==5 | pais_nacio==6
    replace birthreg= 1 if lugnac_departdos==5 | lugnac_departdos==8 | /*
    **/ lugnac_departdos==13 | lugnac_departdos==19 | /*
    **/ lugnac_departdos==20 | lugnac_departdos==23 | /*
    **/ lugnac_departdos==27 | lugnac_departdos==44 | /*
    **/ lugnac_departdos==47 | lugnac_departdos==52 | /*
    **/ lugnac_departdos==70| lugnac_departdos==81 | /*
    **/ lugnac_departdos==85 | lugnac_departdos==86 | /*
    **/ lugnac_departdos==91 | lugnac_departdos==94 | /*
    **/ lugnac_departdos==95 |lugnac_departdos==97 | /*
    **/ lugnac_departdos==99

    I have not figured it out what the probem is

  • #2
    Well, this code presumes that all of the variables mentioned, birthreg, lugnac_departdos, and pais_nacio are numeric. The type mismatch message is telling you that at least one of them isn't. If you run -des birthreg lugnac_departdos pais_nacio- and look at the Storage type column of the table that Stata responds with, you will be able to figure out which one(s) is(are) the problem. Wherever the storage type begins with str, you have a string variable, not numeric. To fix the problem, -destring, replace- that variable. See -help destring- for further details.

    Be warned: if this data was originally imported from another source, having a variable that you think should be numeric turn out to be a string variable usually means that some values of this variable contain non-numeric entries. So if -destring- gives you a message saying that this is the case and refuses to convert the variable, resist the temptation to use the -force- option. Instead, identify the non-numeric values of the variable with -browse if missing(real(name_of_the_problem_variable))- and then figure out what to do about them.

    As an aside, this code is a lot to type, which is error-prone and tedious. It is also not pleasant to look at. You can shorten the code, as well as making it easier to read by replacing those long series of conditions joined by | with a corresponding expression using the -inlist()- function. For example
    Code:
    gen birthreg= 2 if lugnac_departdos==15 | lugnac_departdos==17 |/*
    **/ lugnac_departdos==18 | lugnac_departdos==25 |/*
    **/ lugnac_departdos==41 | lugnac_departdos==50 | /*
    **/ lugnac_departdos==54 | lugnac_departdos==63 |/*
    **/ lugnac_departdos==66 | lugnac_departdos==68 | /*
    **/ lugnac_departdos==73 | lugnac_departdos==76
    
    is a great deal easier to both write and read as
    
    gen birthreg = 2 if inlist(lugnac_departdos, 15, 17, 18, 25, 41, 50, 54, 63, 66, 68, 73, 76)
    Do read -help inlist()- for additional details.

    Comment


    • #3
      Dear Clyde,
      thanks for your time and advice. I have solved the issue with my variable and followed your suggestion with inlist.

      Comment

      Working...
      X