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

# Re: st: set obs by level (of multiple variables)?

 From Nick Cox To statalist@hsphsun2.harvard.edu Subject Re: st: set obs by level (of multiple variables)? Date Thu, 17 Jan 2013 17:33:39 +0000

```Rebecca gives good advice. The underlying principles are discussed in

FAQ     . . . . . . . . . . . . . . . . . . . . . . .  True and false in Stata
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
8/05    What is true and false in Stata?
http://www.stata.com/support/faqs/data-management/
true-and-false/

SJ-2-1  pr0004  . . . . . . . . . . Speaking Stata:  How to move step by: step
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
Q1/02   SJ 2(1):86--102                                  (no commands)
explains the use of the by varlist : construct to tackle
a variety of problems with group structure, ranging from
simple calculations for each of several groups to more
advanced manipulations that use the built-in _n and _N

On Thu, Jan 17, 2013 at 4:30 PM, Rebecca Pope <rebecca.a.pope@gmail.com> wrote:

> bysort TCATOG sex : gen first2 = (_n==1)
>
> That tests whether it is the first observation or not & returns 1 if
> true, 0 otherwise.

On Thu, Jan 17, 2013 at 10:24 AM, Tim Evans <Tim.Evans@wmciu.nhs.uk> wrote:

>> Nick thanks for your help. This does what I need, although, rather than duplicating the last record, duplicating the first might be more helpful as this would contain much of the baseline information I already hold. I naively thought that this would work!!:
>>
>> bysort TCATOG sex : gen first2 = _n - but I have 1-20 rather than 1 followed by 0
>>
>> I could then use replace first2 = 0 if first !=1 - but I'm assuming there is a better way?

Nick Cox

>> The syntax diagram for -set- does not indicate support for -by:- and
>>
>> 1. Whatever is not permitted should be considered forbidden.
>>
>> 2. Less gnomically, there is a really good reason for this. In essence, -set- is about global settings, and even if what you are asking for makes sense -- as it does here -- -set- and -by:- don't mix naturally.
>>
>> See help for -expand-, -expandcl-, -expandby- (SSC).
>>
>> bysort stage sex : gen last = _n == _N
>> expand 2 if last
>> sort stage sex
>> ... if last

On Thu, Jan 17, 2013 at 3:08 PM, Tim Evans <Tim.Evans@wmciu.nhs.uk> wrote:

>>> I'm trying to insert extra observations in my dataset - I've calculated survival and wish to graph the results but the data start from less than 100%, but I'd like the graph to graph from time 0 and thus 100%. My dataset is split by gender and stage so I need something that inserts an observation for say males & stage 1, males stage 2, females stage 1 and females stage 2.
>>>
>>> Unfortunately, while this will provide me with an observation
>>>
>>> set obs `=_N+1' it does not support this:
>>>
>>> bysort stage sex: set obs `=_N+1'
>>>
>>> Does anyone have an idea how I might do this in Stata 11.2?

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