Announcement

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

  • Mata included in foreach var

    Hi everyone,

    I'm trying to automatize/optimize the code used to create my primary table.
    What I'm trying to do is to make a table for each of my explaining variables according to vaccination status (yes/no) with frequencies and percentages for each category and the combine them in a matrix that I will write to an excel file.

    If I were to do this individually for each variable this would be the code (here with sex as an example):

    Code:
    estpost tab vac sex , nototal
    matrix freq_sex=e(b)'
    matrix perc_sex=e(colpct)'
    
    matrix coljoinbyname freq_perc_sex = freq_sex perc_sex
    
    mata
    freq_perc_sex=st_matrix("freq_perc_sex")
    freq_perc_sex=colshape(freq_perc_sex,4)
    st_matrix("freq_perc_sex", freq_perc_sex)
    end
    
    mat list freq_perc_sex
    This will give me an output exactly as I want it in my table.

    However, I would like to do this in a loop naming each matrix freq_perc_`var'. I can't get it to work though. Any inputs on the following code?:

    Code:
    foreach var of varlist (sex education treatment) {
    estpost tab vac `var' , nototal matrix freq_`var'=e(b)' matrix perc_`var'=e(colpct)' matrix coljoinbyname freq_perc_`var' = freq_`var' perc_`var'
    mata
    freq_perc_`var'=st_matrix("freq_perc"_`var') freq_perc=colshape(freq_perc,4) st_matrix("freq_perc"_`var', freq_perc_`var')
    end mat list freq_perc }
    I'm posting this in the Mata forum, as the latter code will work if I leave out the mata code but then I can't use the colshape.
    How do I implement the ´var' in the st_matrix()? I guess this is where the problem is.

    When running the code I get the following error
    Code:
    11. end
    ---Break---
    r(1);
    end of do-file
    ---Break---
    r(1)
    At the moment I'm unfortunately not able to produce a dataex example. I'm working on a secure server and it will take a while to re-write everything from the dataex output. If needed, I will gladly take the time to do so. Thank you very much for the help.

  • #2
    Updated with a very small dataset using dataex. I was not able to edit the first post. Sorry for that.

    Code:
    * Example generated by -dataex-. For more infor, type help dataex
    clear
    input float(id vac sex education treatment)
    1 0 1 0 0
    2 1 1 1 1
    3 0 0 1 0
    4 0 0 0 1
    5 1 0 0 1
    6 1 1 1 0
    7 0 0 1 1
    8 0 1 0 0
    9 1 0 1 1
    10 1 1 0 0
    end

    Comment


    • #3
      If you want
      • a block of Mata code terminated by end
      to run within
      • a Stata block surrounded by braces {} - typically a program, but in fact within any brace-enclosed block of code
      you need to
      • define your Mata code as a Mata function outside the brace-enclosed block
      • call the Mata function within the brace-enclosed block
      Even though your "end" is meant to terminate a Mata block, at the time Stata is parsing the loop, the fact that the "end" belongs to Mata and not to Stata is not recognized, and causes Stata to react incorrectly.

      With that said, in your particular case you can, I think, avoid this by changing
      Code:
      mata
          freq_perc_`var'=st_matrix("freq_perc"_`var')
          freq_perc=colshape(freq_perc,4)
          st_matrix("freq_perc"_`var', freq_perc_`var')
      end
      to
      Code:
      mata: freq_perc_`var'=st_matrix("freq_perc"_`var')
      mata: freq_perc=colshape(freq_perc,4)
      mata: st_matrix("freq_perc"_`var', freq_perc_`var')
      When I tested this change, I got a series of errors. I think this is what you want.
      Code:
      foreach var of varlist (sex education treatment) {
      estpost tab vac `var' , nototal
      matrix freq_`var'=e(b)'
      matrix perc_`var'=e(colpct)'
      matrix coljoinbyname freq_perc_`var' = freq_`var' perc_`var'
      
      mata: freq_perc_`var'=st_matrix("freq_perc_`var'")
      mata: freq_perc=colshape(freq_perc_`var',4)
      mata: st_matrix("freq_perc_`var'", freq_perc_`var')
      
      mat list freq_perc_`var'
      }

      Comment


      • #4
        Thank you so much, William. The solution you provided worked perfectly.
        It didn't occur to me that that Stata could not differ 'end' and '}'. The 3 x mata: did the job!

        Comment

        Working...
        X