Announcement

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

  • SFIToolkit.execute and multiple lines commands

    Regardless if using Python or Java, I can easily do SFIToolkit.ExecuteCommand("di 1+1",0) or SFIToolkit.stata("di 1+1",0) and either one works perfectly.
    But how would that work with multiline inputs? I've tried various combinations, i.e.:

    doesn't work:
    Code:
    SFIToolkit.stata("forvalues i=1/100 {",0);
    SFIToolkit.stata("di 1+1",0);
    SFIToolkit.stata("}",0);
    doesn't work:
    Code:
    SFIToolkit.stata("forvalues i=1/100 {\ndi 1+1\n}",0);
    I've tried variations around that, but I couldn't find a way to execute multilines commands with either the Java or Python sdk, maybe the method isn't documented, please help; thank you.

    J.

  • #2
    Regarding your second approach, is your \n is being escaped into a newline by Python and Java? (I'm neither a Python nor Java user.) And if not, I doubt that the SFIToolkit.stata method does so.

    In Stata, I would construct the string to contain newlines with the following
    Code:
    "forvalues i=1/100 {`=char(10)'di 1+1`=char(10)'}"
    to get the hex 0A (decimal 10) newline character substituted into the string, but I don't know how to do that in Python or Java.

    Comment


    • #3
      Hmmm, that's a very good idea, but it didn't work, (in python/java or on the stata "console", at least on linux) neither did this:

      Code:
      "forvalues i=1/100 {`=char(13)'`=char(10)'di 1+1`=char(13)'`=char(10)'}"

      Comment


      • #4
        I've no experience with Stata and Python, so disregard this if it doesn't make sense. If in Python, aren't multiline strings constructed with triple quotes (""")? Perhaps that is needed here.

        Alternatively, you might try placing the continue line operator (///) where linebreaks are intended.

        Comment


        • #5
          I tried triple quotes (in java and python), I also tried #delimit, and I finally tried /// but I suspect the two last options only apply to do-files.
          Neither worked.

          Comment


          • #6
            You could write Stata commands to a tempfile to be run. In the following example running python "interactive" from Stata the local must be escaped, this should not be the case if saving as a python script file to be run via -python script-
            Code:
            python :
            
            from sfi import SFIToolkit
            
            tempfile = SFIToolkit.getTempFile()
            
            stcmd = '''
            forvalues i=1/5 {
                di \`i'
            }
            '''
            f = open(tempfile, "w")
            f.write(stcmd)
            f.close()
            
            stcmd = "do " + tempfile
             
            SFIToolkit.stata(stcmd, True)
            
            end
            Last edited by Bjarte Aagnes; 05 Feb 2022, 14:01.

            Comment


            • #7
              Yes, I was trying to avoid that... because if I have to do that for a lot of stata commands, then it's just spaghetti code... It works if you have *one* isolated case, but it's not something that's going to scale right.
              There's got to be a better way to handle that...
              Last edited by jerome falken; 05 Feb 2022, 14:18.

              Comment


              • #8
                I would think that python code like the following
                Code:
                mystata("""
                    forvalues i=1/100 {
                        di 1+1",0);
                    }
                """)
                where mystata() encapsulates the process of acquiring a temporary file, writing its argument out to that file, and then submitting the file using the SFIToolkit.stata command would not be too spaghetti-like.

                Comment


                • #9
                  For calling Stata from Python with a block of Stata commands, in Stata 17, see [37] in https://www.stata.com/python/pystata...-API-functions

                  Comment


                  • #10
                    Thank you Hua

                    Comment

                    Working...
                    X