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]

From |
Ingeborg Forthun <ingeborg.forthun@gmail.com> |

To |
statalist@hsphsun2.harvard.edu |

Subject |
st: Looping over variables |

Date |
Tue, 18 Dec 2012 08:58:04 +0100 |

Hello all, I have a dataset that includes information on the outcome of pregnancies and number of weeks of pregnancy for each outcome for about 100 000 women identified by an id-number PREG_ID. The outcome of each pregnancy is defined by the variables aa95 (first pregnancy), aa101 (second pregnancy), aa107 (third pregnancy), and so on (in total 10 possible pregnancies). And each of these variables can take values from 1-7 (1=liverborn, 2=stillbirth, 3=abortion etc). 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. I have used this code in STATA: gen n_preg = 0 replace n_preg = n_preg + 1 if aa95==1 | aa95==5 | ((aa95==2 | aa95==3 | aa95==4 | aa95==6 | aa95==7) & aa96>12 & aa96 != .) replace n_preg = n_preg + 1 if aa101==1 | aa101==5 | ((aa101==2 | aa101==3 | aa101==4 | aa101==6 | aa101==7) & aa102>12 & aa102 != .) replace n_preg = n_preg + 1 if aa107==1 | aa107==5 | ((aa107==2 | aa107==3 | aa107==4 | aa107==6 | aa107==7) & aa108>12 & aa108 != .) replace n_preg = n_preg + 1 if aa113==1 | aa113==5 | ((aa113==2 | aa113==3 | aa113==4 | aa113==6 | aa113==7) & aa114>12 & aa114 != .) replace n_preg = n_preg + 1 if aa119==1 | aa119==5 | ((aa119==2 | aa119==3 | aa119==4 | aa119==6 | aa119==7) & aa120>12 & aa120 != .) replace n_preg = n_preg + 1 if aa125==1 | aa125==5 | ((aa125==2 | aa125==3 | aa125==4 | aa125==6 | aa125==7) & aa126>12 & aa126 != .) replace n_preg = n_preg + 1 if aa131==1 | aa131==5 | ((aa131==2 | aa131==3 | aa131==4 | aa131==6 | aa131==7) & aa132>12 & aa132 != .) replace n_preg = n_preg + 1 if aa137==1 | aa137==5 | ((aa137==2 | aa137==3 | aa137==4 | aa137==6 | aa137==7) & aa138>12 & aa138 != .) replace n_preg = n_preg + 1 if aa143==1 | aa143==5 | ((aa143==2 | aa143==3 | aa143==4 | aa143==6 | aa143==7) & aa144>12 & aa144 != .) replace n_preg = n_preg + 1 if aa149==1 | aa149==5 | ((aa149==2 | aa149==3 | aa149==4 | aa149==6 | aa149==7) & aa150>12 & aa150 != .) This works fine, but is there a more elegant solution, for example using foreach? I have tried help foreach with not much luck. I'll be very grateful for any help on this. Best regards, Ingeborg Forthun * * 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/

- Prev by Date:
**st: RE: RE: frontier** - Next by Date:
**st: biasness in panel FE** - Previous by thread:
**st: xtivreg post-estimation command help** - Next by thread:
**Re: st: Looping over variables** - Index(es):