UCLA Academic Technology Services HomeServicesClassesContactJobs
Search

Stata FAQ
Can I do by and sort in one command?

When you want to use by var: command you need to first sort var, however, you can use the command bysort to do both commands in one line.

Let's illustrate use of the bysort using the high school and beyond data file we use in our Stata classes.
use http://www.ats.ucla.edu/stat/stata/notes/hsb1, clear
(highschool and beyond (200 cases))
If we wanted summary statistics for each ses group, we would need to use sort and then by as illustrated below.
sort ses
by ses: summarize read write

-> ses=      low  
Variable |     Obs        Mean   Std. Dev.       Min        Max
---------+-----------------------------------------------------
    read |      47     48.2766   9.342987         28         68  
   write |      47    50.61702   9.490391         31         65  

-> ses=   middle  
Variable |     Obs        Mean   Std. Dev.       Min        Max
---------+-----------------------------------------------------
    read |      95    51.57895   9.425609         31         73  
   write |      95    51.92632   9.106044         31         67  

-> ses=     high  
Variable |     Obs        Mean   Std. Dev.       Min        Max
---------+-----------------------------------------------------
    read |      58        56.5   10.85834         34         76  
   write |      58    55.91379   9.442874         33         67
However, with the bysort command, you can save typing in the sort command. Please note that this still executes the sort command before the by command, but it relieves you of the need to separately type in the sort command.
bysort ses: summarize read write

sorting on ses 

-> ses=      low  
Variable |     Obs        Mean   Std. Dev.       Min        Max
---------+-----------------------------------------------------
    read |      47     48.2766   9.342987         28         68  
   write |      47    50.61702   9.490391         31         65  

-> ses=   middle  
Variable |     Obs        Mean   Std. Dev.       Min        Max
---------+-----------------------------------------------------
    read |      95    51.57895   9.425609         31         73  
   write |      95    51.92632   9.106044         31         67  

-> ses=     high  
Variable |     Obs        Mean   Std. Dev.       Min        Max
---------+-----------------------------------------------------
    read |      58        56.5   10.85834         34         76  
   write |      58    55.91379   9.442874         33         67

For More Information

For more information, see the help or reference manual about the sort command and by.

How to cite this page

Report an error on this page

UCLA Researchers are invited to our Statistical Consulting Services
We recommend others to our list of Other Resources for Statistical Computing Help
These pages are Copyrighted (c) by UCLA Academic Technology Services


The content of this web site should not be construed as an endorsement of any particular web site, book, or software product by the University of California.