Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: st: RE: re: creating a variable from many variables


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   RE: st: RE: re: creating a variable from many variables
Date   Wed, 17 Feb 2010 11:12:40 -0000

I'm not clear whether this includes an extra question but there are several ways to catch observations in which all values are missing: see e.g. -egen- help on -row*()- functions. 

For a review of working row-wise, see 

SJ-9-1  pr0046  . . . . . . . . . . . . . . . . . . .  Speaking Stata: Rowwise
        (help rowsort, rowranks if installed) . . . . . . . . . . .  N. J. Cox
        Q1/09   SJ 9(1):137--157
        shows how to exploit functions, egen functions, and Mata
        for working rowwise; rowsort and rowranks are introduced

Nick 
n.j.cox@durham.ac.uk 

Rajaram Subramanian Potty

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
 .       .       .       .       .       .       .

On Tue, Oct 6, 2009 at 11:56 PM, Nick Cox <n.j.cox@durham.ac.uk> 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.

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/



© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index