Announcement

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

  • Exporting multiple graphs to a Word document

    I've written code that will allow me to create quality control charts for each of 10 categories for my entire institution, then again for each of those 10 categories broken down by department (this ends up being a few hundred graphs each time the report is run. I'd like somehow combine these graphs into one Word document as they group logically (each category for the institution, then one for each department), but have hit a wall when I try to code this in Mata. Is anyone aware of a way to automate this process? I have the loops of code posted below for your reference, excluding the attempts to port these into Word. I realize the code might be messy...but it does work as intended.

    Thank you!

    Sam

    --

    foreach c in `category' {
    local catlab : label category `c'
    sum yvar if category==`c' & monthyear<`intdate'
    gen preaverage=r(mean)
    gen presd=preaverage+r(sd)
    gen pretwosd=preaverage+(2*r(sd))
    gen preminsd=preaverage+(-1*r(sd))
    gen premintwosd=preaverage+(-2*r(sd))
    sum yvar if category==`c' & monthyear>=`intdate'
    gen postaverage=r(mean)
    gen postsd=postaverage+r(sd)
    gen posttwosd=postaverage+(2*r(sd))
    gen postminsd=postaverage+(-1*r(sd))
    gen postmintwosd=postaverage+(-2*r(sd))
    twoway (scatter yvar monthyear if category==`c'), ///
    title("`catlab' utilization") ///
    ytitle("YVar") ///
    xtitle("Month / Year") ///
    subtitle("`reportperiod'") ///
    xlabel(#6, angle(forty_five)) ///
    xmtick(##6) ///
    legend(off) || ///
    (line preaverage monthyear if monthyear<`intdate') || ///
    (line presd monthyear if monthyear<`intdate', lcolor(black) lpattern(dash)) || ///
    (line pretwosd monthyear if monthyear<`intdate', lcolor(black)) || ///
    (line preminsd monthyear if monthyear<`intdate', lcolor(black) lpattern(dash)) || ///
    (line premintwosd monthyear if monthyear<`intdate', lcolor(black)) ///
    (line postaverage monthyear if monthyear>=`intdate') || ///
    (line postsd monthyear if monthyear>=`intdate', lcolor(black) lpattern(dash)) || ///
    (line posttwosd monthyear if monthyear>=`intdate', lcolor(black)) || ///
    (line postminsd monthyear if monthyear>=`intdate', lcolor(black) lpattern(dash)) || ///
    (line postmintwosd monthyear if monthyear>=`intdate', lcolor(black))
    drop preaverage presd pretwosd preminsd premintwosd postaverage postsd posttwosd postminsd postmintwosd
    graph save Graph
    }

    foreach s in `dept' {
    local deptlab : label service `s'
    foreach c in `category' {
    local catlab : label category `c'
    local catlab : label category `c'
    sum yvar if category==`c' & service==`s' & monthyear<`intdate'
    gen preaverage=r(mean)
    gen presd=preaverage+r(sd)
    gen pretwosd=preaverage+(2*r(sd))
    gen preminsd=preaverage+(-1*r(sd))
    gen premintwosd=preaverage+(-2*r(sd))
    sum yvar if category==`c' & service==`s' & monthyear>=`intdate'
    gen postaverage=r(mean)
    gen postsd=postaverage+r(sd)
    gen posttwosd=postaverage+(2*r(sd))
    gen postminsd=postaverage+(-1*r(sd))
    gen postmintwosd=postaverage+(-2*r(sd))
    twoway (scatter yvar monthyear if category==`c' & service==`s'), ///
    title("`deptlab' `catlab' utilization") ///
    ytitle("YVar Patient Days") ///
    xtitle("Month / Year") ///
    subtitle("`reportperiod'") ///
    xlabel(#6, angle(forty_five)) ///
    xmtick(##6) ///
    legend(off) || ///
    (line preaverage monthyear if monthyear<`intdate') || ///
    (line presd monthyear if monthyear<`intdate', lcolor(black) lpattern(dash)) || ///
    (line pretwosd monthyear if monthyear<`intdate', lcolor(black)) || ///
    (line preminsd monthyear if monthyear<`intdate', lcolor(black) lpattern(dash)) || ///
    (line premintwosd monthyear if monthyear<`intdate', lcolor(black)) ///
    (line postaverage monthyear if monthyear>=`intdate') || ///
    (line postsd monthyear if monthyear>=`intdate', lcolor(black) lpattern(dash)) || ///
    (line posttwosd monthyear if monthyear>=`intdate', lcolor(black)) || ///
    (line postminsd monthyear if monthyear>=`intdate', lcolor(black) lpattern(dash)) || ///
    (line postmintwosd monthyear if monthyear>=`intdate', lcolor(black))
    drop preaverage presd pretwosd preminsd premintwosd postaverage postsd posttwosd postminsd postmintwosd
    graph save Graph
    }
    }
    Last edited by Samuel Aitken; 14 Apr 2016, 15:35.

  • #2
    I didn't try to work through your code but believe you want graph export, not graph save, unless the "porting into Word" part that you didn't show includes conversion to a format that Word can read.

    That said, I would recommend exporting the graphs with names that can be sorted and that reflect the desired order (e.g., graph001.png, graph002.png, graph003.png, ...). These graphs can then be imported into Word and should appear in the same order.

    I don't understand why you want to code this in Mata.

    Comment


    • #3
      Thanks for the reply - the piece of code I posted didn't have Graph Export listed, but I have been exporting them according to standard naming conventions based on the category/department and exporting as PNGs.

      I'm actually happy to not do this in Mata - I just don't know of a different way to get multiple graphs show up in the same Word document using Stata. In other words, I'd like the first bit of code to export 10 graphs (one for each category) to the same Word document, then the second bit of code to export one Word document for each department. That is where I don't know how to begin.

      Comment


      • #4
        I can think of several options (I imagine the 2nd is what you are after):

        1) use pdf: export the graphs to pdf and then access your pdf program from within Stata to combine the pdfs in the desired order (I've done this before, but cannot remember which pdf program I used--It was not Acrobat)

        2) use rtf: Use a user written utility to embed an exported graph into an RTF document that can be opened by Stata. Roger Newson's rtfutil package is available via ssc install rtfutil. The help file for rtfinsert provides an example for embedding a graph in such a document.

        3) use html: As is the case for rtf, there are a number of user contributed programs to export to html and then opened in Word. I don't happen to know any, but a quick search should yield many results.

        4) use (La)Tex: Likewise, there are a number of user programs that allow you to write and ultimately compile TeX code that would allow for embedded graphs in a pdf.
        Stata/MP 14.1 (64-bit x86-64)
        Revision 19 May 2016
        Win 8.1

        Comment


        • #5
          Great, thank you! Those sound to be what I'm looking for. I'll take a look at the RTF option and see if that can get these in a more user-friendly format.

          Comment

          Working...
          X