Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: RE: how to create a dummy with information across rows

From   Joe Canner <>
To   "" <>
Subject   st: RE: how to create a dummy with information across rows
Date   Fri, 6 Dec 2013 01:10:21 +0000


Personally, I think it would be easier to work the data with all HH members in one row, especially since that is how the data was before you reshaped it. Something like this could get you started:

forvalues x=1/25 {
gen teenmom'x'=0
gen kidsofteenmom'x'=""
forvalues y=1/25 {
   replace teenmom'x'=1 if mother'y'=='x' &  inrange(age'x',13,19)
   replace kidsofteenmom'x'=kidsofteenmom'x'+"'y' " if mother'y'=='x' & inrange(age'x',13,19)

When this is done you will have teenmom1-teenmom25 which indicate whether the given HH member is a teen mother. (I took the term "teen" literally; you can choose whatever ages you want.) You will also have string variables kidsofteenmom1-kidsofteenmom25 which will have a list of the children. You don't indicate what you mean by "linking" the mother and child but hopefully this will get you at least part of the way there.

Of course, it is also possible to do this with the reshaped data, using -bysort-:

gen teenmom=0
gen kidsofteenmom=""
forvalues x=1/25 {
bysort hh: replace teenmom=1 if mother['x']==_n & inrange(age,13,19)
bysort hh: replace kidsofteenmom=kidsofteenmom+"'x '" if mother['x']==_n & inrange(age,13,19)

There may be better ways to do this that eliminate the outer loop in each case, but I can't think of any at the moment; perhaps others can improve on this.

(Note: you will need to substitute the correct leading single quote for all of the macro references. I am writing this on a tablet that only has one kind of single quote.)

Joe Canner
Johns Hopkins University School of Medicine
From: [] on behalf of ana mylena aguilar []
Sent: Thursday, December 05, 2013 6:11 PM
To: statalist
Subject: st: how to create a dummy with information across rows

I have a datatset with information by households ( rows) and
individual household members in the columns.Household size can go all
the way until 25. Therefore, the format for x number of variables is
for example age1..age25.I reshaped the dataset to have household
members within each household in each row as below. However, I need to
contruct a variable that require using infomation across rows.
hh roster line age     rel with hh head            child's mother line
(only <14 years old)
1     1              35       household head
1     2              15        daughter                                  missing
1     3              10        daughter                                  1
1     4               1         grandson                                 2
I need to create a dummy variable to identify a teen mother in the
household. In this case, there is one (line 2). The household is
formed by a mother, two daughters ( age 15 and 10) and a grandson. The
older daughter has a 1 year old child. Every child under 14 has
information on who is his/her biological mother ( roster line of the
mother). But I would like to link the information of the chidl with
the young mother.   Do I need to re-reshape or a loop would work?

Any help would be great, thanks
*   For searches and help try:

*   For searches and help try:

© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index