Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: RE: st: Problem with "If" statement


From   Deepankar Basu <[email protected]>
To   [email protected]
Subject   Re: RE: st: Problem with "If" statement
Date   Wed, 07 Jun 2006 15:42:02 -0400

Nick,

Following your suggestion, I have -reshape- the data. This is how it
looks now:

id  chilno  dfsize  nboy alive b  boyno  cno
1     1       4       2    4   1    1     1
1     2       4       2    4   2    1     2
1     3       4       2    4   1    2     3
1     4       4       2    4   2    2     4
1     5       4       2    4   .    2     4
1     6       4       2    4   .    2     4
1     7       4       2    4   .    2     4
2     1       4       2    3   1    1     1
2     2       4       2    3   .    1     1
2     3       4       2    3   2    1     2
2     4       4       2    3   .    1     2
2     5       4       2    3   1    2     3
2     6       4       2    3   .    2     3
2     7       4       2    3   .    2     3


dfsize: desired family size
nboy  : number of boys alive in the family
alive : noumber of children alive
b     : 1(boy), 2(girl), .(dead or not born)
boyno : first boy or second boy, etc.
cno   : first child or second child, etc. (only alive children)


1. I want to first generate the following variables:

local i 1
while `i' <= 7 {  \\There are a maximum of 7 children in any family

 local j 1
 while `j'<= `j' {
   gen p`i'`j'=0
   local j= `j' + 1
 }
local i = `i' + 1
}


local k = `nboy'
local i 1
forvalues i=1(1)`k' {
  replace p`dfsize'`k' = (0.5)^`alive'
}

I can do this step.

2. Now I want to replace the value of p`dfsize'`k' with 0 whenever the
kth (k runs from 1 to `nboy') boy is not the Nth child. For instance,
for the family with id==1, I would want: p41=0, p42=0, p43=(0.5)^4. For
the second family (id==2), I would want: p51=0, p52=(0.5)^3, p53=0,
p54=0. (Note that I want the p* variables to have a single value for
each family.)

This is where I am stuck.

2. Later, I want to get back to the -wide- format because I want to use
the p* variables for my ML estimation.

Any suggestions will be appreciated.

Deepankar


On Mon, 2006-06-05 at 14:59 +0100, n j cox wrote:
> It is indeed a good idea to tell us the actual problem.
> 
> You would be well advised to -reshape- your data from wide
> to long. The other way lies mostly analytic awkwardness and
> programming pain. My guess is that your guess about
> needing nested loops is incorrect.
> 
> Assume an identifier -id- as well as the variables
> you report.
> 
> keep id dfsize b*
> reshape long b , i(id) j(childno)
> 
> Then you get which boy it is by
> 
> bysort id (childno) : gen boyno = sum(b == 1)
> 
> Then you can relate -boyno- and -childno-.
> 
> You may then need to -reshape- back.
> 
> Nick
> [email protected]
> 
> DEEPANKAR BASU
> 
> Thanks for your comments. My actual programming problem is much more 
> complicated than just generating p`x'`y' by checking whether `x' == `y'. 
> I had given that example as a prototype of the problem I am facing with 
> the "if" statement. I could re-phrase my question as: can I do what 
> Keith has suggested by using the "if ... else" statement? I give details 
> of my actual problem below.
> 
> I am working with a (survey-based) dataset which has the following 
> variables (related to fertility behaviour) among others:
> 
> 1. dfsize: desired family size
> 2. b1: sex of the first child (1=boy, 2=girl, and "missing" if no first 
> child in the family)
> 3. b2: sex of second child
> ...
> 11. b9: sex of ninth child
> 12. nboy: number of boys alive in the family
> 
> I want to do the following: for each family (observation), I want to see 
> whether the k_th boy is the the N_th child, where k goes from 1 to (N-1) 
> and N is "dfsize" (the desired family size). If it is the case that the 
> k_th boy is the N-th child, then I want to have:
>   p`N'`k'=(0.5)^`N';
> Otherwise, I want to have:
>   p`N'`k'=0
> 
> Note that I will have several nested loops within an outer "if...else" 
> statement. The question is: can I do the whole thing differently, 
> without using the "if...else" statement?
> 
> *
> *   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/

*
*   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–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index