Dear all,
I posted this around 6 months ago. As Nick pointed out I have to omit
all values that are missing. If all values are missing the new
variable also should be treated as missing. Now, I am giving the
example, since in the my original posting I did not mention any
observation with all values missing.
var1 var2 var3 var4 var5 var6 createdvar
4 . 4 4 4 . 1
2 4 . . . . 0
4 4 4 4 4 4 1
2 3 4 4 4 4 0
4 4 4 4 4 4 1
4 . . . . . 1
3 3 3 3 3 3 0
4 3 . . . . 0
2 3 1 4 3 . 0
1 1 . . . . 0
. . . . . . .
Thanks
RAJARAM. S
On Tue, Oct 6, 2009 at 11:56 PM, Nick Cox <[email protected]> wrote:
> Thanks for the various plugs.
>
> A more elegant method than is implemented in -egen, rall()- is
>
> gen byte four = 1
>
> foreach v of var var* {
> replace four = 0 if !(missing(`v') | `v' == 4)
> }
>
> An alternative for the middle line is
>
> replace four = 0 if !inlist(`v', 4, .)
>
> However, note the implication that any observation on which all values
> are missing is also tagged 1. Rajaram might want to exclude any such
> observations. It's not clear from the original post as there is no such
> example.
>
> Nick
> [email protected]
>
> Martin Weiss
> ============
>
> Also note Nick`s recent column
> http://www.stata-journal.com/article.html?article=pr0046
>
> Kit Baum
> ========
>
> ssc install egenmore (Nick Cox's grab bag of egen fns)
>
> . egen four = rall(var*), c(@ == 4 | mi(@))
>
> . l
>
> +------------------------------------------------+
> | var1 var2 var3 var4 var5 var6 four |
> |------------------------------------------------|
> 1. | 4 . 4 4 4 . 1 |
> 2. | 2 4 . . . . 0 |
> 3. | 4 4 4 4 4 4 1 |
> 4. | 2 3 4 4 4 4 0 |
> 5. | 4 4 4 4 4 4 1 |
> |------------------------------------------------|
> 6. | 4 . . . . . 1 |
> 7. | 3 3 3 3 3 3 0 |
> 8. | 4 3 . . . . 0 |
> 9. | 2 3 1 4 3 . 0 |
> 10. | 1 1 . . . . 0 |
> +------------------------------------------------+
>
>
> Rajaram Subramanian Potty
> =========================
>
> I have about 6 variables and I want to create a variable based on all
> these variables. If the all the variables take a particular value, I
> want to generate the variable as 1 otherwise 0. The problem is that for
> some of the cases some variables will not be applicable. The following
> is an example of the data for 10 cases. In the follwing example if the
> value of all the applicable variables is 4 then a created variable is 1
> otherwise it is 0 as shown below.
>
> var1 var2 var3 var4 var5 var6 createdvar
> 4 . 4 4 4 . 1
> 2 4 . . . . 0
> 4 4 4 4 4 4 1
> 2 3 4 4 4 4 0
> 4 4 4 4 4 4 1
> 4 . . . . . 1
> 3 3 3 3 3 3 0
> 4 3 . . . . 0
> 2 3 1 4 3 . 0
> 1 1 . . . . 0
>
>
> *
> * For searches and help try:
> * http://www.stata.com/help.cgi?search
> * http://www.stata.com/support/statalist/faq
> * http://www.ats.ucla.edu/stat/stata/
>
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/