Announcement

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

  • create and call subroutine from the same .do file

    Hi,

    I have a large piece of code that I would like to break down in several smaller pieces of code that I could run (or not) from the top of the .do file. I do not want to break down the main do file in several do files but rather have more control over the part of the code I am running in 1 single do file.

    * Main file:
    ** Call 1st piece of the code: 1) Merging data
    ***** Some code that calls the 1st part "merging data"

    ** Call 2nd piece of the code: 2) regression analysis
    ***** Some code that calls "regression analysis
    etc.


    I have tried the following but I do not get any display. Anyone can point to the right direction/format?Thanks!

    Code:
    clear
    capture program drop main
    program main
        test
    end
    
    capture program drop test
    program test
        di "Hello World"
    end



  • #2
    In the code you show, you have defined two programs but you haven't run either of them.
    Code:
    . main
    Hello World
    
    .

    Comment


    • #3
      Thanks William.When I call run "test" for instance, do I always need to run first the part:
      Code:
       
       capture program drop test program test     di "Hello World" end
      Otherwise, I am getting the following error:
      . test
      last estimates not found

      Is there any way to go around the program? I do not necessarily want to run the "program ... end" first. Is there a way around it? Thanks.

      Comment


      • #4
        Also, I have tried to copy/paste pieces of codes (that actually work without the subroutine structure) inside "test" and I am getting the following error:
        Code:
        main
        command main is unrecognized
        Would you be able to help me understand why? Thanks.

        Comment


        • #5
          Any thought William Lisowski ? Thanks

          Comment


          • #6
            The code example works for me.

            However a problem might be that test is a build in Stata program and there might be clashes in terms of priority.

            Comment


            • #7
              The follow-on problem in posts #3 and #4 were subsequently reposted with the addition of a reproducible example at

              https://www.statalist.org/forums/for...e-in-a-do-file

              which led quickly to a response, as the reproducible example in post #1 led to a quick answer in post #2, and the lack of an example in posts #3 and #4 led to a very slow response in this topic.

              When this problem was reposted, along with the link in the new topic back to this topic, a link forward to the new topic should have been added to this topic.

              Comment

              Working...
              X