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 Rebecca Pope To statalist@hsphsun2.harvard.edu Subject Re: st: set obs by level (of multiple variables)? Date Thu, 17 Jan 2013 10:30:56 -0600

```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?
>
> Best wishes
>
> Tim
>
>
>
>
> -----Original Message-----
> From: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Nick Cox
> Sent: 17 January 2013 15:53
> To: statalist@hsphsun2.harvard.edu
> Subject: Re: st: set obs by level (of multiple variables)?
>
> 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/
>
> _DISCLAIMER:
> This email and any attachments hereto contains proprietary information, some or all of which may be confidential or legally privileged. It is for the exclusive use of the intended recipient(s) only. If an addressing or transmission error has misdirected this e-mail and you are not the intended recipient(s), please notify the author by replying to this e-mail. If you are not the intended recipient you must not use, disclose, distribute, copy, print, or rely on this e-mail or any attachments, as this may be unlawful.
>
>
> *
> *   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/

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