Bookmark and Share

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

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

Re: st: Listing the first 5 observations

From   Nick Cox <>
Subject   Re: st: Listing the first 5 observations
Date   Wed, 29 Aug 2012 15:31:43 +0100

-if- and -in- are completely unrelated. Usually this is a feature, but
here it means that you have to do some work to get what you want.

One way is

gen OK = age == 49
sort OK
l var1 in -5/L

Another way is

browse var1 if age == 49

where in practice you will get a screenful.

I've not been serious about your stipulation of randomness; I guess
you mean no more than arbitrary.

Note: one criterion; two or more criteria.


On Wed, Aug 29, 2012 at 3:23 PM, Damian Finlayson
<> wrote:

> 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.
*   For searches and help try:

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