Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

# Re: st: Looping over variables

 From Nick Cox To statalist@hsphsun2.harvard.edu Subject Re: st: Looping over variables Date Wed, 19 Dec 2012 10:37:47 +0000

```This is likely to be confusing to others.

As you say, you want to count the number of pregnancies and my code,
along the lines suggested by Daniel, does that. It does that because
the logical condition evaluates to 1 or 0, which gives you the right

In a simpler example if you are counting instances of 42 or 43 then

inlist(42, 42, 43)

returns 1 and

inlist (24, 42, 43)

returns 0  -- so in a loop you can use that result directly. You could
say something like

replace n42_43 = n42_43 + inlist(<whatever>, 42, 43)

You don't have to say

replace n42_43 = n42_43 + 1 if inlist(<whatever>, 42, 43)

If you want to write it your way, that's fine, but there is no sense
in which you _have_ to do that.

Nick

On Wed, Dec 19, 2012 at 9:56 AM, Ingeborg Forthun
<ingeborg.forthun@gmail.com> wrote:
> Thank you very much for your advice!  It works! But as I want to count
> the number of pregnancies for each observation (forgot to write this
> in my first e-mail!) I had to add +1 in the third line in order for it
> to add 1 for each pregnancy.
>
>  gen n_preg = 0
>
> . forval j = 95(6)149 {
>   2.         local J = `j' + 1
>   3.         replace n_preg = n_preg + 1 if inlist(aa`j', 1, 5) |
> (inlist(aa`j', 2, 3, 4, 6, 7) & inrange(aa`J', 13, .))
>   4. }
>
>
>  Ingeborg
>
>
> 2012/12/18 Nick Cox <njcoxstata@gmail.com>
>>
>> I agree with Daniel's main advice. You can simplify this (e.g.)
>>
>> gen n_preg = 0
>> forval j = 95(6)149 {
>>         local J = `j' + 1
>>         replace n_preg = n_preg + inlist(aa`j', 1, 5) | (inlist(aa`j', 2, 3,
>> 4, 6, 7) & inrange(aa`J', 13, .))
>> }
>>
>>
>> [D]     functions . . . . . . . . . . . . . . .  inlist() programming function
>>         (help inlist())
>>
>> [D]     functions . . . . . . . . . . . . . . . inrange() programming function
>>         (help inrange())
>>
>> SJ-9-1  pr0046  . . . . . . . . . . . . . . . . . . .  Speaking Stata: Rowwise
>>         (help rowsort, rowranks if installed) . . . . . . . . . . .  N. J. Cox
>>         Q1/09   SJ 9(1):137--157
>>         shows how to exploit functions, egen functions, and Mata
>>         for working rowwise; rowsort and rowranks are introduced
>>
>> SJ-6-4  dm0026  . . . . . . Stata tip 39: In a list or out? In a range or out?
>>         . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
>>         Q4/06   SJ 6(4):593--595                                 (no commands)
>>         tip for use of inlist() and inrange()
>>
>> It seems also that the names of your variables bear little relation to
>> their contents. Systematic use of -rename- is likely to make further
>> analysis much easier (and less error-prone).
>>
>> to see why.
>>
>> Nick
>>
>> On Tue, Dec 18, 2012 at 8:47 AM, daniel klein <klein.daniel.81@gmail.com> wrote:
>>
>> > You could probably create a loop over the values 95, 101 and so on,
>> > and add 1 to the respective number inside the loop to get at the
>> > durration. But I would look at -egen-'s -anycount()- function first.
>> > This might be a good way of approching this.
>>
>> Ingeborg Forthun
>>
>> > [...]
>> > I want to make a variable that counts the number of pregnancies for each
>> > woman if outcome is 1 or 5 or if outcome is 2,3,4,6 or 7 and number of
>> > weeks of pregnancy was more than 12 weeks. Number of weeks of
>> > pregnancy is given by aa96 (corresponding to the outcome of the first
>> > pregnancy given by aa95), aa102 (corresponding to the outcome of the
>> > second pregnancy given by aa101), and so on.
*
*   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/
```