Announcement

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

  • Using dtable or collect to add a column to a table containing the difference between two other columns

    Hello everyone,

    I'm new to working with the commands dtable and collect, and I was wondering, if there was a way to add a column containing the difference of two other columns.

    To be more specific, I look at the shares of the total population in comparison to a subgroup as in the example below. In the next step, I want to calculate the differences in the percentages for every row. Is there a way to do this?


    Code:
    clear all
    sysuse auto, clear
    
    // generating second factor variable
    generate consumption = 0
    replace consumption = 1 if mpg > 21
    
    dtable i.foreign, by(consumption) sample(, statistic(frequency percent))         ///
        sformat("%s" percent fvpercent)
    
    
    * put each statistic in a unique column
    collect composite define column1 = frequency fvfrequency
    collect composite define column2 = percent fvpercent
    collect style autolevels result column1 column2, clear
    
    collect query autolevels consumption
    * reset the autolevels of the -by()- variable, putting .m;
    collect style autolevels consumption .m `s(levels)', clear
    
    
    collect style cell var[i.foreign], ///
        border(, width(1)) font(, size(7))
    collect label levels consumption 0 "Lower" 1 "Higher"
    
    
    collect layout (var[i.foreign]) (consumption[.m 1]#result)


Working...
X