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: Creating a variable with a value conditional on other variables

 From Nick K To statalist@hsphsun2.harvard.edu Subject Re: st: Creating a variable with a value conditional on other variables Date Sat, 9 Apr 2011 17:29:51 +0200

```Thanks Nick, your input and the link were very helpful.
It seems that the by option/command is the key to all this, but what I
can't figure out is how to do a by within a by. So my "family" is the
variable SIC, but I also want it to do it by YEAR. I tried to do a by
within a by but it doesn't work.

On Sat, Apr 9, 2011 at 2:28 PM, Nick Cox <njcoxstata@gmail.com> wrote:
> 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
> <tirthankar.chakravarty@gmail.com> 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 <nick.stata@gmail.com> 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/
```

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