Bookmark and Share

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


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

Re: st: Looping over variables


From   Ingeborg Forthun <[email protected]>
To   [email protected]
Subject   Re: st: Looping over variables
Date   Wed, 19 Dec 2012 13:30:00 +0100

I am sorry if I have misunderstood, but the two codes do not give the
same result.

The first code:
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, .))
 }

returns:

   n_preg |      Freq.     Percent        Cum.
------------+-----------------------------------
          0 |     46,703       45.91       45.91
          1 |     55,024       54.09      100.00
------------+-----------------------------------
      Total |    101,727      100.00


The second code:
gen n_preg = 0
forval j = 95(6)149 {
        local J = `j' + 1
        replace n_preg = n_preg + 1 if inlist(aa`j', 1, 5) |
(inlist(aa`j', 2, 3,
 4, 6, 7) & inrange(aa`J', 13, .))
 }

returns:

  n_preg |      Freq.     Percent        Cum.
------------+-----------------------------------
          0 |     46,703       45.91       45.91
          1 |     35,664       35.06       80.97
          2 |     15,164       14.91       95.88
          3 |      3,303        3.25       99.12
          4 |        682        0.67       99.79
          5 |        143        0.14       99.93
          6 |         35        0.03       99.97
          7 |         19        0.02       99.99
          8 |          7        0.01       99.99
          9 |          4        0.00      100.00
         10 |          3        0.00      100.00
------------+-----------------------------------
      Total |    101,727      100.00


This is what I need to know. I have to know the exact number of
pregnancies and not only 0 or 1.

Ingeborg

2012/12/19 Nick Cox <[email protected]>:
> 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
> answer.
>
> 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
> <[email protected]> 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 <[email protected]>
>>>
>>> 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, .))
>>> }
>>>
>>> See also for specific and general advice:
>>>
>>> [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).
>>>
>>> Finally, for "STATA" read "Stata", and please read the Statalist FAQ
>>> to see why.
>>>
>>> Nick
>>>
>>> On Tue, Dec 18, 2012 at 8:47 AM, daniel klein <[email protected]> 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/
*
*   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–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index