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

st: RE: Construct a new variable


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: Construct a new variable
Date   Thu, 30 Jan 2003 21:36:22 -0000

Rodrigo Brice�o
>
> Dear Stata Users: I need some assistance in the
> construction of a new
> variable. I really don't know how to construct it, because
> the sintaxis is a
> little complex for my knowledge of Stata. I will thank a
> lot everybody who
> can help me. Lets take an Example:
>
> Household       MN       SEX      AGE     REL     CIVIL     SS1  SS2
> 1001008021	1	1	37	1	1	1       1
> 1001008021	2	2	32	2	1	.        3
> 1001008021	3	1	12	3	6	.
> 1001008021	4	1	5	3	6	.        4
> 1001008031	1	2	42	1	1	.        3
> 1001008031	2	2	25	2	1	1       1
> 1001008031	3	2	18	3	6	1       2
> 1001008031	4	2	20	3	6	1       2
> 1001008031	5	1	4	3	6	1       2
>
>
> I want to generate the variable SS2 with those values. The
> logic is the
> next:
>
> 1. SS2 will have an 1 if SS1=1 and if the person in that
> household is
> married or have sons (civil=1 or if the person have
> dependants: rel=3).

egen dependantsinhh = sum(rel==3), by(household)
gen ss2 = 1 if (ss1 == 1) & ((civil == 1) | dependantsinhh)

> 2. SS2 wil have a 2 if SS1=1 and if the person if a son
> (rel=3) and age>=10.

replace ss2 = 2 if ss1 == 1 & rel == 3 & age >= 10

But I am not sure that you mean that literally. You may
mean that someone in the family has ss1 == 1 etc.

egen ss1is1inhh = sum(ss1 == 1), by(household)

replace ss2 == 2 if ss1is1inhh & rel == 3 & age >= 10

> 3. SS2 will have a 3 if rel=1 or rel=2 have SS1=1, and SS1
> for this person
> is missing. We are trying to assign values to the husbands
> or wifes whose
> partner have SS=1.
> 4. SS2 will have a 4 if reljefe=1 or 2, SS=1 (for any of
> them or both) and
> they have chldren less or equal to 5 (rel=3 and age<=5).

etc.

Basically,

1. if conditions refer only to an individual it's
a matter of constructing -if- conditions using
operators.

2. if conditions depend on the characteristics
of other members of the same family, you probably
need to construct variables for that beforehand.

There are FAQs in this territory:

How do I create variables summarizing for each
individual properties of the other members of a group?
http://www.stata.com/support/faqs/data/members.html

How do I create a variable recording whether any members
of a group (or all members of a group) possess some characteristic?
http://www.stata.com/support/faqs/data/anyall.html

Nick
[email protected]

*
*   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