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

st: re: simple tab needed but multiple records

From   Kit Baum <>
Subject   st: re: simple tab needed but multiple records
Date   Fri, 5 Oct 2007 17:45:51 -0400

Joseph said

 I have a dataset of x-ray records with multiple records per
patient.  The records consist of id, age, and sex and I need to know how
many persons had an x-ray when they were between the age of 17 and 25
AND when when they were over 30.  My attempts have been crude and I know
there has to be a simple yet elegent way to do this.

I think this should work, without the necessity of reshaping:

bysort id: gen early = inrange(age, 17, 25)
by id: gen late = age > 30
by id: gen both = cond(_n==_N, (sum(early) & sum(late)) , .)
count if both == 1

To test,

set obs 1000
g id=mod(_n,100)+1
g age=40*uniform()

Kit Baum, Boston College Economics and DIW Berlin
An Introduction to Modern Econometrics Using Stata:

*   For searches and help try:

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