Announcement

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

  • Simple: postfile with multiple variables

    I'm simply trying to save some characteristics of variables into a new file. A generalized version of my code is below:

    Code:
    capture postutil clear
    postfile table z x y using "file.dta", replace
    
    forvalues i = 1/5 {
            sum x y if z == `i'
            post table (`i') (`r(mean)') (`r(mean)')
    }
    postclose table

    The problem is that only the characteristics for variable y are shown. I could work around this by creating and merging two different files for each variable, but I'm sure there's a better way.
    Thanks in advance!

  • #2
    Hi Adam,

    Here's an example that works with the auto dataset. It's a tad more verbose than your code, but does the job.

    Code:
    sysuse auto, clear
    gen z = floor(runiform(1,6))
    capture postutil clear
    postfile table z mean str8 var using "file.dta", replace
    
    forvalues i = 1/5 {
            sum price if z == `i'
            local var "price"
            post table (`i') (`r(mean)') ("`var'")
            sum foreign if z == `i'
            local var "foreign"
            post table (`i') (`r(mean)') ("`var'")
    }
    postclose table
    
    use "file.dta", clear
    
    encode var, gen(varnum)
    drop var
    reshape wide mean, i(z) j(varnum)
    rename (mean1 mean2) (price foreign)

    Comment


    • #3
      When summarize is invoked with multiple variables, as you have found, it only stores the results for the last of those variables in r(). So you need to do separate summarize commands for x and y. This, in turn, requires you to temporarily store the results for x in a local macro while you are busy getting the results for y. Then you can post it all at once. So, like this:

      Code:
      capture postutil clear
      postfile table z x y using "file.dta", replace
      
      forvalues i = 1/5 {
              sum x if z == `i'
              local topost (`i') (`r(mean)')
              sum y if z == `i'
              post table `topost' (`r(mean)')
      }
      postclose table
      Added: Crossed with Chris Larkin's response, which shows a different approach.

      Comment


      • #4
        Thank you Chris, this works fine!

        Comment


        • #5
          Thanks, Clyde! This also works and is more relatable to a novice.

          Comment


          • #6
            Adam: have you checked out statsby? Although learning about post and its siblings is great training, statsby might provide a more direct route to doing what you want in some circumstances. Here follows a version of your " creating and merging two different files for each variable":

            Code:
            . sysuse auto, clear
            (1978 Automobile Data)
            
            . gen z = floor(runiform(1,6))
            
            . statsby mean_p = r(mean), by(z) saving(p.dta, replace): sum price
            (running summarize on estimation sample)
            
                  command:  summarize price
                   mean_p:  r(mean)
                       by:  z
            
            Statsby groups
            ----+--- 1 ---+--- 2 ---+--- 3 ---+--- 4 ---+--- 5
            .....
            
            . statsby mean_m = r(mean), by(z) saving(m.dta, replace): sum  mpg
            (running summarize on estimation sample)
            
                  command:  summarize mpg
                   mean_m:  r(mean)
                       by:  z
            
            Statsby groups
            ----+--- 1 ---+--- 2 ---+--- 3 ---+--- 4 ---+--- 5
            .....
            
            . use m, clear
            (statsby: summarize)
            
            . merge 1:1 z using p
            
                Result                           # of obs.
                -----------------------------------------
                not matched                             0
                matched                                 5  (_merge==3)
                -----------------------------------------
            
            . list
            
                 +---------------------------------------+
                 | z     mean_m     mean_p        _merge |
                 |---------------------------------------|
              1. | 1   24.14286     5800.5   matched (3) |
              2. | 2   19.21428   6483.071   matched (3) |
              3. | 3         21   6560.545   matched (3) |
              4. | 4   23.22222   5316.833   matched (3) |
              5. | 5   18.82353   6846.471   matched (3) |
                 +---------------------------------------+

            Comment

            Working...
            X