How to call Stata and run syntax through R?

I have a strange situation, and please forgive me for not providing a reproducible example for this question. I have over 1000 lines of syntax written for Stata to do some analysis (I wrote this before I started using R). This syntax is used to perform analysis on a quarterly data set every 3 months to generate a report. The analysis results are stored in csv files and are read through R and placed into a Word document using the ReporterS package.

Is there a way to call Stata via R and specify / pipe syntax to run it? (I understand that the reverse situation can be accomplished using rsource(user command) in Stata). I can still manually start Stata and run the syntax there. But is it possible to do this through R? So, a brilliant application / website interface can be created for this part, and the user does not need to do this manually?

+4
source share
1 answer

As @thelatemail shows, the easiest way is to just start Stata in batch mode with a call system.

Here is an example file (called "example.do"):

log using out.log, replace
sysuse auto
regress mpg weight foreign

R- ( Stata , Stata-64 ):

> system("Stata-64 /e do example.do"); readLines("out.log")
 [1] "-----------------------------------------------------------------------------------------------------------------------"
 [2] "      name:  <unnamed>"                                                                                                 
 [3] "       log:  FilePathHere"                                                                     
 [4] "  log type:  text"                                                                                                      
 [5] " opened on:   9 Jan 2015, 13:34:18"                                                                                     
 [6] ""                                                                                                                       
 [7] ". sysuse auto"                                                                                                          
 [8] "(1978 Automobile Data)"                                                                                                 
 [9] ""                                                                                                                       
[10] ". regress mpg weight foreign"                                                                                           
[11] ""                                                                                                                       
[12] "      Source |       SS       df       MS              Number of obs =      74"                                         
[13] "-------------+------------------------------           F(  2,    71) =   69.75"                                         
[14] "       Model |   1619.2877     2  809.643849           Prob > F      =  0.0000"                                         
[15] "    Residual |  824.171761    71   11.608053           R-squared     =  0.6627"                                         
[16] "-------------+------------------------------           Adj R-squared =  0.6532"                                         
[17] "       Total |  2443.45946    73  33.4720474           Root MSE      =  3.4071"                                         
[18] ""                                                                                                                       
[19] "------------------------------------------------------------------------------"                                         
[20] "         mpg |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]"                                         
[21] "-------------+----------------------------------------------------------------"                                         
[22] "      weight |  -.0065879   .0006371   -10.34   0.000    -.0078583   -.0053175"                                         
[23] "     foreign |  -1.650029   1.075994    -1.53   0.130      -3.7955    .4954422"                                         
[24] "       _cons |    41.6797   2.165547    19.25   0.000     37.36172    45.99768"                                         
[25] "------------------------------------------------------------------------------"                                         
[26] ""                                                                                                                       
[27] ". "                                                                                                                     
[28] "end of do-file"                                                                                                         
[29] ""                                                                                                                       
[30] ". exit, clear"                                                                               

, Stata Markup (SMCL), do log using out.log, replace smcl. :

 [1] "{smcl}"                                                                                                                                                                                 
 [2] "{com}{sf}{ul off}{txt}{.-}"                                                                                                                                                             
 [3] "      name:  {res}<unnamed>"                                                                                                                                                            
 [4] "       {txt}log:  {res}FilePathHere"                                                                                                                        
 [5] "  {txt}log type:  {res}smcl"                                                                                                                                                            
 [6] " {txt}opened on:  {res} 9 Jan 2015, 13:41:53"                                                                                                                                           
 [7] "{txt}"                                                                                                                                                                                  
 [8] "{com}. sysuse auto"                                                                                                                                                                     
 [9] "{txt}(1978 Automobile Data)"                                                                                                                                                            
[10] ""                                                                                                                                                                                       
[11] "{com}. regress mpg weight foreign"                                                                                                                                                      
[12] ""                                                                                                                                                                                       
[13] "      {txt}Source {c |}       SS       df       MS              Number of obs ={res}      74"                                                                                           
[14] "{txt}{hline 13}{char +}{hline 30}           F(  2,    71) ={res}   69.75"                                                                                                               
[15] "    {txt}   Model {char |} {res}  1619.2877     2  809.643849           {txt}Prob > F      = {res} 0.0000"                                                                              
[16] "    {txt}Residual {char |} {res} 824.171761    71   11.608053           {txt}R-squared     = {res} 0.6627"                                                                              
[17] "{txt}{hline 13}{char +}{hline 30}           Adj R-squared = {res} 0.6532"                                                                                                               
[18] "    {txt}   Total {char |} {res} 2443.45946    73  33.4720474           {txt}Root MSE      = {res} 3.4071"                                                                              
[19] ""                                                                                                                                                                                       
[20] "{txt}{hline 13}{c TT}{hline 11}{hline 11}{hline 9}{hline 8}{hline 13}{hline 12}"                                                                                                        
[21] "{col 1}         mpg{col 14}{c |}      Coef.{col 26}   Std. Err.{col 38}      t{col 46}   P>|t|{col 54}     [95% Con{col 67}f. Interval]"                                                
[22] "{hline 13}{c +}{hline 11}{hline 11}{hline 9}{hline 8}{hline 13}{hline 12}"                                                                                                              
[23] "{space 6}weight {c |}{col 14}{res}{space 2}-.0065879{col 26}{space 2} .0006371{col 37}{space 1}  -10.34{col 46}{space 3}0.000{col 54}{space 4}-.0078583{col 67}{space 3}-.0053175"      
[24] "{txt}{space 5}foreign {c |}{col 14}{res}{space 2}-1.650029{col 26}{space 2} 1.075994{col 37}{space 1}   -1.53{col 46}{space 3}0.130{col 54}{space 4}  -3.7955{col 67}{space 3} .4954422"
[25] "{txt}{space 7}_cons {c |}{col 14}{res}{space 2}  41.6797{col 26}{space 2} 2.165547{col 37}{space 1}   19.25{col 46}{space 3}0.000{col 54}{space 4} 37.36172{col 67}{space 3} 45.99768"  
[26] "{txt}{hline 13}{c BT}{hline 11}{hline 11}{hline 9}{hline 8}{hline 13}{hline 12}"                                                                                                        
[27] "{res}{txt}"                                                                                                                                                                             
[28] "{com}. "                                                                                                                                                                                
[29] "{txt}end of do-file"
+3

Source: https://habr.com/ru/post/1570091/


All Articles