Announcement

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

  • Processing two lists in parallel

    I am puzzled about how to do something that I am sure is simple. I have two lists: (a) a list of drugs and (b) a list of minimum doses of those drugs. I have data on patients who have been prescribed one of more of those drugs and I want to create a set of variables that indicate whether the prescription is at or above the minimum.

    Here is the local macro for the list of drugs and the macro containing the list of minimum doses:

    Code:
    local roots_orig amlo aten azil ben carv chlor fur hctz ind irb lis los neb nif nis spiro tel val meto_ir meto_la
    
    local orig_min 5 50 40 10 6.25 25 40 12.5 1.25 150 10 100 5  30 10 25 40 320 75 25
    The roots_orig macro points to a list of total doses per day, e.g. amlo_total. I want to create a new variable, e.g. amlo_gcc if the value of amlo_total >= 5 and proceed similarly across the list of drugs.

    If I were programming this "by hand" I would simply write:

    Code:
    gen amlo_gcc = amlo_total  >= 5
    gen aten_gcc  = aten_total >= 50
    etc

    I realize that I can't do this with nested foreach loops, but I can't seem to figure out how to handle the two lists in parallel. It might be easiest to just brute force a solution, but I have a number of other instances to deal with.
    Richard T. Campbell
    Emeritus Professor of Biostatistics and Sociology
    University of Illinois at Chicago

  • #2
    There is a FAQ on this topic at Stata.com: https://www.stata.com/support/faqs/p...arallel-lists/
    Stata/MP 14.1 (64-bit x86-64)
    Revision 19 May 2016
    Win 8.1

    Comment


    • #3
      Here's one way to do it using tokenize to iterate over one list while the forval loop iterates over the other list.

      Code:
      clear
      set obs 5000
      
      
      local roots_orig amlo aten azil ben carv chlor fur hctz ind irb lis los neb nif nis spiro tel val meto_ir meto_la
      
      local orig_min 5 50 40 10 6.25 25 40 12.5 1.25 150 10 100 5 30 10 25 40 320 75 25
      
      **Create fake dataset
      foreach j in `roots_orig' {
      g `j'_total = runiformint(0,325)
      }
      
      
      *tokenize min values and interate using macro shift as you go through forval loop:
      token `"`orig_min'"'
      foreach j in `roots_orig' {
      
      di `"`j' :: `1'"'
      
      g `j'_gcc = `j'_total if `j'_total>`1'
      macro shift
      }
      
      
      aorder
      Eric A. Booth | Senior Director of Research | Far Harbor | Austin TX

      Comment


      • #4
        I like this way of approaching the problem:

        Code:
        local agrp "cat dog cow pig"
        local bgrp "meow woof moo oinkoink"  
        foreach a of local agrp {    
            gettoken b bgrp : bgrp      
            di "`a' says `b'"
        }
        There is an asymmetry to the approach, but that itself may appeal as well as appall. With your left hand you loop over the items of one group, and as you go round the loop with your right hand you take off the first item of another group without replacing it. Or switch hands if you prefer.

        The method extends to any number of groups, as does that in the FAQ, but the image of hands is no longer so simple with three or more groups.

        There aren't any checks that groups have the same number of items, but that is also true of the FAQ approach and in neither case would that be a bug so far as Stata is concerned.

        There is ancient literature on this too: https://www.stata-journal.com/sjpdf....iclenum=pr0009 That paper, I think, preceded the introduction of gettoken. -- or at least of my awareness of its use for this problem, which much more recently was reinforced by seeing good examples from Robert Picard.

        Comment


        • #5
          Thanks to all who responded to this query. Problem solved. It is so wonderful to be part of this community. Meow, woof, moo, oinkoink!
          Richard T. Campbell
          Emeritus Professor of Biostatistics and Sociology
          University of Illinois at Chicago

          Comment

          Working...
          X