Announcement

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

  • List macros from summarize in loop to a matrix

    Hi All,
    I am trying to list results from a sumarise command under loop. The below code works fine and display the results stored in global macro. However, I would like to have them listed as one table (like a matrix for, example, instead of displaying them one by one. How can I do this? I like my final table/matrix to be with columns for the local macros 'hr' and 'hrpop' and rows for var1, var2 and var3. How can I incorporate it in the code below? Thanks.

    Code:
    local wt "mult"
    local z "PL"
    tempvar ind
    local i=0
    local subtr "var1 var2 var3"
    foreach X of local subtr{
        qui gen dum`i' = 0
        qui replace dum`i' = 1 if `X'<=`z'
        qui sum dum`i' [fw=`wt']
        local hr = r(mean)
        local pop= r(N)
        local hrpop=`hr'*`pop'
        global C`i' = `hr'
        global C`i'_n=`hrpop'
        drop dum`i'
        local ++i
    }
    display "C0 = " %9.4f $C0
    display "Number = " %9.0f $C0_n
    display "C1 = " %9.4f $C1
    display "Number = " %9.0f $C1_n
    display "C2 = " %9.4f $C2
    display "Number = " %9.0f $C2_n
    Last edited by Rijo John; 04 Nov 2018, 20:37.

  • #2
    Your code is much more complicated than it needs to be to do this. It creates unnecessary variables and local macros, and it uses the dreaded global macros when there is no reason to resort to this dangerous coding practice.

    Code:
    local subtr var1 var2 var3
    local nvars: word count `subtr'
    matrix M = J(3, `nvars', .)
    forvalues i = 1/`nvars' {
        local X: word `i' of `subtr'
        qui gen dum = (`X'<= PL)
        qui sum dum [fw=mult]
        matrix M[1, `i'] = r(mean)
        matrix M[2, `i'] = r(N)
        matrix M[3, `i'] = r(mean)*r(N)
        drop dum
    }
    matrix rownames M = hr pop hrXpop
    matrix colnames M = `subtr'
    
    matrix list M

    Comment


    • #3
      Thank you Clyde. It works perfect. I knew there must be a much neater solution. Thanks for helping out.
      Is there a way to specify the number format for the cells in the matrix M? I want them to be in the format %9.0f for r(mean)*r(N) and %9.4f for r(mean).

      Originally posted by Clyde Schechter View Post
      Your code is much more complicated than it needs to be to do this. It creates unnecessary variables and local macros, and it uses the dreaded global macros when there is no reason to resort to this dangerous coding practice.

      Code:
      local subtr var1 var2 var3
      local nvars: word count `subtr'
      matrix M = J(3, `nvars', .)
      forvalues i = 1/`nvars' {
      local X: word `i' of `subtr'
      qui gen dum = (`X'<= PL)
      qui sum dum [fw=mult]
      matrix M[1, `i'] = r(mean)
      matrix M[2, `i'] = r(N)
      matrix M[3, `i'] = r(mean)*r(N)
      drop dum
      }
      matrix rownames M = hr pop hrXpop
      matrix colnames M = `subtr'
      
      matrix list M

      Comment


      • #4
        Nevermind. I see that it can be done with -matlist-
        Thanks.

        Comment


        • #5
          Note that r(sum) is documented as a saved result of summarize (whose meanonly option may also be used here).

          Comment


          • #6
            Originally posted by Nick Cox
            Note that r(sum) is documented as a saved result of summarize (whose meanonly option may also be used here).
            Ah, great! That means one step less. Thanks Nick.

            Comment

            Working...
            X