Announcement

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

  • List a single observation, formatted in a column

    Dear all,

    I would like to list the values of a single observation such that for each variable the observation's value is displayed on its own row. The display format should look something like this:
    v1 "ABC"
    v2 400
    v3 250
    The reason I would like to list a single observation like this is that my panel dataset contains annual balance sheet data for thousands of companies for multiple years. Therefore, I would like to be able to quickly take a look at a certain company's say 2018 balance sheet in a more standard data format than that displayed with official Stata's -list- command or the user written -listblck- command.

    Is there an official or user written command available that allows me to plot single observations like this? Or would it somehow be possible to -browse- certain observations in transposed form?

    Thanks,
    Daniel

  • #2
    This uses sxpose (Cox, SSC), so you may lose precision for non-integer values.

    Code:
    program listxpose
        version 11.2
        syntax [ varlist ] in [ , * ]
        preserve
        quietly {
            keep  `varlist'
            order `varlist'
            keep  `in'
            sxpose , clear force `options'
            tempvar tmp
            generate str32 `tmp' = ""
            order `tmp' , first
            mata : st_sstore(., "`tmp'", tokens(st_local("varlist"))')
        }
        list , nohead noobs
        restore
    end
    Here are examples

    Code:
    . listxpose in 42
      +----------------------------+
      |         make   Plym. Arrow |
      |        price          4647 |
      |          mpg            28 |
      |        rep78             3 |
      |     headroom             2 |
      |----------------------------|
      |        trunk            11 |
      |       weight          3260 |
      |       length           170 |
      |         turn            37 |
      | displacement           156 |
      |----------------------------|
      |   gear_ratio   3.049999952 |
      |      foreign             0 |
      +----------------------------+
    
    . listxpose gear_ratio price foreign make in 42
      +--------------------------+
      | gear_ratio   3.049999952 |
      |      price          4647 |
      |    foreign             0 |
      |       make   Plym. Arrow |
      +--------------------------+
    Best
    Daniel

    Comment


    • #3
      Here's another approach, made into a tiny program:
      Code:
      cap prog drop showobs
      prog showobs
      args obsnum
      foreach v of varlist * {
        display "`v': " `v'[`obsnum']
      }
      end
      //
      // List observation #9
      clear
      sysuse auto
      showobs 9

      Comment


      • #4
        Daniel and Mike,

        Thanks very much for your programs. Both techniques work very well! In the end, I decided to go for a slight variation of Daniel's version as this allowed me slightly more degrees of freedom in terms of listing multiple (up to three) observations. Like this I can display up to three years of a company's balance sheet data.

        Code:
        Code:
        program listr
            version 11.2
            syntax [ varlist ] [if] [in] [ , * ]
            preserve
            quietly {
                marksample touse
                keep `varlist'
                order `varlist'
                capture keep `if' `in'
                capture keep in 1/3
                sxpose , clear force `options'
                tempvar tmp
                generate str32 `tmp' = ""
                order `tmp' , first
                mata : st_sstore(., "`tmp'", tokens(st_local("varlist"))')
            }
            list , nohead noobs
            restore
        end
        Result:
        Code:
        . sysuse auto, clear
        
        . listr if substr(make,1,4)=="Chev"
          +-------------------------------------------------------------+
          |         make   Chev. Chevette   Chev. Impala   Chev. Malibu |
          |        price             3299           5705           4504 |
          |          mpg               29             16             22 |
          |        rep78                3              4              3 |
          |     headroom              2.5              4            3.5 |
          |-------------------------------------------------------------|
          |        trunk                9             20             17 |
          |       weight             2110           3690           3180 |
          |       length              163            212            193 |
          |         turn               34             43             31 |
          | displacement              231            250            200 |
          |-------------------------------------------------------------|
          |   gear_ratio      2.930000067    2.559999943    2.730000019 |
          |      foreign                0              0              0 |
          +-------------------------------------------------------------+
        Again, thanks very much for your help.

        Best,
        Daniel

        Comment


        • #5
          Just a couple of remarks.

          The

          Code:
          marksample touse
          line is superfluous. The following code never refers to the temporary variable touse. Moreover, the code could not even refer to touse because touse is dropped (better yet: not kept) in the very next line. Also, even if the code would refer to touse, observations with missing values would be excluded which is likely not what is wanted.

          Note also that

          Code:
          keep `if' `in'
          keep in 1/3
          is likely to yield unexpected results. The code does not keep the first three observations in the dataset, but the first three observations of the selected subset.

          Best
          Daniel
          ​​​
          Last edited by daniel klein; 19 Oct 2019, 14:59.

          Comment


          • #6
            Thanks, Daniel, for the comments.

            "marksample touse" indeed is superfluous. I forgot to delete it. Thanks for pointing to this. By contrast, I don't think that the "keep ..." commands yield unexpected results. I added row "keep in 1/3" simply in order to make sure that I don't get a row overflow in Stata's results window on my monitor.

            That said, for my use case it would be great if the Stata browser would offer a "transpose" option that allows the user to display variables on rows and observations as columns without the user having to -(s)xpose- the data first.

            Thanks again,
            Daniel

            Comment


            • #7
              Originally posted by Daniel Hoechle View Post
              By contrast, I don't think that the "keep ..." commands yield unexpected results. I added row "keep in 1/3" simply in order to make sure that I don't get a row overflow in Stata's results window on my monitor.
              As long as you are writing this program solely for your personal use that behavior might be fine and the results might be what you expect to see (now). However, I (and perhaps you, too, in a couple of months from now) would be surprised if I typed

              Code:
              listr in 42/73
              and found only three observations listed.

              Originally posted by Daniel Hoechle View Post
              That said, for my use case it would be great if the Stata browser would offer a "transpose" option that allows the user to display variables on rows and observations as columns without the user having to -(s)xpose- the data first.
              Although I have not yet wanted to do this, you could add it to the Wishlist for Stata 17.

              Best
              Daniel

              Comment

              Working...
              X