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 12:20:20 -0500

Hi Alison, Cory and Nick,
Thanks a lot for the advice and multiple ways for dealing with the issues
Thanks a lot again.
yours sincerely
Mike.

On Sun, Feb 10, 2013 at 10:15 AM, Nick Cox <njcoxstata@gmail.com> wrote:
> One condition is redundant:
>
> gen x = .
>
> forval j = 1/4 {
>        replace x = var`j'  if missing(x) & inlist(var`j', 1, 2)
>        replace x = .  if  var`j'  != x & var`j' < .
> }
>
>
>
> On Sun, Feb 10, 2013 at 10:55 AM, Nick Cox <njcoxstata@gmail.com> wrote:
>> Corrected version:
>>
>> gen x = .
>>
>> forval j = 1/4 {
>>       replace x = var`j'  if missing(x) & inlist(var`j', 1, 2)
>>       replace x = . if !missing(x, var`j') & var`j'  != x
>> }
>>
>>
>>
>> On Sun, Feb 10, 2013 at 10:12 AM, Nick Cox <njcoxstata@gmail.com> wrote:
>>> Here is another way to do it:
>>>
>>> gen x = .
>>>
>>> forval j = 1/4 {
>>>      replace x = var`j'  if missing(x) & inlist(var`j', 1, 2)
>>>      replace x = . if !missing(x) & var`j'  != x
>>> }
>>>
>>> So, we initialise to missing; change our minds at the first instance
>>> of 1 or 2; but change it back again if we see a different value in the
>>> same observation.
>>>
>>> For a review of working rowwise, 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
>>>
>>> .pdf at http://www.stata-journal.com/sjpdf.html?articlenum=pr0046
>>>
>>> Nick
>>>
>>> On Sun, Feb 10, 2013 at 5: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'
>>>>>  }
>>>
>>> On Sun, Feb 10, 2013 at 10:41 AM, Michael Stewart
>>> <michaelstewartresearch@gmail.com> wrote:
>>>
>>>>>> 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.
>>>>
> *
> *   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