Announcement

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

  • Looping over subroutines - Break error

    Hi,

    I am trying to loop over subroutines but I am getting a break error before entering any of the subroutines. Can someone help me fix the code and understand why this is happening? Thanks.

    Code:
    foreach UNIVERSE in "LS&PCOMP" "LNASCOMP" "LS&P600I" "LDJCMP65" {
    
        *** O) Parameter definition ***
        capture program drop parameter_definition
        program parameter_definition
    
            global universe `UNIVERSE'
            display "$universe"
    
        end
    
        *** I) Data construction ***
        capture program drop data_construction
        program data_construction
    
            display "Construction Code"
            display "$universe"
            
        end
    
        *** II) Predictive model ***
        capture program drop predictive_model
        program predictive_model
    
            display "Predictive Model Code"
            display "$universe"
    
        end
    
    
    
    
    parameter_definition
    data_construction
    predictive_model
    }
    Code:
    . foreach UNIVERSE in "LS&PCOMP" "LNASCOMP" "LS&P600I" "LDJCMP65" {
      2. 
    .         *** O) Parameter definition ***
    .         capture program drop parameter_definition
      3.         program parameter_definition
      4. 
    .                 global universe `UNIVERSE'
      5.                 display "$universe"
      6. 
    .         end
    --Break--
    r(1);
    
    end of do-file
    
    --Break--
    r(1);




  • #2
    You may find the discussion at post #2 of

    https://www.statalist.org/forums/for...-don-t-press-q

    informative, although the answer - do not define programs within loops - is not what you want to hear. Note that the linked post refers to blocks of Mata code, but the lesson holds for any block that is terminated by a line of "end".
    Last edited by William Lisowski; 07 Aug 2020, 05:30.

    Comment


    • #3
      Perhaps the code here is just the first step towards something else, but what is presented is problematic even if the difficulty identified by William Lisowski is resolved. The first line in parameter_definition is


      Code:
       
        global universe `UNIVERSE'
      which will do thing except blank out the named global. No local macro UNIVERSE has been defined before it in the program, and no local macro anywhere outside the program is visible within the program.That's what local means....

      So the statement reduces to


      Code:
      global universe

      Comment


      • #4
        I think the code presented in post #1 is a minimal reproducible example of the problem, for which I thank Francois. Perhaps this code will take him closer to what he needs for his actual code.
        Code:
        *** main program ***
        capture program drop main
        program main
        foreach UNIVERSE in LS&PCOMP LNASCOMP LS&P600I LDJCMP65 {
            parameter_definition `UNIVERSE'
            data_construction
            predictive_model
        }
        end
        
        *** O) Parameter definition ***
        capture program drop parameter_definition
        program parameter_definition
        
            global universe `1'
            display "Parameter Definition Code"
            display "$universe"
        
        end
        
        *** I) Data construction ***
        capture program drop data_construction
        program data_construction
        
            display "Construction Code"
            display "$universe"
           
        end
        
        *** II) Predictive model ***
        capture program drop predictive_model
        program predictive_model
        
            display "Predictive Model Code"
            display "$universe"
        
        end
        
        *** do the work
        main
        Code:
        . *** do the work
        . main
        Parameter Definition Code
        LS&PCOMP
        Construction Code
        LS&PCOMP
        Predictive Model Code
        LS&PCOMP
        Parameter Definition Code
        LNASCOMP
        Construction Code
        LNASCOMP
        Predictive Model Code
        LNASCOMP
        Parameter Definition Code
        LS&P600I
        Construction Code
        LS&P600I
        Predictive Model Code
        LS&P600I
        Parameter Definition Code
        LDJCMP65
        Construction Code
        LDJCMP65
        Predictive Model Code
        LDJCMP65
        
        .

        Comment


        • #5
          Thanks William. Would you be able to explain the syntax? In particular, how does the local variable from here:
          Code:
           
           parameter_definition `UNIVERSE'
          enters the loop here:
          Code:
          global universe `1'
          After reordering the blocks, with the new "main" block on top, what is the problem with my original syntax? I just want to make sure I know how to address the problem next time.

          Comment


          • #6
            `1' refers to the first positional argument given to the program parameter_definition. The first positional argument here is `UNIVERSE' so that `1' becomes `UNIVERSE' when you call the command. In your initial code, you did not use this local macro as an argument for your command.

            Comment


            • #7
              see [U] 18.4 Program arguments

              Comment


              • #8
                Along with carefully comparing my code in post #4 to your code in post #1 and noting the differences, like the addition of `UNIVERSE' as an argument on the parameter_definition program call, you further need to carefully read Chapter 18 Programming Stata in the Stata User's Guide PDF included in your Stata installation and accessible from Stata's Help menu. In particular, section 18.3 addresses the confusion you have shown about the difference between macros and variables, and section 18.4 explains how arguments are used to pass parameters into programs.

                When reading this material, note the following problem with the typography in the PDF. The Stata PDF documentation unfortunately displays local macro references incorrectly using smart single quotes as
                Code:
                 ‘shortcut’
                which leads to no end of confusion, especially because such code will not work if copied and pasted into Stata. On the other hand, it does look elegant in the printed documentation. From your experience thus far with local macros, you already know that you need to type
                Code:
                 `shortcut'
                so don't let the documentation confuse you on this matter.

                Comment


                • #9
                  Thank you so much William, both for the solution & explanation!

                  Comment

                  Working...
                  X