Announcement

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

  • Using Stata automation for external do-file editor

    I asked a question in this thread, but I figure it is probably better to ask the question in its own thread.

    I am running Stata on a Windows machine and want to use an external editor (in my case Sublime Text 3) instead of Stata's native do-file editor. I therefore want to use Stata automation to send commands to Stata from my editor. To do so, I tried to write a simple VBScript (which I can then call from ST3) in the following way:

    Code:
    dim stataobj
    set stataobj=CreateObject("stata.StataOLEApp")
    stataobj.DoCommand("do some_dofile.do")
    The script makes Stata run the given do-file. However, as soon as the script is finished, Stata closes. My question is therefore if there is any way to execute a VBScript that supplies commands to Stata but where Stata is kept open after the script is finished. I have read the instructions at http://www.stata.com/automation/ but have not been able to find a solution and would be grateful for any help on how to solve this problem.
    Last edited by Mattias Nordin; 27 Aug 2014, 09:25.

  • #2
    This is for my knowledge please, why are you using Sublime Text3, how it is better than Stata do file ediotr?
    Regards
    --------------------------------------------------
    Attaullah Shah, PhD.
    Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
    FinTechProfessor.com
    https://asdocx.com
    Check out my asdoc program, which sends outputs to MS Word.
    For more flexibility, consider using asdocx which can send Stata outputs to MS Word, Excel, LaTeX, or HTML.

    Comment


    • #3
      Originally posted by Attaullah Shah View Post
      This is for my knowledge please, why are you using Sublime Text3, how it is better than Stata do file ediotr?

      Sublime Text offers great syntax highlighting and is great for writing your own shorthands for commands you use often. Plus, I already use it in other instances such as when writing LaTeX code.

      Comment


      • #4
        Mattias, it will not work the way you wrote it because the lifespan of Stata's object will be the same as the lifetime of your script. While for the task you need it should be the lifetime of Sublime (never heard of it). What you discovered is that VB will clear unused objects periodically (Garbage Collection) and is true for many languages, and is definitely a problem in your case. Even if the object was not cleared, every time you run your script it would create a new instance of Stata object, which is even worse than what you had with starting Stata from command line.

        Probably possible if Sublime is also OLE automate-able, then create a wrapper context and two objects: one for Stata and one for Sublime, then establish communication between them.

        Stata's editor is good and is going to be even better in the next version (anticipation). Insertion of templates is an easy thing to do and supported by Scintilla, on which the Stata's doeditor is based. So should be a breathe. (if that is implemented, I'd like a web-accessible storage for my templates, to avoid synchronization problems when working on 10different computers).

        Best, Sergiy Radyakin.

        Comment


        • #5
          Perhaps you already aware of this approach, but in case you're not: One popular low-tech way to run Stata commands from an external editor under Windows goes like this:

          1) Create a macro (Window sense of macro, not Stata sense) in the editor that will save a highlighted portion of text to some scratch file, e.g. "c:\temp\__myscratchfile.do"

          2) In your Stata profile.do file, define a function key that will run this scratch file:
          Code:
          capture erase   "c:\temp\__myscratchfile.do"    /* clear out the leftover to be safe    */
          global F8 "do   "c:\temp\__myscratchfile.do;"
          Thus, your routine becomes a )highlight the commands of interest in the file resident in your editor and invoke macro via some keyboard shortcut; b) alt-tab (or whatever) to Stata; c) hit F8

          Although this might sound cumbersome, I find it quite efficient.

          Regards, Mike

          Comment


          • #6
            Mattias, I think the package Stata Enhanced for Sublime will do what you want.

            https://sublime.wbond.net/packages/Stata%20Enhanced

            Regards,
            Guest
            Last edited by sladmin; 11 Dec 2017, 09:33. Reason: anonymize poster

            Comment


            • #7
              Thanks for your responses! I am currently working on using Python and the win32com package to access Stata. It seems like I in that way can keep the connection to Stata even after I have run the do-file.

              Guest: I'm aware of Stata Enhanced, unfortunately that package doesn't use automation which I would like to do.
              Last edited by sladmin; 11 Dec 2017, 09:33. Reason: anonymize poster

              Comment

              Working...
              X