Announcement

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

  • "calling" sets of commands


    Dear Stata Users,

    I have a series of command "sets" in a do file, and want to "call" each set within a later command in that do file. What is the best way to handle this?

    Here is the basic structure of the problem (all in a single do file at this point):

    * First Set
    keep if varname1 == 2
    local regvars "RegVarName1"
    local logitvars "LogitVarName1 LogitVarName2"
    local mlogitvars ""

    * Second Set
    keep if varname2 == 4
    local regvars "RegVarName2"
    local logitvars "LogitVarName3"
    local mlogitvars "MLogitVarName1"

    [....lots and lots of sets...]


    use mydata

    [ run syntax in "First Set" here]

    [ do lots of stuff with the local macros defined by First Set ]


    And then I want to cycle back to "use mydata", run the syntax in "Second Set", etc. until all sets have been used.

    I have tried working with ado files and macros for each syntax set, but no luck so far. I am probably overlooking an easy solution.

    Thanks for any help you can offer.

    --David








  • #2
    You should think about do files that receive arguments.

    Comment


    • #3
      One possibility would be to save the First and Second sets as two separate do-files (let's call them first.do and second.do).

      Then you can invoke those commands with -include first.do- and -include second.do- respectively at the appropriate places in main do-file. -include- is different from -run- or -do- in that the contents of the -include-d file are treated as if they were a part of the main do-file, so macros defined within that segment of code still have scope in the main file.

      Comment


      • #4
        You got some good advice from Nick (if a bit more concise than usual) and from Clyde (who called attention to the fact that (local) macros are local to the do-file (or ado-file) in which they are defined, which may have been the cause of your problems "with ado files [...] for each syntax set"). As Clyde noted, you could use include to mechanically include each of your "syntax sets" in a way that preserves the local macro assignments, but I'm not sure that's the right abstraction. Instead, it sounds like the code that does "lots of stuff with the local macros defined by First Set" and which you then want to "cycle back to" is the part that is common, and therefore what you want to abstract. Thus, I would suggest taking that code and putting it in an ado-file (or, as Nick suggested, a do-file that can handle arguments), and then your code above becomes something like
        myprogram RegVarName1 (LogitVarName1 LogitVarName2) if varname1==2
        myprogram RegVarName2 LogitVarName3 MLogitVarName1 if varname2==4
        etc.
        That, I presume, is the strategy Nick was advocating.
        Last edited by Phil Schumm; 16 Jul 2014, 21:22.

        Comment


        • #5


          Thanks for the helpful advice. Both strategies worked well. I primarily went with Nick's strategy (clarified by Phil), creating a file with lots of arguments that were "sent" to a do file with all the code.

          One problem with sending so many arguments to a do file is keeping track of which argument is what. And the number of arguments has a tendency to expand (exponentially?) to deal with all the particularities of the models and convergence problems of the imputation models. Adding if/else statements in the do file helped.

          Clyde's strategy was pretty easy to implement using this code to parse my syntax into do files to include:

          local i = 1

          cmdlog using Set`i'.do
          [locals defined]
          cmdlog close

          local i = `i' + 1
          cmdlog using Set`i'.do

          I could then loop through the syntax, including each set.

          Thanks again for your generosity and expertise.

          --David

          Comment


          • #6
            Hi,
            I checked these comments, but I have other doubt related with local command and its use with logit command, maybe you can help me. While I programming the local command there's no problem:
            local distante "sregion v024"
            local madre "v208 v012 v106 v131"
            local hogar "v151 v190 v137"
            The real problem arises when i want to use the locals in a logit model, I tried three ways to write command but none of them have worked:
            logit enfermedad `distante´ `madre´ `hogar´
            logit enfermedad 'distante' 'madre' 'hogar'
            logit enfermedad `distante' `madre' `hogar'
            The last line works, but it read as if the line were programmed without any variable, it just recognized a constant. Thanks in advance.

            Comment


            • #7
              See 18.3.1 in http://www.stata.com/manuals14/u18.pdf

              You want left single quote and right single quote. The right single quote is often displayed leaning neither to the left nor to the right [political implications there are none].

              Code:
              . di char(96)
              `
              
              . di char(39)
              '

              Comment

              Working...
              X