[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: advance a -foreach- loop?

From   "" <>
To   Statalist <>
Subject   Re: st: advance a -foreach- loop?
Date   Wed, 14 May 2008 07:41:32 -0700

Jeph Herrin wrote:

I have hundreds of variables that I need to loop through
and process. Some of them are grouped together because they
represent different options that are not mutually exclusive;
for example, "Insurance type: check all that apply:" has
results stored in:


I have a datum -type- associated with each that tells me it is part of a
"multiselect" (sic) question, so when I loop through all my variables:

   foreach V of varlist `biglist' {
 if `type'=="M" {
  <do stuff>

The ... handles every other types of variable. However, when I
<do stuff> I'd like to handle all the variables in that particular
group, eg, payor_1-payor_6. I can do this, but then I need to advance
the -foreach- to the variable *after* payor_6.

One thought I had was to reference the variables by their number, which
is available via -describe, number- (I'd have to -keep `biglist'- first)
but I'm not sure how to refer to a variable by its number, and can't
find any info on how to do so.

Does anyone see how to make this idea - or any other- work so I can skip
ahead in a -foreach- loop?


How about something along the lines of the do-file below?  The "type" is
stored as a characteristic in the example.  It uses -while- loops instead of
nested -foreach- to process the macros via -gettoken-.

Joseph Coveney

clear *
set more off
input byte payor_1 byte payor_2 byte payor_3 ///
  byte payor_4 byte payor_5 byte payor_6 ///
  str1 payee_1 str2 payee_2
1 1 1 1 1 1 "Q" "R"
foreach var of varlist payor_* {
 char define `var'[type] M
foreach var of varlist payee_* {
 char define `var'[type] "Not M"
local biglist payor_1 payor_2 payor_3 payor_4 ///
  payor_5 payor_6 payee_1 payee_2
* Begin here
gettoken var biglist : biglist
while ("`var'" != "" ) {
 local multiselect : char `var'[type] // <-> gettype(`V')
 while ("`multiselect'" == "M") {
  display in smcl as text "Do stuff `var'"
  gettoken var biglist : biglist
  local multiselect : char `var'[type]
 display in smcl as text "Do other stuff `var'"
 gettoken var biglist: biglist

*   For searches and help try:

Privileged, confidential or patient identifiable information may be contained in this message. This information is meant only for the use of the intended recipients. If you are not the intended recipient, or if the message has been addressed to you in error, do not read, disclose, reproduce, distribute, disseminate or otherwise use this transmission. Instead, please notify the sender by reply e-mail, and then destroy all copies of the message and any attachments.

*   For searches and help try:

© Copyright 1996–2022 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index