Announcement

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

  • How can I write a multiline code with %stata in python?

    Suppose I write the following code in python:
    Code:
    %loading pystata as usual
    import stata_setup
    stata_setup.config("C:/Program Files/Stata17", "mp")
    from pystata import stata
    from sfi import Macro
    
    for ii in [1,2]:
     Macro.setLocal('kk', str(ii))
     %stata forvalues jj=1/`kk' {
     %stata disp `jj'
     %stata }
    But this obviously does not work as the %stata command is designed to be a one-line. What is the workaround here?
    (The example is just intended to be an MWE. The actual code (in python) is more than a thousand lines and should be able to run with one click and no more interaction.)

  • #2
    You need magic
    %%stata
    multiple Stata code lines

    Comment


    • #3
      You need magic
      %%stata
      multiple Stata code lines

      Comment


      • #4
        Originally posted by FernandoRios View Post
        You need magic
        %%stata
        multiple Stata code lines
        No. Magic %% does not work with other python commands I've tried that already. For example, this does not work:
        Code:
         %loading pystata as usual
        import stata_setup
        stata_setup.config("C:/Program Files/Stata17", "mp")
        from pystata import stata
        from sfi import Macro  
        
        for ii in [1,2]:  
         Macro.setLocal('kk', str(ii))
         %%stata  
         forvalues jj=1/`kk'
         {  
         disp `jj'
         }
        Last edited by John Williamss; 19 Jan 2023, 19:27.

        Comment


        • #5
          Originally posted by John Williamss View Post

          No. Magic %% does not work with other python commands I've tried that already. For example, this does not work:
          Code:
          %loading pystata as usual
          import stata_setup
          stata_setup.config("C:/Program Files/Stata17", "mp")
          from pystata import stata
          from sfi import Macro
          
          for ii in [1,2]:
          Macro.setLocal('kk', str(ii))
          %%stata
          forvalues jj=1/`kk'
          {
          disp `jj'
          }
          Hi John Williams,

          You are right. Cell magic %% will not run within a Python loop. Alternatively, you can use the run() function within the stata module of pystata to run a block of code as follows:

          Code:
          from pystata import stata
          from sfi import Macro
          Then run

          Code:
          for ii in [1,2]:
              Macro.setLocal('kk', str(ii))
              stata.run('''
                  forvalues jj=1/`kk' {
                      disp `jj'
                  }
              ''')
          The output is:

          Code:
          . 
          .         forvalues jj=1/`kk' {
            2.             disp `jj'
            3.         }
          1
          
          .     
          . 
          
          . 
          .         forvalues jj=1/`kk' {
            2.             disp `jj'
            3.         }
          1
          2
          In this way, you can mix a block of Python statements and a block of Stata code together.

          Comment


          • #6
            Originally posted by Zhao Xu (StataCorp) View Post

            Hi John Williams,

            You are right. Cell magic %% will not run within a Python loop. Alternatively, you can use the run() function within the stata module of pystata to run a block of code as follows:

            Code:
            from pystata import stata
            from sfi import Macro
            Then run

            Code:
            for ii in [1,2]:
            Macro.setLocal('kk', str(ii))
            stata.run('''
            forvalues jj=1/`kk' {
            disp `jj'
            }
            ''')
            The output is:

            Code:
            .
            . forvalues jj=1/`kk' {
            2. disp `jj'
            3. }
            1
            
            .
            .
            
            .
            . forvalues jj=1/`kk' {
            2. disp `jj'
            3. }
            1
            2
            In this way, you can mix a block of Python statements and a block of Stata code together.
            Thanks!

            Comment

            Working...
            X