Statalist


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

Re: st: advance a -foreach- loop?


From   "Austin Nichols" <austinnichols@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: advance a -foreach- loop?
Date   Wed, 14 May 2008 10:11:51 -0400

Jeph Herrin <junk@spandrel.net> :
Stata/SE can put 1,081,511 characters in a local macro, so that
constraint may not be binding. To be clear, when I proposed moving the
groups of variables into a separate macro and dealing with the groups
in a separate loop, I took it as read that you had a method for
constructing such groups, since you wrote "I'd like to handle all the
variables in that particular group... I can do this, but then I need
to advance the -foreach-"

The reason for moving the groups of variables into a separate macro
and dealing with the groups in a separate loop is: suppose for any
given variable v, you can identify a group of 6 related variables m_v
(including v itself) and process all 6 inside the loop.  When you
reach the next variable in group m_v, you will repeat this process,
making the whole thing take 6 times as long as it should.  OTOH, the
construction of a long local macro containing groups should take a
negligible amount of time, and then the groups can be looped over one
by one.


On Wed, May 14, 2008 at 9:05 AM, Jeph Herrin <junk@spandrel.net> wrote:
>
> Austin, Nick, thanks for the replies.
>
> I think I should have been more specific about my list
> of variables. I am processing several input files with this,
> and in each all I know about the variables of interest is that
> they have a given prefix. So in practice
>
>  local biglist = "prfx_*"
>
> Ie, I oversimplified in my example; the variables are actually
>
>  prfx_payor_1
>  prfx_payor_2
>  prfx_payor_3
>  ... etc
>
> There are hundreds of variables in each file, and it would be far
> more time consuming to go through and group them, especially as
> I will be rerunning this routinely on files that someone else is
> producing, with the variables changing potentially each time.
>
> Also, because of the number of variables, I think expanding into
> a macro and then indexing over the words will not work because of
> the limit on macro length (right?).
>
> However, I may have to next my loop inside another, and do some
> preprocessing of -biglist- first.
>
> thanks,
> Jeph
>
>
>
> Austin Nichols wrote:
>>
>> Jeph Herrin <junk@spandrel.net>:
>> Can you not use -else- here?  I.e.
>>  foreach V of varlist `biglist' {
>>  gettype(`V')
>>  if `type'=="M" {
>>   <do stuff>
>>  }
>>  else {
>>   <do other stuff>
>>  }
>>  }
>>
>> If you did want to reference variables by their number, and did -keep
>> `biglist'- first, you can refer to word `i' of `biglist' to get the
>> ith variable (see "Macro extended functions for parsing" in help
>> extended_fcn).
>>
>> BTW, it might be more efficient to move the type "M" variables into a
>> separate macro to process separately, depending on how you are
>> choosing to handle all the variables in a particular group, e.g.
>> payor_1-payor_6.  E.g.
>>
>>  foreach V of varlist `biglist' {
>>  gettype(`V')
>>  if `type'=="M" {
>>   <make list of variables related to `V' e.g. payor_1-payor_6>
>>   <and put the list in local `thislist' >
>>   loc mlist `" `mlist' "`thislist'" "'
>>  }
>>  else {
>>   <do other stuff>
>>  }
>>  }
>>  foreach V of varlist `mlist' {
>>  <now operate on each group of related vars>
>>  }
>>
>> On Tue, May 13, 2008 at 3:45 PM, Jeph Herrin <junk@spandrel.net> 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:
>>>
>>>  payor_1
>>>  payor_2
>>>  payor_3
>>>  payor_4
>>>  payor_5
>>>  payor_6
>>>
>>>
>>> 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' {
>>>       gettype(`V')
>>>       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?
>>
>> *
>> *   For searches and help try:
>> *   http://www.stata.com/support/faqs/res/findit.html
>> *   http://www.stata.com/support/statalist/faq
>> *   http://www.ats.ucla.edu/stat/stata/
>>
> *
> *   For searches and help try:
> *   http://www.stata.com/support/faqs/res/findit.html
> *   http://www.stata.com/support/statalist/faq
> *   http://www.ats.ucla.edu/stat/stata/
>
*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



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