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]

st: how to automate sorting and how to automate extracting info from a sort

From   Anjanette Chan Tack <>
Subject   st: how to automate sorting and how to automate extracting info from a sort
Date   Wed, 22 Dec 2010 12:21:28 -0600 (CST)

Hi Statalist --

I was wondering if you large dataset manipulation masters could advise me on a smart way to do the following:

Imagine you have a very large matrix, with 865 columns and thousands of rows. What you want from this matrix is to get the second smallest numeric value of all the rows within a column, for each column. Ultimately, you want to create a table 865 by 2 table, where for each column, you have that one value (which is, again, the second smallest numeric value of all the rows in that column). 

To do this in stata the slow way (which is the only way I know), you could start with the first column, sort the values of the rows, pull out the value of the entry in the second row and record it in the 865 by 2 table you want to build. Then you repeat for the second column, the third, and so on. This is a very repetitive process, and mindless. Is there a way to tell stata, or some other program, to sort each column individually, and to pull out the value of the second row, and to produce a nice table with this information for all columns?

Presently I have 8 datasets that I'd have to do this for. So using the slow way, I'd have to do this little operation 865*8 times. You can imagine that I would be very happy to find a way out of doing this by hand. If you have a smart way to automate this process, your help would save me from a very miserable time.

Let me know if you have any suggestions. I have access to stata, so if you can think of a command to automate this with it would be excellent. If there are other programs that could do this work, and they aren't too hard to learn to use for this task, I'd be willing to try that as well. I can also turn these giant matrices into any format (csv, tab delimited, dbf files). The files generated are too large for excel to display them in full, but I can also have them generated in pieces (e.g. the matrix for one dataset broken into 4 files, for example). This wouldn't affect the sorting, as the sorting goes by column.

Thanks for your help!


Anjanette M. Chan Tack
PhD student 
University of Chicago Department of Sociology
*   For searches and help try:

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