Statalist The Stata Listserver

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

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

From   n j cox <>
Subject   Re: RE: st: Problem with "If" statement
Date   Mon, 05 Jun 2006 14:59:45 +0100

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.



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:
Otherwise, I want to have:

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:

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