Announcement

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

  • Manipulating macro list

    I have a do file which contains a macro list. I want to evaluate each element of the list and determine if it meets a particular criterion:

    Code:
    local roots_orig amlo aten azil ben carv chlor
    foreach var of local roots_orig {
      if `var'_min == 0
    Then, I want to go over the list again, but this time removing the elements from the list for which the criterion is true. For example, if amlo_min were equal to zero, the revised list should be:

    Code:
    local roots_orig aten azil ben carv chlor
    Obviously, I could simply skip elements in the list when `var'_min == 0, but the situation is more complicated than that and I really need to recover the reduced list.

    I dimly recall someone responding to a similar question a few years ago and showing how to do it, but I can not find the original post.
    Richard T. Campbell
    Emeritus Professor of Biostatistics and Sociology
    University of Illinois at Chicago

  • #2
    What do you want to do inside your loop? For example, you can go

    Code:
    local roots_orig amlo aten azil ben carv chlor
    foreach var of local roots_orig {    
        quietly count if `var'_min == 0        
        if r(N) > 0 di "`var'_min{col 33}" r(N)
    }
    The problem with a command like

    Code:
    if `var'_min == 0
    is not just that it does nothing, but also that it means

    Code:
    if `var'_min[1] == 0
    so that it examines the first observation only.

    Comment


    • #3
      With attempting to resolve the issues raised by Nick, the key tool you will need is the list extended macro function, describe in help macrolists. Here's an example of how to use it to start with a local macro containing a list of elements and create a new local macro with elements selectively removed.
      Code:
      . macro list _a _b _c _before
      _a:             yes
      _b:             no
      _c:             maybe
      _before:        a b c
      
      . 
      . local after `before'
      
      . foreach m of local before {
        2.     if "``m''"=="no" {
        3.             local after : list after - m
        4.             }
        5.     }
      
      . 
      . macro list _before _after
      _before:        a b c
      _after:         a c

      Comment


      • #4
        Like Nick and William, I am a bit puzzled by the example. I do not think that extended functions are needed here. From what I understand, this could be as simple as

        Code:
        local a yes
        local b no
        local c maybe
        
        local before a b c
        local after // void
        
        foreach m of local before {
            if "``m''"!="no" local after `after' `m'
        }
        Best
        Daniel

        Comment


        • #5
          In attempting to explain this issue I "simplified" the problem to the point of making my question unanswerable. As Nick correctly infers, the thing won't work without a by statement. I think William Lisowski has pointed me in the right direction. If I can't figure it out from there I will be back with a more carefully contrived example. The actual problem consists of macro lists containing 30 or so elements. Thanks to all.
          Richard T. Campbell
          Emeritus Professor of Biostatistics and Sociology
          University of Illinois at Chicago

          Comment

          Working...
          X