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

st: RE: scripting question (_n)


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: scripting question (_n)
Date   Fri, 26 Aug 2005 16:52:53 +0100

Sounds like 

gen id = _n
bysort var1 (id) : gen var2 = sum(id != id[_n-1] + 1)
sort id 

Nick 
[email protected] 

kelly johnson
 
> I have a variable (var1) like this:
> 
> var1
> a
> a
> a
> a
> a
> b
> a
> a
> a
> b
> a
> ... (there are several thousand of these)
> 
> I want to creat a unique number to identify each set from a-b. e.g.:
> var1 var2
> a 1
> a 1
> a 1
> a 1
> a 1
> b 1
> a 2
> a 2
> a 2
> b 2
> a3
> ...
> I know how to use the _n command, but am unsure of how to 
> employ it in this 
> case (i can do it in the opposite order). any help would be 
> appreciated.

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