Statalist The Stata Listserver

[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: Ordering a dataset by frequency

From (William Gould, Stata)
Subject   Re: st: Ordering a dataset by frequency
Date   Mon, 05 Mar 2007 11:54:35 -0600 wrote, 

> I have a dataset of 50,000 names.  I need to order them by frequency so 
> that the names that recur the most are at the top of the list, decreasing 
> by frequency.  [...]

If I just wanted to list the names and frequencies, in effect making a
one-way tabulation, I would type 

        . use dataset 
        . keep name 
        . sort name 
        . by name: gen freq = _N
        . by name: keep if _n==1
        . gsort -freq name
        . list name freq

If I wanted to save the frequencies with the original data:

        . use dataset 
        . sort name
        . by name: gen freq=_N
        . save, replace

If I wanted to list the entire dataset with the most frequent names on top, 

        . gen negfreq = -freq
        . sort negfreq name

-- Bill
*   For searches and help try:

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