UCLA Academic Technology Services HomeServicesClassesContactJobs
Help the Stat Consulting Group by giving a gift             
Loading

Stata FAQ
 How can I list observations in blocks?

Sometimes the output of the list command can be clumsy, showing an entire observation with all of the variables wrapped across the screen. Sometimes you would like the data shown in columns without the observations wrapping across the screen. You can obtain an add on program called listblck that can do this. The listblck command was written by Jeroen Weesie and appears in the Stata Technical Bulletin #50. You can download listblck from within Stata by typing findit listblck (see How can I used the findit command to search for programs and get additional help? for more information about using findit).

First we're going to show some output from list to illustrate what it can do, and where you may run into problems, then we'll move on to show you what you can do with listblk. To do this we'll use the high school and beyond data file we use in our Stata Classes, the code below downloads the dataset.
use http://www.ats.ucla.edu/stat/stata/notes/hsb2, clear
(highschool and beyond (200 cases))

We use the list command to list the first five observations, and we see that this can be hard to read.

list in 1/5
     +-------------------------------------------------------------------------+
  1. |  id | female |  race |    ses | schtyp |     prog | read | write | math |
     |  70 |   male | white |    low | public |  general |   57 |    52 |   41 |
     |-------------------------------------------------------------------------|
     |               science               |               socst               |
     |                    47               |                  57               |
     +-------------------------------------------------------------------------+

     +-------------------------------------------------------------------------+
  2. |  id | female |  race |    ses | schtyp |     prog | read | write | math |
     | 121 | female | white | middle | public | vocation |   68 |    59 |   53 |
     |-------------------------------------------------------------------------|
     |               science               |               socst               |
     |                    63               |                  61               |
     +-------------------------------------------------------------------------+

     +-------------------------------------------------------------------------+
  3. |  id | female |  race |    ses | schtyp |     prog | read | write | math |
     |  86 |   male | white |   high | public |  general |   44 |    33 |   54 |
     |-------------------------------------------------------------------------|
     |               science               |               socst               |
     |                    58               |                  31               |
     +-------------------------------------------------------------------------+

     +-------------------------------------------------------------------------+
  4. |  id | female |  race |    ses | schtyp |     prog | read | write | math |
     | 141 |   male | white |   high | public | vocation |   63 |    44 |   47 |
     |-------------------------------------------------------------------------|
     |               science               |               socst               |
     |                    53               |                  56               |
     +-------------------------------------------------------------------------+

     +-------------------------------------------------------------------------+
  5. |  id | female |  race |    ses | schtyp |     prog | read | write | math |
     | 172 |   male | white | middle | public | academic |   47 |    52 |   57 |
     |-------------------------------------------------------------------------|
     |               science               |               socst               |
     |                    53               |                  61               |
     +-------------------------------------------------------------------------+

We can try to the table option, which will put each case on a single line, which should be easier to read. (Note table is the default display method for list; however, Stata will switch to display mode under some circumstances.)

 list in 1/5, table

     +----------------------------------------------------------------------------
> ---------------+
     |  id   female    race      ses   schtyp       prog   read   write   math   s
> cience   socst |
     |----------------------------------------------------------------------------
> ---------------|
  1. |  70     male   white      low   public    general     57      52     41    
>     47      57 |
  2. | 121   female   white   middle   public   vocation     68      59     53    
>     63      61 |
  3. |  86     male   white     high   public    general     44      33     54    
>     58      31 |
  4. | 141     male   white     high   public   vocation     63      44     47    
>     53      56 |
  5. | 172     male   white   middle   public   academic     47      52     57    
>     53      61 |
     +----------------------------------------------------------------------------
> ---------------+

Looking at the above output, we can see why Stata selected display mode, that is, to avoid having the table wrap. This is where listblck is useful. Compare the above output with the result from the listblck command below.

listblck in 1/5

  . listblck in 1/5

     +---------------------------------------------------+
     |  id   female    race      ses   schtyp       prog |
     |---------------------------------------------------|
  1. |  70     male   white      low   public    general |
  2. | 121   female   white   middle   public   vocation |
  3. |  86     male   white     high   public    general |
  4. | 141     male   white     high   public   vocation |
  5. | 172     male   white   middle   public   academic |
     +---------------------------------------------------+

     +---------------------------------------+
     | read   write   math   science   socst |
     |---------------------------------------|
  1. |   57      52     41        47      57 |
  2. |   68      59     53        63      61 |
  3. |   44      33     54        58      31 |
  4. |   63      44     47        53      56 |
  5. |   47      52     57        53      61 |
     +---------------------------------------+
listblk has a number of options that allow us to modify what is shown. We can use the nolabel option to suppress the printing of value labels, but you can see that it still displays the data in a very wide format.  This is because the formats for the variables try to provide enough space to display the labeled value (e.g., for the variable female, there is enough room to display the value "female").
listblck in 1/5, nolabel
	
     +-------------------------------------------+
     |  id   female   race   ses   schtyp   prog |
     |-------------------------------------------|
  1. |  70        0      4     1        1      1 |
  2. | 121        1      4     2        1      3 |
  3. |  86        0      4     3        1      1 |
  4. | 141        0      4     3        1      3 |
  5. | 172        0      4     2        1      2 |
     +-------------------------------------------+

     +---------------------------------------+
     | read   write   math   science   socst |
     |---------------------------------------|
  1. |   57      52     41        47      57 |
  2. |   68      59     53        63      61 |
  3. |   44      33     54        58      31 |
  4. |   63      44     47        53      56 |
  5. |   47      52     57        53      61 |
     +---------------------------------------+
    

To try and fit more on a single line, you can suppress the display of the observation number.

listblck in 1/5, nolabel noobs
	
  +--------------------------------------------------+
  |  id   female   race   ses   schtyp   prog   read |
  |--------------------------------------------------|
  |  70        0      4     1        1      1     57 |
  | 121        1      4     2        1      3     68 |
  |  86        0      4     3        1      1     44 |
  | 141        0      4     3        1      3     63 |
  | 172        0      4     2        1      2     47 |
  +--------------------------------------------------+

  +--------------------------------+
  | write   math   science   socst |
  |--------------------------------|
  |    52     41        47      57 |
  |    59     53        63      61 |
  |    33     54        58      31 |
  |    44     47        53      56 |
  |    52     57        53      61 |
  +--------------------------------+
In the examples above, sometimes the socst variable was displayed all alone and you could not see the id associated with the socst score.  Since id is the first variable, we can use the repeat(1) option and the first variable will be repeated for every block of data.
listblck in 1/5, repeat(1)
	
     +---------------------------------------------------+
     |  id   female    race      ses   schtyp       prog |
     |---------------------------------------------------|
  1. |  70     male   white      low   public    general |
  2. | 121   female   white   middle   public   vocation |
  3. |  86     male   white     high   public    general |
  4. | 141     male   white     high   public   vocation |
  5. | 172     male   white   middle   public   academic |
     +---------------------------------------------------+

     +---------------------------------------------+
     |  id   read   write   math   science   socst |
     |---------------------------------------------|
  1. |  70     57      52     41        47      57 |
  2. | 121     68      59     53        63      61 |
  3. |  86     44      33     54        58      31 |
  4. | 141     63      44     47        53      56 |
  5. | 172     47      52     57        53      61 |
     +---------------------------------------------+
If you wanted a different variable first, you can use the order command to tell Stata what variable comes first.  Suppose you wanted gender to come first, and you wanted gender repeated on every line. You could use the order command and then the repeat(1) option, as illustrated below.
order female
listblck in 1/5, repeat(1)

     +---------------------------------------------------+
     | female    id    race      ses   schtyp       prog |
     |---------------------------------------------------|
  1. |   male    70   white      low   public    general |
  2. | female   121   white   middle   public   vocation |
  3. |   male    86   white     high   public    general |
  4. |   male   141   white     high   public   vocation |
  5. |   male   172   white   middle   public   academic |
     +---------------------------------------------------+

     +------------------------------------------------+
     | female   read   write   math   science   socst |
     |------------------------------------------------|
  1. |   male     57      52     41        47      57 |
  2. | female     68      59     53        63      61 |
  3. |   male     44      33     54        58      31 |
  4. |   male     63      44     47        53      56 |
  5. |   male     47      52     57        53      61 |
     +------------------------------------------------+
You can use the order id command, and id is the first variable again.
order id
listblck in 1/5, repeat(1)


     +---------------------------------------------------+
     |  id   female    race      ses   schtyp       prog |
     |---------------------------------------------------|
  1. |  70     male   white      low   public    general |
  2. | 121   female   white   middle   public   vocation |
  3. |  86     male   white     high   public    general |
  4. | 141     male   white     high   public   vocation |
  5. | 172     male   white   middle   public   academic |
     +---------------------------------------------------+

     +---------------------------------------------+
     |  id   read   write   math   science   socst |
     |---------------------------------------------|
  1. |  70     57      52     41        47      57 |
  2. | 121     68      59     53        63      61 |
  3. |  86     44      33     54        58      31 |
  4. | 141     63      44     47        53      56 |
  5. | 172     47      52     57        53      61 |
     +---------------------------------------------+

For more information

For more information, see the help or reference manual about the list command and the order command.

How to cite this page

Report an error on this page or leave a comment

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.