Keith and Nick,
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?
Thanks,
Deepankar
----- Original Message -----
From: Nick Cox <[email protected]>
Date: Monday, June 5, 2006 6:48 am
Subject: RE: st: Problem with "If" statement
> In addition to Keith's solution, note that there is an
> FAQ on the subject at
>
> FAQ . . . . . . . . . . . . . . . . . . . . . if command vs.
> if qualifier
> . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
> . J. Wernow
> 6/00 I have an if command in my program that only seems
> to evaluate the first observation, what's going on?
>
> http://www.stata.com/support/faqs/lang/ifqualifier.html
> -search if- would point you to this.
>
> Nick
> [email protected]
>
> Keith Dear
>
> > You don't need either sort of -if-. Try it like this instead:
> > gen byte p`size'`alive' = cond(`size'==`alive', 100, 10)
> > Moreover there is then no need to initialise the pxy variables.
>
> Deepankar
>
> > >I am facing problems using the "if" statement (not the "if"
> > >qualifier) because the expression in the "if" statement has a
> > >variable. Hence, the commands are being executed taking only
> the
> > >first observation of the variable. What is the way to execute
> the
> > >commands for all the observations? I give a details of a
> prototype
> > >(simplified) problem below.
> > >
> > >I have two variables x and y. Each can take values 1 or 2. And
> I
> > >have four more variables p11, p12, p21 and p22. I want to go
> down
> > >each observation of x, compare it to y and if they are equal
> then
> > >make p`x'`y'=100, otherwise make p`x'`y'=10.
> > >
> > >To do the above task, I write the following program:
> > >++++++++++++++++++++++++++++++++++++++
> > >capture program drop rpc
> > >program define rpc
> > >version 8.0
> > >args size alive
> > >if `size'==`alive' {
> > > replace p`size'`alive'=100
> > >}
> > >else {
> > > replace p`size'`alive'=10
> > >}
> > >end
> > >++++++++++++++++++++++++++++++++++++
> > >
> > >I run this program and then run the following do-file:
> > >
> > >+++++++++++++++++++++++++++++++++++
> > >set more 1
> > >version 8.0
> > >local i 1
> > >while `i' <=2 {
> > > local j 2
> > > while `j' <= `i' {
> > > gen p`i'`j'=0
> > > local j = `j' + 1
> > > }
> > > local i = `i' + 1
> > >}
> > >
> > >rpc x y
> > >
> > >+++++++++++++++++++++++++++++++++++++
> > >
> > >
> > >My problem is that I cannot get the program to go through each
> > >observation on x and y. It takes the first observation of x and
> y
> > >and stops there. It does not move to the next observation.
>
>
> *
> * 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/