Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down at the end of May, and its replacement, statalist.org is already up and running.


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

Re: st: Need help with working across variables


From   Michael Stewart <michaelstewartresearch@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Need help with working across variables
Date   Sun, 10 Feb 2013 00:53:21 -0500

Dear Cory,
This works,
Thanks a lot
on the same lines have another question

id   var
1    2
1    2
1    3
1    2
2    1
2    1
2    1
2    .
2    1
3    1
3   2
3   4

How do I check of all(nonmissing) observations for a particular id are the same
thanks
Mike

On Sun, Feb 10, 2013 at 12:34 AM, Cory Smith <corybsmith@gmail.com> wrote:
> 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 <alison.connor@gmail.com> 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
>> <michaelstewartresearch@gmail.com> 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/



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


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