Announcement

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

  • Type Mismatch

    Hello,

    I need some help:

    I am using the data from this https://dataverse.harvard.edu/datase...910/DVN/II2DB6

    The following states have banned abortion:

    "AL"
    "AR"
    "ID"
    "KY"
    "LA"
    "MS"
    "MO"
    "OK"
    "SD"
    "TN"
    "TX"
    "WV"
    "WI"

    I want to create a variable ''BanYes'' = 1 for the above states.

    I tried using this code:

    gen BanYes = 0
    replace BanYes = 1 if st == "AL"
    replace BanYes = 1 if st == "AR"
    replace BanYes = 1 if st == "ID"
    replace BanYes = 1 if st == "KY"
    replace BanYes = 1 if st == "LA"
    replace BanYes = 1 if st == "MS"
    replace BanYes = 1 if st == "MO"
    replace BanYes = 1 if st == "OK"
    replace BanYes = 1 if st == "SD"
    replace BanYes = 1 if st == "TN"
    replace BanYes = 1 if st == "TX"
    replace BanYes = 1 if st == "WV"
    replace BanYes = 1 if st == "WI"

    However, stata says ''type mismatch''.

    How do I solve this?

  • #2
    There is a difference between a string variable (in red font while browsing the data) and a numerical variable with value labels (in blue font). The error can occur if the variable "st" is a numerical variable with value labels and you are treating it as a string.

    Code:
    browse st
    To see the corresponding numerical values, run

    Code:
    lab list
    Also see

    Code:
    help inlist
    for a more efficient method to the series of replace commands that you have.

    Comment


    • #3
      Thanks for the help. With lab list I can now see each value given to each state.

      Now it works

      By the way, I am not using inlist, because stata says the expression is too long.

      Comment


      • #4
        See this thread for solutions using -inlist()- and alternatives: https://www.statalist.org/forums/for...ssion-too-long.

        Comment


        • #5
          By the way, I am not using inlist, because stata says the expression is too long.
          Yes, with strings, inlist can take at most 10 arguments, which falls short for you. However, it is still better perhaps to chain two inlists together, like so:

          Code:
          gen byte BanYes = inlist(st, "AL", "AR", "ID", "KY", "LA", "MS", "MO", "OK", "SD") | inlist(st, "TN", "TX", "WV", "WI")

          Comment


          • #6
            As the variable in question st s evidently numeric, which is why the thread arises at all IIUC, the limit on the number of string arguments for inlist() will not bite.

            That said, if the code ends up looking like

            Code:
            gen BanYes = inlist(st, 1, 3, 12)
            or whatever the numbers are, then the code will not be especially transparent.

            Comment

            Working...
            X