Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

# Re: st: Fwd: Nested Foreach Comparing too Many Variables

 From Nick Cox To "statalist@hsphsun2.harvard.edu" Subject Re: st: Fwd: Nested Foreach Comparing too Many Variables Date Fri, 22 Mar 2013 17:07:49 +0000

```Nested -foreach- with I possibilities and -foreach- with J
possibilities gives you _all_ I * J possibilities, in your case 5 * 5
= 25.

You want a single loop over I = 5 possibilities.

See Section 3 of

SJ-3-2  pr0009  . . . . . . . . . . . . . Speaking Stata:  Problems with lists
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
Q2/03   SJ 3(2):185--202                                 (no commands)
discusses ways of working through lists held in macros

http://www.stata-journal.com/sjpdf.html?articlenum=pr0009

On Fri, Mar 22, 2013 at 1:52 PM, Nick Cox <njcoxstata@gmail.com> wrote:
> Yes; you are right that you were wrong.
>
> There was a fuller discussion in my paper "Problems with lists" in the Stata
> Journal in 2003. I'll post the full reference later today.
>
> Nick
>
>
> On 22 Mar 2013, at 13:32, William Sankey <wsankey@gmail.com> wrote:
>
>> Thank you Nick, this looks great.
>>
>> Would you be able to tell me if I was right...is Diagnosis1 being
>> compared against Severity1 Severity2 Severity3 and Severity4...or just
>> Severity1? I just really need Diagnosis1 to match to Severity1,
>> Diagnosis2 to match to Severity2 etc...
>>
>> Is that what is happening here?
>>
>> Thank you again for the help.
>>
>> Will Sankey
>>
>> On Fri, Mar 22, 2013 at 5:06 AM, Nick Cox <njcoxstata@gmail.com> wrote:
>>>
>>> Yes. You will kick yourself.
>>>
>>> forval j = 1/5 {
>>>         replace  complex_severity=1 ///
>>>         if  inlist(Diagnosis`j',"710","714","715","720","725","733")
>>> & inrange(Severity`j', 2, .)
>>> }
>>>
>>> -inrange()- is another way to handle your second comparison. What you
>>> had is fine; I just showed you another way.
>>>
>>> Nick
>>>
>>> On Fri, Mar 22, 2013 at 9:57 AM, William Sankey <wsankey@gmail.com>
>>> wrote:
>>>
>>>> I am running the following nested foreach and am afraid I am comparing
>>>> too many things:
>>>>
>>>> local DIAG "Diagnosis1 Diagnosis2 Diagnosis3 Diagnosis4 Diagnosis5"
>>>> local SEV "Severity1 Severity2 Severity3 Severity4 Severity5"
>>>>
>>>>        foreach X of varlist `DIAG' {
>>>>                    foreach Y of varlist `SEV' {
>>>>        replace  complex_severity=1 if
>>>> inlist(`X',"710","714","715","720","725","733") & (`Y'>=2  & `Y' !=.)
>>>>        }
>>>>        }
>>>>
>>>> I would like to compare Diagnosis1 with Severity1 but fear the code is
>>>> comparing Diagnosis1 with every severity level.
>>>>
>>>> Could you recommend a better way of achieving the goal; something
>>>> along the lines of a different array?
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/
```

© Copyright 1996–2015 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index