Announcement

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

  • Define subroutine in a .do file

    Hi,

    I have a large do file that I would like to break down in several smaller pieces of code that I could run (or not) from the top of the 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 get the error message below. Anyone can point to the right direction/format?Thanks!

    Code:
    clear
    main
    
    capture program drop main
    program main
        test
    end
    
    capture program drop test
    program test
        di "Hello World"
        test2
    end
    
    capture program drop test2
    program test2
    
    input x y
    32 56
    26 67
    34 61
    39 58
    29 58
    end
    
    list
    end
    Code:
    . clear
    
    . main
    command main is unrecognized
    r(199);
    
    end of do-file
    Can someone tell me what I am doing wrong? Thanks

    I have tried to post a similar message a few days ago:
    https://www.statalist.org/forums/for...e-same-do-file

  • #2
    You first tried to call the program main before defining it. Stata only know what it is supposed to do after you defined a program. So if you want to apply this strategy, the top of your do file should contain all the program definitions, and only after that you can call those programs.
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      Thanks Maarten. I have implemented your suggestion but I am still getting the same error. Here is the structure of my program (minimized to save space but contains code in each subroutine and finished by "end"):

      Code:
      *** O) Parameter definition ***
      capture program drop parameter_definition
      program parameter_definition
      
      *** I) Data construction ***
      capture program drop data_construction
      program data_construction
      
      *** II) Predictive model ***
      capture program drop predictive_model
      program predictive_model
      
      *** III) Call the subroutines ***
      parameter_definition
      data_construction
      predictive_model
      Here is the error I get when I hit the button "Execute (do)" for the whole program
      Code:
      . data_construction
      command data_construction is unrecognized
      It works when I highlight and run each subroutine individually and then call the subroutines. Is there any way to prevent me having to run them individually each time before calling? Thanks for your help.

      Comment


      • #4
        Can't reproduce your error. There's something that you're not showing us. Check again.

        .ÿ
        .ÿversionÿ16.1

        .ÿ
        .ÿclearÿ*

        .ÿ
        .ÿ***ÿO)ÿParameterÿdefinitionÿ***
        .ÿcaptureÿprogramÿdropÿparameter_definition

        .ÿprogramÿdefineÿparameter_definition
        ÿÿ1.ÿÿÿÿÿÿÿÿÿversionÿ16.1
        ÿÿ2.ÿÿÿÿÿÿÿÿÿsyntax
        ÿÿ3.ÿ
        .ÿÿÿÿÿÿÿÿÿdisplayÿinÿsmclÿasÿtextÿ_newline(1)ÿ"ParameterÿDefinition"
        ÿÿ4.ÿend

        .ÿ
        .ÿ***ÿI)ÿDataÿconstructionÿ***
        .ÿcaptureÿprogramÿdropÿdata_construction

        .ÿprogramÿdefineÿdata_construction
        ÿÿ1.ÿÿÿÿÿÿÿÿÿversionÿ16.1
        ÿÿ2.ÿÿÿÿÿÿÿÿÿsyntax
        ÿÿ3.ÿ
        .ÿÿÿÿÿÿÿÿÿdisplayÿinÿsmclÿasÿtextÿ_newline(1)ÿ"DataÿConstruction"
        ÿÿ4.ÿend

        .ÿ
        .ÿ***ÿII)ÿPredictiveÿmodelÿ***
        .ÿcaptureÿprogramÿdropÿpredictive_model

        .ÿprogramÿdefineÿpredictive_model
        ÿÿ1.ÿÿÿÿÿÿÿÿÿversionÿ16.1
        ÿÿ2.ÿÿÿÿÿÿÿÿÿsyntax
        ÿÿ3.ÿ
        .ÿÿÿÿÿÿÿÿÿdisplayÿinÿsmclÿasÿtextÿ_newline(1)ÿ"PredictiveÿModel"
        ÿÿ4.ÿend

        .ÿ
        .ÿ***ÿIII)ÿCallÿtheÿsubroutinesÿ***
        .ÿparameter_definition

        ParameterÿDefinition

        .ÿdata_construction

        DataÿConstruction

        .ÿpredictive_model

        PredictiveÿModel

        .ÿ
        .ÿexit

        endÿofÿdo-file


        .

        Comment

        Working...
        X