Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • list first 10 rows where something is true

    Suppose I want to see the first 10 rows where something is true. The following command won't do that for me:

    list if /* something */ in 1/10

    Instead that command lists rows where something is true, if they also happen to be in the first 10 rows of the data. Often there are no rows that meet those conditions.

    What should I do to see the first 10 rows where something is true?


  • #2
    See

    usefully: https://www.statalist.org/forums/for...nt-and-tabmiss (especially #7)

    historically: https://www.stata.com/statalist/arch.../msg00448.html

    Comment


    • #3
      You can use listsome from SSC to do this. You can even list a random sample of observations that meet the condition:

      Code:
      sysuse nlsw88.dta, clear
      listsome idcode-married if age == 37, max(5)
      listsome idcode-married if age == 37, max(5) random
      and the results:
      Code:
      . listsome idcode-married if age == 37, max(5)
      
            +--------------------------------+
            | idcode   age    race   married |
            |--------------------------------|
         1. |      1    37   black    single |
         2. |      2    37   black    single |
         7. |      9    37   white    single |
        20. |     36    37   white    single |
        64. |    143    37   white   married |
            +--------------------------------+
      
      . listsome idcode-married if age == 37, max(5) random
      
            +--------------------------------+
            | idcode   age    race   married |
            |--------------------------------|
       792. |   1880    37   white   married |
       875. |   2059    37   white   married |
       887. |   2088    37   white   married |
      1496. |   3434    37   black   married |
      1868. |   4309    37   black   married |
            +--------------------------------+
      
      .

      Comment


      • #4
        Let me start by writing that I've posted what follows not with any belief that it's an improvement over listsome since it certainly is not. I just found the trick elegant and amusing, and not one I'd seen before, including in Nick's references on earlier discussions of this problem.

        Code:
        . sysuse auto, clear
        (1978 Automobile Data)
        
        . list make rep78 if rep78==5 & sum(rep78==5)<=5
        
             +----------------------+
             | make           rep78 |
             |----------------------|
         20. | Dodge Colt         5 |
         43. | Plym. Champ        5 |
         53. | Audi 5000          5 |
         57. | Datsun 210         5 |
         61. | Honda Accord       5 |
             +----------------------+
        Added in edit: Should have reproduced Robert's first example for comparability.
        Code:
        . sysuse nlsw88.dta, clear
        (NLSW, 1988 extract)
        
        . list idcode-married if age == 37 & sum(age==37)<=5
        
              +--------------------------------+
              | idcode   age    race   married |
              |--------------------------------|
           1. |      1    37   black    single |
           2. |      2    37   black    single |
           7. |      9    37   white    single |
          20. |     36    37   white    single |
          64. |    143    37   white   married |
              +--------------------------------+
        Last edited by William Lisowski; 07 Mar 2018, 11:38.

        Comment


        • #5
          #4 William Lisowski That;s a nice one-liner to avoid a new command.

          For a similar trick in the context of panel data see https://www.stata.com/support/faqs/d...t-occurrences/

          Comment

          Working...
          X