Bookmark and Share

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


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: Listing the first 5 observations


From   "Roger B. Newson" <[email protected]>
To   [email protected]
Subject   Re: st: Listing the first 5 observations
Date   Wed, 29 Aug 2012 15:31:38 +0100

How about:

sort age, stable
by age: list if _n<=5

This will sort your dataset by age (preserving the existing order within age groups), and then list the first 5 observatios in each age group. As you say, -in- cannot be combinedwith -by:-.

I hope this helps.

Best wishes

Roger

Roger B Newson BSc MSc DPhil
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]
Web page: http://www.imperial.ac.uk/nhli/r.newson/
Departmental Web page:
http://www1.imperial.ac.uk/medicine/about/divisions/nhli/respiration/popgenetics/reph/

Opinions expressed are those of the author, not of the institution.

On 29/08/2012 15:23, Damian Finlayson wrote:
Hi,

This seems like a relatively simple query but no matter which way I
try I cannot get it to work.

I am trying to randomly select a number of observations according a
set criteria.

Say I have 3 variables: var1, var2 and random.  Var1 consists purely
of unique values (such as names).  Var 2 has groups/categories (e.g.
age). Random consists of unique random numbers.

I would like to get stata to sort each category (age) by a random
number and then list the first 5 names for a given age say 49.

The command I am trying to use is:

list var1 if age == 49

but I cannot seem to amend this command so that only the first 5
observations  (as per the random number) are listed.  If I use in 1/5
it does not work as the first observation for age 49 could be the 53
observation the second at 114 and so on.  If I ask for all
observations I could end up with thousands of names being listed.

Thanks in advance for your help,
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/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/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index