Announcement

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

  • Allowing different numeric formats within table and collect or dtable

    I'd like to know if the new -table-/-collect- system is capable of allowing different numeric formats in the same column? Let's consider the simplest possible example, I want on result with a %6.2f format and one with a %6.1f format. This is a reasonable request, especially for descriptive tables, where variables are measured with more or less precision than each other.

    Here is a minimal attempt, and failure, to produce such a table. I've tried other attempts, but also without success.

    Result:

    Code:
    ---------------------
                    Mean
    ---------------------
    Price         6165.26
    Mileage (mpg)   21.30
    ---------------------
    Code;

    Code:
    clear *
    cls
    
    sysuse auto, clear
    
    collect clear
    
    dtable price, cont(price) nformat(%6.2f mean) name(D1)
    collect layout (var) (result[mean])
    
    dtable mpg, cont(mpg) nformat(%6.1f mean) name(D2)
    collect layout (var) (result[mean])
    
    collect combine Tbl = D1 D2
    
    collect layout (collection[D1 D2]#var) (result[mean])
    collect style header collection, level(hide)
    Last edited by Leonardo Guizzetti; 03 Oct 2024, 22:04.

  • #2
    Just by what you have shown in #1, I can see that you are more proficient in the use of the -collect- command than I am. But I think I can point you in the right direction, and then you can fill in the actual code required.

    Several months back, I posted a superficially similar question. In my case, however, the statistics involved were medians, not means. As it happens, the -statistic()- option of -table- recognizes two ways of specifying medians: median and p50. And it turns out that -nformat()- identifies the statistics it applies to by name. So I was able to achieve what I wanted by designating one of the outputs as a median and the other as p50 and apply different display formats to them in the same table.

    Now, there are no alternate names for mean in the -collect- command. But, I believe, you can create one. Somewhere, though I cannot find it, I recall reading that -collect- (or maybe it's -table-) allows you to define a statistic. The classic example of that is that you can define an interquartile range to be the pair consisting of p25 and p50. I imagine that you can apply this same device to define a statistic mymean equal to the mean. Then you can make one of your outputs a mean, and another a mymean, and then apply separate display formats.

    I wish I could find or remember how you define a new statistic in this framework, but, I can't. But maybe you have also seen this and remember it, or will have better luck finding it than I had.

    I really hope this turns out to be helpful.

    Comment


    • #3
      Thank you, Clyde. I do vaguely recall the post you reference, and thought it was a clever trick back then. I will poke around and see what else I can try following your suggestion. I'll report back my failures or success.

      Comment


      • #4
        You can use collect style cell to target numeric format styles to more than a specific result level; they can be variable-specific too. My code additions are highlighted in blue.
        Code:
        clear *
        cls
        
        sysuse auto, clear
        
        collect clear
        
        dtable price, cont(price) nformat(%6.2f mean) name(D1)
        collect layout (var) (result[mean])
        
        dtable mpg, cont(mpg) nformat(%6.1f mean) name(D2)
        collect layout (var) (result[mean])
        
        collect combine Tbl = D1 D2
        
        collect style cell result[mean]#var[price], nformat(%6.2f)
        collect style cell result[mean]#var[mpg], nformat(%6.1f)
        
        collect layout (collection[D1 D2]#var) (result[mean])
        collect style header collection, level(hide)
        Here is the resulting table.
        Code:
        -----------------------
                          Mean 
        -----------------------
        D1                     
          Price         6165.26
        D2                     
          Mileage (mpg)    21.3
        -----------------------

        Comment


        • #5
          Thanks, Jeff Pitblado (StataCorp). I knew there was a way to do it, and I hadn't thought of this way yet. I appreciate your help.

          Comment


          • #6
            Playing around with Jeff's solution more, it's easy to see I can simplify that code a lot. I was originally working under the impression that 2 collections would somehow be needed to specify different numerical formats, and Jeff showed in #4 how this is not so. For a simple table like my toy example, there's no need to combine two collections. The simplified code is below.

            Code:
            collect clear
            
            dtable price mpg, cont(price mpg)
            
            collect style cell result[mean]#var[price], nformat(%6.2f)
            collect style cell result[mean]#var[mpg], nformat(%6.1f)
            
            collect layout (var) (result[mean])
            collect preview

            Comment

            Working...
            X