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

Re: st: Top 10 Tricks in Stata (summary to date)

From   Roger Newson <>
Subject   Re: st: Top 10 Tricks in Stata (summary to date)
Date   Sun, 23 May 2004 19:26:56 +0100

At 18:32 23/05/04 +0100, Nick Cox wrote:
On Friday 21 May 2004 I asked

Which would we nominate as (say) the top ten tricks which are the
deepest and most Stataish features in what we use? What is _both_
simple _and_ deep?  What leads to great results with at most a few
lines of code?


I'd nominate straight away

1. -by:-.

2. -foreach- or -forval- with varlists or numlists.

3. -merge-. I rarely use it but -merge-masters have real leverage
in file manipulations.

4. -assert-. My candidate for the most underestimated command in
Stata (second is -count-).

5. -reshape-.

Any other nominations?



Here is a compilation of responses, edited a bit. I omitted some
very general comments and the most facetious suggestions.  I don't
think everyone was playing exactly the same game, not that it
Nobody seems to have mentioned the -generate()- option of -gsort-, which can be very useful for making non-byable programs byable, especially in combination with -levels-. Typically, if -do_whatever- is a non-byable program, then the user can write

gsort byvar_1 byvar_2 byvar_n, gene(bygp)
levels bygp, local(bygps)
foreach B of numlist `bygps' {
do_whatever if bygp==`B'

and execute -do_whatever- for each by-group defined by the by-variables byvar_1, byvar_2 and byvar_n. Of course, a simple loop like the above can be improved on in specific cases by adding other commands, eg to provide a heading for each iteration.


Roger Newson
Lecturer in Medical Statistics
Department of Public Health Sciences
King's College London
5th Floor, Capital House
42 Weston Street
London SE1 3QD
United Kingdom

Tel: 020 7848 6648 International +44 20 7848 6648
Fax: 020 7848 6620 International +44 20 7848 6620
or 020 7848 6605 International +44 20 7848 6605

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

* For searches and help try:

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