Announcement

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

  • Identify range of macros to be dropped.

    Hi Everyone,

    I'm working on a project where I have to define a lot of local variables and will reuse those naming conventions each loop. Given the scope of the project I would like to wipe the locals manually to prevent 'bleed-through' from one loop to the next. However, there are locals that I periodically define before and after that I would like to preserve.

    Is there a way to wipe a range of locals?

    Apologies for the bolded pseudo-code, but it conveys the basic essence and the desired result in my example. I've gone through the pmacro documentation but I can't find anything quite describing what I'm talking about.

    Code:
    . local a = 1 
    
    . local b = 2 
    
    . local c = 3 
    
    . local d = 4
    
    . local e = 5
    
    . macro list 
    _a:             1
    _b:             2
    _c:             3
    _d:             4
    _e:             5
    
    macro drop _b - _d
    
    . macro list 
    _a:             1
    _e:             5
    Is there a way to do this? I know there are work arounds to prefixing locals and dropping based on prefixes, but that will be annoying to redefine every local. Alternatively I could use globals to keep the macros I want and drop locals, but I'd prefer to avoid globals if possible.

    Thanks for any insight into this!

    Cheers,

    David.

  • #2
    You seem to have answered your own question. At one extreme, macro drop _all drops every local and you can define the others at will.

    Comment


    • #3
      I see no particular reason to define globals. Or redefine locals to have certain prefixes. Why not just keep track of the locals to drop as
      Code:
      local to_drop _b _c _d
      and eventually
      Code:
      macro drop `to_drop'

      Comment


      • #4
        Hemanshu Kumar

        Yes, that will work, but it is also more labour intensive than what I'm seeking. It would require me to redefine a local after each of the 100 locals I am already defining. This is why I was looking for a function that worked similar to how ranges of variables can be dropped. To clarify, I can absolutely brute force the solution, but 95% of the time Stata has a cool feature that I'm simply unaware of.
        Last edited by David Speed; 23 Dec 2022, 12:05.

        Comment

        Working...
        X