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

# Re: st: Creating a variable with a value conditional on other variables

 From Nick Cox <[email protected]> To <[email protected]> Subject Re: st: Creating a variable with a value conditional on other variables Date Sat, 9 Apr 2011 13:28:51 +0100

```This is an FAQ. See

FAQ     . . Creating variables recording prop. of the other members of a group
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
4/05    How do I create variables summarizing for each
individual properties of the other members of a
group?
http://www.stata.com/support/faqs/data/members.html.

But in any case, there is a direct approach that will seem obvious
when explained.

First, the mean of all the others is naturally just

(sum of all the others) / (count of all the others)

and "sum of all the others" is

sum of all - this value

so we can go

su myvar, meanonly

gen meanothers = (r(sum) - myvar) / (r(N) - 1)

This needs a fix for when -myvar- is missing, too. That is going to
be, I think,

gen meanothers = (r(sum) - cond(missing(myvar), 0, myvar)) / (r(N)  - 1)

but the FAQ goes through this and a variety of problems in the same territory.

Nick

On Sat, Apr 9, 2011 at 12:19 PM, Tirthankar Chakravarty
<[email protected]> wrote:
> -jackknife- it:
>
> *********************
> sysuse auto, clear
> jackknife r(mean), keep: su mpg
> su mpg
> li _jk_1 in 1/10
> *********************
>
> The variable you need is stored as "_jk_1".
>
> T
>
> On Sat, Apr 9, 2011 at 4:05 AM, Nick K <[email protected]> wrote:
>> Hi,
>>
>> I'm new to Stata and am having a hard time figuring out a relatively
>> simple problem. I'm experienced with other programming languages and
>> SQL so pseudo code may get my point across more clearly.
>>
>> foreach(observation)
>> {
>>    variable Xi = average(Y) if A != Ai
>> }
>>
>> Basically it would go over every observation and create a variable of
>> the average of some other variable NOT INCLUDING the variable's value
>> for the current row. So X is the average of Y for all observations
>> EXCEPT the current one.
>>
>> Thanks

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

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