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 10:56:44 +0100

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