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 17:18:13 +0100

```No; that won't work.

As the help for -by:- indicates, -by:- (and typically also -by()-
options) take varlists, i.e. one or more variable names. So your
command will be something like

bysort SIC YEAR : ...

or

... by(SIC YEAR)

Nick

On Sat, Apr 9, 2011 at 4:29 PM, Nick K <[email protected]> wrote:

> 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 <[email protected]> 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
>> <[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.

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

• References: