automationdocumentation-generationoffice-automationcode-statisticsstata

Automation for Generating Reports


We are using Stata to combine and analyze data for all of our agencies in a district each month. I'd like to somehow create reports of the data analysis automatically for these monthly reports. The report includes a summary table of the reported indicators, a couple of graphics of the key indicators, and an analysis table showing statistically significant differences in the data groups. I'd like these to be in pdf and automatically emailed out to the agencies. Any ideas on software I can use to automate this?


Solution

  • Since you're using Stata to do the analysis, you can let it do the heavy lifting of the report automation as well.

    The trick is using a Stata package like -rtfutil- to export the tables and graphics you describe to a single document. At that point you'll need to convert that to pdf before emailing it.

    Here some sample code for using -rtfutil- automate the creation of a document including a table and two graphics (plus some paragraphs of text) in a RTF document(using the system dataset "auto.dta" as an example):

        ******
    
        clear
    
        //RTF UTILITY FOR INSERTING GRAPHICS & TABLES//
    
        local sf "/users/ppri/desktop/"
    
                //SETUP
                 sysuse auto, clear
                 twoway scatter mpg price,  mlabel(make) || lfitci mpg price
                 graph export "`sf'myplot1.eps", replace
                 twoway scatter price mpg,  mlabel(make) by(for)
                 graph export "`sf'myplot2.eps", replace
    
                 **
                 tempname handle1
    
                //RTFUTIL
                 rtfopen `handle1' using "`sf'mydoc1.rtf", replace
                 file write `handle1' _n  _tab  "{\pard\b SAMPLE DOCUMENT \par}" _tab  _n 
                 file write `handle1' _n "{\line}" 
                 // Figure1
                 file write `handle1' "{\pard\b FIGURE 1:  Plot of Price\par}" _n
                 rtflink `handle1' using "`sf'myplot1.eps"
                 // Figure2
                 file write `handle1' _n "{\page}" _n /*
    */ "{\pard Here is the plot and a paragraph about it.  Here is the plot and a paragraph about it.  Here is the plot and a paragraph about it.  Here is the plot and a paragraph about it.....blah blah blah  blah blah   \line}" _n
                 file write `handle1' _n "{\line}" 
                 file write `handle1' "{\pard\b FIGURE2:  Plots of Price vs. MPG\par}" _n
                 rtflink `handle1' using "`sf'myplot2.eps"
                 //  Table Title
                 file write `handle1' _n "{\page}" _n
                 file write `handle1' _n "{\par\pard}" _n /*
    */   "{\par\pard  HERE IS A TABLE WITH THE CARS:  \par\pard}" _n
                 file write `handle1' _n "{\par\pard}" _n
    
    
                 // Summary Table
                          rtfrstyle make mpg weight, cwidths(2000 1440 1440) local(b d e)
                          listtex make foreign mpg if mpg<15, /*
                    */   handle(`handle1') begin("`b'") delim("`d'") end("`e'") /*
                    */  head("`b'\ql{\i Make}`d'\qr{\i Foreign}`d'\qr{\i MPG }`e'")
                          file write `handle1' _n "{\line}"  
                          file write `handle1' _n _tab(2)  /*
                   */ "{\pard\b Sources:  Census Data, etc...  \par}" _n _n
                        **
                 rtfclose `handle1'
    
        ******
    

    This will put all the elements you asked about into a RTF document (be careful with any issues with wrapping of this code when copy/paste it from the webpage).
    In your question, you also mentioned wanting to create a PDF during this process. Here you'll need to go to use some non-Stata solution. If you're using Mac OSX you can use the Terminal -convert- utility or automator to do this, or here are some other solutions: http://codesnippets.joyent.com/posts/show/1601
    I don't use windows so I'm not sure about solutions with that OS. Good luck.