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
Nick Cox <[email protected]>
To
[email protected]
Subject
Re: st: Need help with working across variables
Date
Sun, 10 Feb 2013 10:12:10 +0000
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 <[email protected]> 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 <[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'
>> }
On Sun, Feb 10, 2013 at 10:41 AM, Michael Stewart
<[email protected]> 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/