# Re: st: question for hierarchical (i.e. hh & person) file

 From Susanne und Ulrich Kohler To statalist@hsphsun2.harvard.edu Subject Re: st: question for hierarchical (i.e. hh & person) file Date Thu, 6 Oct 2005 21:17:19 +0200

```Assuming that X1 contains only  0 and 1 and has no missings, you can go on as
follows:

. by UIDhhstr (X1), sort:  gen x2 = x1[_N]

If X1 also has other values than 0 and 1 or has missings, the following should
work:

. by UIDhhstr, sort: gen x2 = sum(X1==1)
. by UIDhhstr: replace x2 = x2[_N]>=1

My general advice here is to learn recoding with -by varlist:- and explicit
subscripts. A possible starting point would be

SJ-2-1  pr0004  . . . . . . . . . . Speaking Stata:  How to move step by: step
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
Q1/02   SJ 2(1):86-102                                   (no commands)
explains the use of the by varlist : construct to tackle
a variety of problems with group structure, ranging from
simple calculations for each of several groups to more
advanced manipulations that use the built-in _n and _N

(or take a look at  pg. 80-86 in "Data Analysis Using Stata" by Frauke Kreuter

many regards
Uli

On Thursday 06 October 2005 19:58, F V wrote:
> I have a very large data-set (i.e. around 60,000
> households, and around 320,000 individuals). say that
> i have three variables: a hh identifier, a person
> identifier, and an X1 variable of interest that
> measures whether a person is X1 or not.
>
>
> UIDhhstr	X1	UIDpersonstr
> 01101021	1	0110102101
> 01101021	0	0110102102
> 01101021	0	0110102103
> 01101031	1	0110103101
> 01101031	0	0110103102
> 01101031	0	0110103103
> 01101031	0	0110103104
> 01101031	0	0110103105
> 01101031	0	0110103106
>
>
> say I wanted to tell Stata: gen a variable X2 such
> that it takes the value of 1 if for any member of a
> household X1==1. how can i do this in stata? after
> generating X2, then i could collapse the data down to
> the household level.

--
kohler@wz-berlin.de
+49 (0)30 36991040
*
*   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/
```