Announcement

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

  • generate variable if not missing; error: type mismatch

    Hello everybody,

    I think this is a quite easy question but nontheless I haven't found a solution yet.

    I want to generate a variable x including several conditions. The first condition refers to a dummy variable (a) --> x=1 if a==1. the other variables contain open string answers and I want to generate x=1 if any answer for the variable is given. This means only missing values do not apply to this condition.
    So far I haven't found a solution how I can "count" cells that do contain an answer.

    I tried this:
    gen x = 1 if a==1 | b==" " | c==" " | d==" " | e==" "

    Stata gives me an error "type mismatch".

    Any suggestions?
    Thank you!

  • #2
    Take a look at the output from describe; it shows the storage type for each variable. The reason for the error message almost certainly is that a is a string variable or one of b, c, d, and e is numeric (byte, int, long, float or double).
    Code:
    describe a b c d e

    Comment


    • #3
      Note first that missing for Stata strings is an empty or blank string, not one or more spaces. Naturally, if you want to regard a string with spaces as missing, that is your decision.

      The function missing() can be used with a mix of string and numeric arguments.

      Please note our strong preference for full real names such as "Svend Juul": "Jessy1611" doesn't qualify as such.

      Comment


      • #4
        @Svend Juul: You are right, the storage types are a=byte, b,c=str & d,e=long
        but I just want to look if there is any answer given f.e. for the variables. How can I tell stata: "look if there is an answer (no matter which) and count. if there is an answer given it fulfills the condition for x=1".
        The expresion " "/ or "<>" is how it works with excel so I thought I would try if it works for stata as well. With this you tell excel "look if cell is filled".

        @Nick Cox: I will change my username for the next post

        Comment


        • #5
          For d and e, missing is "." not "" (there are additional missing value codes .a, .b, .c etc. but you seldom encounter them). Nick actually gave you an even better option, missing(). So something like:
          Code:
          
          
          *=======this should work:
          gen x = 0 if a==0 & b=="" & c=="" & d==. & e==.
          replace x=1 if x==.
          *=======or better
          gen x=0 if a==0  & missing(b) & missing(c)  & missing(d) & missing(e)
          replace x=1 if x==.
          *=======or even better (in case blanks crept into b or c
          gen x=0 if a==0  & missing(trim(b))  & missing(trim(c)) & missing(d) & missing(e)
           replace x=1 if x==.
          gets you a zero if missing on *all* the variables, which is what I think you want. Based on your code, it was missing on any of them, but your text says missing on all of them.
          Last edited by ben earnhart; 09 Nov 2014, 07:13.

          Comment


          • #6
            Use the missing() function; it works both for numeric and string variables. This would be valid whether b is numeric or string:
            Code:
            gen x=1 if missing(b)            // if b is missing
            gen x=1 if ! missing(b)          // if b is not missing
            If you want the check the missingness of several variables, use egen's rowmiss() and rownonmiss() functions:
            Code:
            egen x1 = rowmiss(a b c d e)
            egen x2 = rownonmiss(a b c d e)

            Comment


            • #7
              Does rowmiss() allow for strings? I considered the rownonmiss() with the "strok" option, but figured spelling it out variable by variable might be preferred for learning conditionals in general.

              Comment


              • #8
                Ah. "rowmiss()" does not allow for ",strok", but "rownonmiss(), strok" would do the job.
                Last edited by ben earnhart; 09 Nov 2014, 07:29. Reason: typo

                Comment

                Working...
                X