Announcement

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

  • Defining a program inside another program

    I am trying to define a program (call it prog_b) inside another program (call it prog_a). Something like this:

    program define prog_a
    program define prog_b
    [commands I want prog_b to execute]
    end
    end

    The reason is that I need to use prog_b in a forvalues loop and I need it to function slightly differently in each loop, so in each loop I want to run prog_a to redefine prog_b so that in each loop I can use the appropriately modified version of prog_b for that loop.

    The problem is that when I run the code above, in order to define the two functions, I get an error message that says "command end is unrecognized."


  • #2
    You can't nest program definitions so far as I know. In effect Stata is telling you that.

    Your approach would presumably need to include dropping of the original program each time after the first, if I understand correctly.

    You can define two or more programs separately in an .ado or .do file.

    Wanting a program to run differently is a matter of feeding it different arguments, say through variables, qualifiers, weights and options. An awkward way to do that would be to change a global macro before it is called, which I don't recommend.

    Often a good approach is to call Mata code within a loop.
    Last edited by Nick Cox; 03 Feb 2023, 12:14.

    Comment


    • #3
      Duplicate post. Sorry.
      Last edited by daniel klein; 03 Feb 2023, 14:51.

      Comment


      • #4
        It might help to provide a little more detail. What is the underlying problem that you want to solve. Your current approach appears strange and overly complicated.
        Last edited by daniel klein; 03 Feb 2023, 14:54.

        Comment

        Working...
        X