Announcement

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

  • Frames with collapse

    I'm new to v19 but used Stata Collapse for years (and also SAS proc something output to create a new separate dataset with the summary values).
    I know Frames will do this (very excited), but I can't find the perfect self-help training tutorial or vide.

    Q1) Can anyone recommend a frames tutorial or video with examples like this (similar to proc freq output=..., or collapse to a new dataset).
    Q2) If anyone is feeling generous, would you offer a simple code example of using collapse to create a summary dataset, but then keep the original data in memory and the summary set in a new frame?
    I'll 'recognize' the code when I see it and be very grateful.
    Sue

  • #2
    To reproduce the output of summarize, you will need to loop over the variables. Temporary files (tempfiles) existed before frames, so you can use them to store the output of each collapse command and then transfer the results to a frame. There may be a more efficient way to do this, however. The illustration uses the auto dataset.

    Code:
    sysuse auto, clear
    *START HERE AFTER LOADING DATA
    cap frame drop stats
    tempfile hold
    save `hold', emptyok
    frame put *, into(stats)
    frame stats{
        ds, has(type numeric)
        foreach var in `r(varlist)'{
            preserve
            collapse (count) Obs= `var' (mean) Mean= `var' (sd) SD=`var' (min)  Min=`var' (max) Max= `var'
            gen Variable = "`var'"
            append using `hold'
            save `hold', replace
            restore
        }
    }
    frame stats{
        append using `hold'
        keep Obs-Variable
        keep if !missing(Obs)
        order Variable
    }
    summarize
    frame stats: list, sep(0)
    Res.:

    Code:
    . summarize
    
        Variable |        Obs        Mean    Std. dev.       Min        Max
    -------------+---------------------------------------------------------
            make |          0
           price |         74    6165.257    2949.496       3291      15906
             mpg |         74     21.2973    5.785503         12         41
           rep78 |         69    3.405797    .9899323          1          5
        headroom |         74    2.993243    .8459948        1.5          5
    -------------+---------------------------------------------------------
           trunk |         74    13.75676    4.277404          5         23
          weight |         74    3019.459    777.1936       1760       4840
          length |         74    187.9324    22.26634        142        233
            turn |         74    39.64865    4.399354         31         51
    displacement |         74    197.2973    91.83722         79        425
    -------------+---------------------------------------------------------
      gear_ratio |         74    3.014865    .4562871       2.19       3.89
         foreign |         74    .2972973    .4601885          0          1
    
    . 
    . frame stats: list, sep(0)
    
         +-------------------------------------------------------+
         |     Variable   Obs      Mean        SD    Min     Max |
         |-------------------------------------------------------|
      1. |      foreign    74   .297297   .460188      0       1 |
      2. |   gear_ratio    74   3.01486   .456287   2.19    3.89 |
      3. | displacement    74   197.297   91.8372     79     425 |
      4. |         turn    74   39.6486   4.39935     31      51 |
      5. |       length    74   187.932   22.2663    142     233 |
      6. |       weight    74   3019.46   777.194   1760    4840 |
      7. |        trunk    74   13.7568    4.2774      5      23 |
      8. |     headroom    74   2.99324   .845995    1.5       5 |
      9. |        rep78    69    3.4058   .989932      1       5 |
     10. |          mpg    74   21.2973    5.7855     12      41 |
     11. |        price    74   6165.26    2949.5   3291   15906 |
         +-------------------------------------------------------+

    Comment

    Working...
    X