Announcement

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

  • Code Mismatch. Why?

    Hello,

    I have a variable, year, that has the year of birth of participants. I would like to create a new variable, age, that describes the age of each participant in 2023. I tried the following code: gen age=2023-year

    I get a return of a mismatch error. Why and what would be the proper code to create my new variable?

    Thanks in advance.

  • #2
    I guess year is a string variable and the error message was type mismatch. If so,
    Code:
    destring year, replace
    If not, or if that doesn't work, we need more details please: a data example and the exact error message(s) (don't paraphrase or rewrite).

    Comment


    • #3
      gen age = 2023-year
      type mismatch
      r(109);
      error . . . . . . . . . . . . . . . . . . . . . . . . Return code 109
      type mismatch;
      In an expression, you attempted to combine a string and numeric
      subexpression in a logically impossible way. For instance, you
      attempted to subtract a string from a number or you attempted
      to take the substring of a number.

      destring year, replace
      year: contains nonnumeric characters; no replace

      Comment


      • #4
        gen age = 2023-year
        type mismatch
        r(109);
        error . . . . . . . . . . . . . . . . . . . . . . . . Return code 109
        type mismatch;
        In an expression, you attempted to combine a string and numeric
        subexpression in a logically impossible way. For instance, you
        attempted to subtract a string from a number or you attempted
        to take the substring of a number.

        destring year, replace
        year: contains nonnumeric characters; no replace

        *All year variables are numeric

        Comment


        • #5
          *All year variables are numeric
          So you think. But Stata thinks otherwise. And I have never known Stata to get this kind of thing wrong. You just don't know your data as well as Stata does.

          Run -describe year- and I'm willing to be it will show you that it's storage type is a string.

          The fact that -destring- says year contains non-numeric characters tells you that, notwithstanding its name, the variable year contains extraneous material. Your task is to find it and fix it.

          Run:
          Code:
          levelsof year if missing(real(year))
          and you will get a listing of all the values of the year variable that contain non-numeric material. Depending on what that looks like, you will need to write and run some code to clean up that variable.

          Caveat: It can happen that the output of the code that I have suggested you run will look like it has no non-numeric characters in it. That can happen if the contaminating material consists of non-printing characters. Those are characters that Stata can see but are not shown in listings and displays. If this happens to you, get Robert Picard's -chartab- command from SSC and run
          Code:
          chartab year
          It will give you a complete listing, with explanations, and both decimal and hexadecimal character codes, of all the characters that appear in the variable. Reading the list you will easily find the non-numeric characters that are problematic, and then you can write your clean-up code.

          Comment


          • #6
            Issue has been resolved.

            Comment


            • #7
              It would be nice if you would describe how you resolved the issue so that others who encounter a similar problem can learn from your experience.

              Comment

              Working...
              X