Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


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

Re: st: Need help with working across variables


From   Cory Smith <[email protected]>
To   [email protected]
Subject   Re: st: Need help with working across variables
Date   Sun, 10 Feb 2013 00:34:20 -0500

Too complicated in my opinion.

gen x = 0

egen Vmin = rowmin(var1 - var4)
egen Vmax = rowmax(var1 - var4)

replace x = 1 if Vmin == 1 & Vmax == 1 & Vmin != .
replace x = 2 if Vmin == 2 & Vmax == 2 & Vmin != .

Actually, some of the terms in the above are unnecessary but I kept
them in for clarity.

On Sun, Feb 10, 2013 at 12:27 AM, Alison Connor <[email protected]> wrote:
> Hi Michael,
>
> There might be a more efficient way to do it, but if you have egenmore
> you can do:
>
>  egen x=rall(var1 var2 var3 var4), c(@==1)
>
> This will be 1 if all non-missing variables are 1, and 0 otherwise. If
> you only have two values for your variables, then you can run this
> command again, and then merge the two into a third variable:
>
>  egen x1=rall(var1 var2 var3 var4), c(@==1)
>  egen x2=rall(var1 var2 var3 var4), c(@==2)
>
> gen x=0
> replace x=1 if x1==1
> replace x=2 if x2==1
>
> If you are going to do this for multiple values, then you can create a loop:
>
> gen x=0
> forvalues i=1/2 {
>    egen x`i'=rall(var1 var2 var3 var4), c(@==`i')
>   replace x=`i' if x`i'==1
>   drop x`i'
>  }
>
> ~Alison
>
>
> On Sun, Feb 10, 2013 at 10:41 AM, Michael Stewart
> <[email protected]> wrote:
>>
>> HI ,
>> Here is my data format and my question:
>> id      var1    var2    var3    var4
>> 1       1       1       1       .
>> 2       1       1       1       1
>> 3       1       2       1       .
>> 4       2       2       2       2
>> 5       1       1       1       1
>> 6       1       2       .       .
>> 7       2       1       2       .
>>
>> I am trying to generate a new variable x =1 if all non missings values
>> for the variables(per observation) are equal to 1 and x =2 if all non
>> missings values for the variables(per observation) are equal to 2. I
>> tried to do foreach loop like
>>
>> gen x=0
>> foreach x in var1 var2 var3 var4{
>> replace x=1 if `x'=1
>> }
>> but it doesnt seem to do the trick
>>
>> I am specifically interested in to change x =1 if all nonmissign
>> values for an observation are equal to one
>> For example: I would like to replace x=1 for id=1 but not for id 3
>> Similarly, I would like to replace x=2 if all nom missing values for
>> an observation are equal to two.
>>
>> --
>> Thank you ,
>> Yours Sincerely,
>> Mike.
>> *
>> *   For searches and help try:
>> *   http://www.stata.com/help.cgi?search
>> *   http://www.stata.com/support/faqs/resources/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/faqs/resources/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/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index