I think your answer will involve the "system variable" _n. If you type
gene seqnum=_n
then Stata will generate a variable named -seqnum-, whose value is the
sequential order of the observation in the dataset, starting from 1 for
the first observation. And if you type
gene seqord=_n-1
then Stata will generate a variable named -seqord-, whose value is the
sequential order of the observation in the dataset, starting from 0 for
the first observation. And if the dataset is sorted by a variable
-group-, and you type
by group: gene wseqnum=_n
then Stata will generate a new variable -wseqnum-, whose value is the
sequential order of the observation within its group.
There is also a system variable _N, containing the number of
observations in the dataset, or in the by-group if -by varlist:- is
used.
I hope this helps.
Roger
Roger Newson
Lecturer in Medical Statistics
Respiratory Epidemiology and Public Health Group
National Heart and Lung Institute
Imperial College London
Royal Brompton campus
Room 33, Emmanuel Kaye Building
1B Manresa Road
London SW3 6LR
UNITED KINGDOM
Tel: +44 (0)20 7352 8121 ext 3381
Fax: +44 (0)20 7351 8322
Email: [email protected]
www.imperial.ac.uk/nhli/r.newson/
Opinions expressed are those of the author, not of the institution.
-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of
[email protected]
Sent: 02 March 2007 16:34
To: [email protected]
Subject: st: new variable based on row numbers
Hello all,
I am having trouble figuring out how to create a new variable
based on the row numbers of my data. I just want to create a
sequentially numbered variable (x, x+1, x+2 ect...) to
uniquely identify each row of my data.
There is probably a real quick and simple solution to this, I
just haven't yet found it. Can anyone point me in the right
direction?
Thanks
Eleanor Friedman
*
* 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/
*
* 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/