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!
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).
describe a b c d e
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.
@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
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:
*=======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.