Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

RE: st: looping over observations -mata-?


From   "philippe van kerm" <philippe.vankerm@ceps.lu>
To   <statalist@hsphsun2.harvard.edu>
Subject   RE: st: looping over observations -mata-?
Date   Wed, 12 Oct 2005 18:16:17 +0200

Ricardo,

I can't give advice about how Mata can be used for this problem, but it
looks as though you do not need that machinery and should be able to do
this without looping over observations. What about (some variation of)
this:

gen notfather = (relation!="f")
bysort familyid (notfather): gen fatherid = subjid[1] if notfather[1]==0
replace fatherid = . if relation!="c"

gen notmother = (relation!="m")
bysort familyid (notmother): gen motherid = subjid[1] if notmother[1]==0
replace motherid = . if relation!="c"

drop notmother notfather
sort familyid subjid


Hope this helps

Philippe


<<<>>><<<>>><<<>>><<<>>><<<>>><<<>>><<<>>><<<>>>
Dr. Philippe Van Kerm
IRISS - Integrated Research Infrastructure in 
  the Socio-Economic Sciences
CEPS/INSTEAD
BP48, L-4501 Differdange, Luxembourg
>> http://www.ceps.lu/iriss <<
>> http://econpapers.repec.org/RAS/pva19.htm <<
<<<>>><<<>>><<<>>><<<>>><<<>>><<<>>><<<>>><<<>>> 


>st: looping over observations -mata-?
>From 	  Ricardo Ovaldia <ovaldia@yahoo.com>
>To 	  statalist@hsphsun2.harvard.edu
>Subject 	  st: looping over observations -mata-?
>Date 	  Wed, 12 Oct 2005 08:49:34 -0700 (PDT)
>
>Dear all,
>
>I have household data with one observation per family
>member. All House hold have one or both parents and
>anywhere from 1 to seven children. All households have
>children but no grandparents or other relatives. Here
>are a few tipical observations and relevant variables:
>
>. cl  familyid subjid relation
>
>     familyid    subjid   relation
>  1.     1001         1          f
>  2.     1001         2          m
>  3.     1001         3          c
>  4.     1001         4          c
>  5.     1002         1          m
>  6.     1002         2          c
>  7.     1002         3          c
>  8.     1003         1          m
>  9.     1003         2          f
> 10.     1003         3          c
>
>where for -relation-: f=father, m=mother and c=child
>
>I want to create two new variables which hold, for the
>children, their parent's -subjectid-  as follows:
>
>     familyid    subjid   relation  fatherid  motherid
>  1.     1001         1          f         .         .
>  2.     1001         2          m         .         .
>  3.     1001         3          c         1         2
>  4.     1001         4          c         1         2
>  5.     1002         1          m         .         .
>  6.     1002         2          c         .         2
>  7.     1002         3          c         .         2
>  8.     1003         1          m         .         .
>  9.     1003         2          f         .         .
> 10.     1003         3          c         2         1
>
>I wrote a program to do this but is very slow because
>it loops over observations. 
>I think that if I recode this using -mata- it would be
>faster, but I not sure where to begin. Any assistance
>or suggestions will be greatly appreciated.
>
>Regards,
>Ricardo 
>
>
>Ricardo Ovaldia, MS
>Statistician 
>Oklahoma City, OK
>

**********************************************************************
This email and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom they
are addressed. If you have received this email in error please notify
the system manager.

This footnote also confirms that this email message has been swept 
**********************************************************************



*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index