Statalist


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

st: RE: Simple tab needed but multiple records


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: Simple tab needed but multiple records
Date   Sun, 7 Oct 2007 18:51:29 +0100

Maarten is right. If data are like this 


id   female likes_cats likes_dogs 
--   ------  ---------- ----------
1      0         0          0
2      1         1          0 
3      0         1          0 
...

in which each person is represented by only one
observation (record), then it's easy to count 
how many people satisfy two (or indeed more)
different conditions. 

e.g. -count if female & likes_cats & likes_dogs-

Nor are indicators (dummy, logical, Boolean 
variables) essential as we can always use explicit 
true or false conditions instead. 

This kind of structure is I think also 
assumed by Carlo Lazzaro in his posting in 
this thread. 

However, 

This is not the structure Joseph 
has and it would be unnatural to force
his dataset into a different structure
given the irregularity of dates that 
he presumably has. 

Hence Kit's proposal is closer to, indeed
on, the mark. 

What's more, this is essentially the same 
problem as that posted by Paul O'Brien
just the same day and already replied to
with code 

<http://www.hsph.harvard.edu/cgi-bin/lwgate/STATALIST/archives/statalist.0710/date/article-161.html>

The class of problem is this: 

1. There is some kind of grouping, most obviously into panel 
or longitudinal data. For concreteness, we'll talk "panels" and 
remember that the idea is more general. (Indeed, no 
kind of time basis, regular or irregular, is essential here.) 

2. Hence, multiple observations for each panel
are likely. 

3. Some question arises about panels that requires 
comparison of different observations. 

4. For each observation, we can say whether 
it satisfies some condition. That is a true-or-false
calculation. 

5. We need to summarise that true-or-false result
over all observations in each panel. This can be done with 
-egen, by(<panelid>)- or -by <panelid>: egen- or 
-by <panelid>: gen-. 

6. Then we need to combine information on different
conditions using logical operators such as &, | and !. 

7. Finally, we must count panels, not individuals. 

Nick 
[email protected] 

Kit Baum 
---------------------------------------------
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()

Maarten Buis
--------------------------------------------
This kind of problem usually becomes a lot easier when you first use
-reshape- to put the data into wide format.

Joseph Wagner
--------------------------------------------
> 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.  

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