Announcement

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

  • Using Macros To Comment Out Code

    Hello all,

    I have a small question about whether or not it is possible to use a local macro to comment something out. (It's a weird question, very limited use, but of interested to me.)

    For example, if I have the following command:

    tab var1 if var2>0

    Would it be possible to do something along the lines of the following to comment out the if statement?

    local comment = "//"
    tab var1 `comment' if var2>0


    Whenever I run this code, Stata returns "/ invalid name". It seems that Stata is reading the outputted macro value as a variable name. Is this just how macros are parsed? Am I missing a way to do this?

    Thanks!
    Roger

  • #2
    I'm not sure why this fails - I wonder if it has to do with the way the do-file editor interacts with Stata (similar to how you cannot use "//" at the end of the line for commenting in the command window....it produces this same error that '/' not allowed in varlist).

    Alternatively, you can make the entire -if- condition the option in the macro and then you can turn that condition on/off as needed.

    e.g.,
    Code:
    sysuse auto, clear
    local  comment   "if for>0"
    tab rep78  `comment'
    tab rep78 // `comment'
    
    
    local  comment   " "
    tab rep78  `comment'
    tab rep78 // `comment'
    
    
    
    **or , you could set this up at top of document ..
    local mycond "FALSE"  //CHANGE THIS TO MAKE CHANGES THROUGHOUT DOCUMENT
    
    if `"`mycond'"' == "TRUE" local comment "if for>0"
    if `"`mycond'"' != "TRUE" local comment " "
    
    tab rep78  `comment'
    su mpg t* p*  `comment'


    Eric A. Booth | Senior Director of Research | Far Harbor | Austin TX

    Comment


    • #3
      Eric -- thanks for the help! Yes, the macro as the condition is a solution, which I may have to use.

      I do wonder if anyone knows from a technical standpoint why using the macro to demarcate the comment fails?

      Comment


      • #4
        Are you trying to use this interactively? That wouldn't work regardless. // can't be used that way.

        I guess Stata is reading one token at a time, so given tab var1 what can follow legally is limited by the syntax of tabulate, but / makes no sense in any possible syntax.

        In this example nothing beats

        Code:
        tab var1 
        tab var1 if var2 > 0
        for simplicity and clarity, assuming from context that you have an interest in doing both.

        You might need to do something like this

        Code:
         
        local which "if var2 > 0" 
        tab var1 `which' 
        local which 
        tab var1 `which'
        except for doing it within a loop which might make more sense.

        Comment


        • #5
          If the only use is for if/in conditions, you may want to look at the -marksample- command and/or create simple ado files to manage this:

          Code:
          prog def x
          syntax varlist(max=1) [if] [in] [, OPTions(string asis) ]
          marksample touse
          ta `varlist' if `touse', `options'
          end
          It isn't tested, but this should at least give you an idea of a way of handling these types of issues in ways that generalize to Stata programming more generally. The only changes I would suggest to eric_a_booth's approach would look like:

          Code:
          loc mycond "FALSE"
          
          // Only need to set the comment macro in the affirmative case
          // If the condition occurs later and/or if you need to check if the macro is an empty string
          // you might want to handle things a bit more explicitly
          if `"`mycond'"' == "TRUE" loc comment "if foreign > 0"
          
          ta rep78 `comment'
          su mpg t* p* `comment'
          
          // If you wanted to be more explicit about your handling of the cases:
          if `"`mycond'"' == "TRUE" loc comment "if foreign > 0"
          else loc comment ""
          It doesn't alter the functionality, but can save you a few keystrokes from time to time.

          Comment


          • #6
            My guess at the answer to Roger's question in #3

            I do wonder if anyone knows from a technical standpoint why using the macro to demarcate the comment fails?
            is that Stata takes a single initial pass at screening for comments in do-files, and then expands macros without rescreening. To allow an expanded macro to delineate a comment would require repeated rescanning the command after each macro expansion to see if a comment construct had been created by the new expansion. This way lies madness.

            While I've used tricks like the one Roger proposes in Some Alternative System for statistics, Stata's handling of macros is much different, and in my judgement, superior to that in my previous experience.

            Comment

            Working...
            X