Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

RE: st: help to understand syntax 'qui by study: gen last=_n==_N'


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   RE: st: help to understand syntax 'qui by study: gen last=_n==_N'
Date   Sun, 27 Nov 2005 17:12:46 -0000

First, there is a crucial difference between -N and _N
but -N probably only appears here as a result of 
your typing. 

Second, you can work these things out by trial on 
a simple dataset such as Stata's auto data. 

Either way, what you quote depends on a prior

sort study

The -quietly- you understand. 

by study: gen last = _n == _N 

generates a variable which evaluates _n == _N. That 
is true, numerically 1, when the observation number 
_n is equal to the total number of observations _N, 
and false, numerically 0, otherwise. The crucial 
detail is that under -by:- _n and _N are understood
to mean within each group of -study-. 

sysuse auto
bysort rep78 : gen byte last = _n == _N 
edit rep78 last 

will give you an easy example. In general, 
this tags the last observation in each 
group with 1 and the others with 0. 

See also 

Manual entries indexed under -by-

How to move step by: step
Stata Journal 2(1): 86-102 (2002) 
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

What is true and false in Stata?                http://www.stata.com/support/faqs/data/trueorfalse.html

Nick 
[email protected] 

b. water
 
> i am trying to understand one of the do-files application for 
> stata. one of 
> the lines read:
> 
> qui by study: gen last=_n==_N
> 
> my understanding so far: this is quietly, sort according to 
> study, and as a 
> result, to generate a variable 'last' fulfilling this 
> condition _n==-N. 
> although i have the output, i would like to know what in 
> general does gen last=_n==_N do?

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index