Announcement

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

  • Macros that can be read by programs

    I developed a program to perform regressions and numerous post-estimation commands. I'm running many regressions, so writing a program is much more efficient than copy-pasting each time. To demonstrate, consider this basic program with Stata's car dataset:


    Code:
    sysuse auto.dta, clear
    
    program myProgram
    
    reg price `1' if `2'
    est store res_`1'
    
    end
    This allows me to run the program, each time specifying the independent variable and the if condition:

    Code:
    myProgram mpg `"make!="Ford Mustang" & make!="Honda Accord""'
    myProgram trunk `"make!="Ford Mustang" & make!="Honda Accord""'
    myProgram length `"make!="Ford Mustang" & make!="Honda Accord""'
    myProgram weight `"make!="Ford Mustang" & make!="Honda Accord""'
    When the if condition does not change, like in the case above, I would like to define an object that stores the condition so that I just have to refer to that object (and not the entire condition). I have tried doing this with a global macro:

    Code:
    global ifCondition `"make!="Ford Mustang" & make!="Honda Accord""'
    But when I then try to execute the program using the if condition:

    Code:
    myProgram mpg $ifCondition
    Stata returns the error:

    Code:
    Invalid syntax
    Do I have to redefine the global macro, and if yes, how? Alternatively, is there some other macro I could use?

    Also, please note that putting the if condition into the program is not an answer. I need the program to be flexible to different if conditions.
    Last edited by Colette Salemi; 06 Aug 2022, 14:08.

  • #2
    Hi Colette,

    One of the awesome things about Stata macros is that they can store lines of executable code as well as things like strings! The following line works on my end:

    Code:
    global ifCondition = make!="Ford Mustang" & make!="Honda Accord"

    Comment


    • #3
      #2 The global will be evaluated using values in observation 1. That is unlikely to be what anyone wants. Adding the equals sign is not the solution.


      But I can’t yet see what is wrong with #1.

      Comment


      • #4
        Originally posted by Daniel Schaefer View Post
        Hi Colette,

        One of the awesome things about Stata macros is that they can store lines of executable code as well as things like strings! The following line works on my end:

        Code:
        global ifCondition = make!="Ford Mustang" & make!="Honda Accord"
        Thank you for trying to help. When I tried this code, the program executed, but it did not apply the if condition (N=74, the number of obs. instead of N=72, the number of obs, minus those two car makes).

        Comment


        • #5
          Just run
          Code:
           myProgram mpg `"$ifCondition"'
          Stata removes the quotation marks at the beginning and the end of the string when you put it into a macro.

          Comment


          • #6
            Originally posted by Sven-Kristjan Bormann View Post
            Just run
            Code:
             myProgram mpg `"$ifCondition"'
            Stata removes the quotation marks at the beginning and the end of the string when you put it into a macro.
            Thank you! This worked!!

            Comment


            • #7
              Nick Cox I definitely wasn't thinking the equals sign was the solution. Much worse I'm afraid. I striped the quotes from the line, and imagined that the interpreter knew how to store the expression and evaluate it in the right context. That is, of course, completely wrong. Obviously if the line isn't wrapped in quotes, the line is executed then and there. What was I thinking?

              Comment


              • #8
                I think I missed the obvious too. This will work too.

                Code:
                 
                 global ifCondition `" `"make!="Ford Mustang" & make!="Honda Accord""' "'
                as each time the global is processed the outermost quotation marks are stripped.



                Comment


                • #9
                  Code:
                  set trace on
                  set traceexpand on
                  will help debug code.

                  One alternative can be to prevent/delay macro expansion using "\".
                  Code:
                  global IF make!="Ford Mustang" & make!="Honda Accord"
                  
                  qui myProgram mpg \$IF
                  Code:
                  . di `"`e(cmdline)'"
                  regress price mpg if make!="Ford Mustang" & make!="Honda Accord""
                  And, the syntax command is an alternative to parse args:
                  Code:
                  program myRegPriceProgram
                  
                  syntax varlist(max=1) if
                  
                  local x1 `varlist'
                    
                  reg price `x1' `if'
                  
                  est store res_`x1'
                  
                  end
                  
                  sysuse auto
                  
                  global IF make!="Ford Mustang" & make!="Honda Accord"
                  
                  myRegPriceProgram mpg if $IF
                  Last edited by Bjarte Aagnes; 07 Aug 2022, 04:31.

                  Comment

                  Working...
                  X