Statalist The Stata Listserver


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

st: RE: Counting Ulcers


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: Counting Ulcers
Date   Thu, 7 Sep 2006 22:44:20 +0100

You have two choices: you keep with this long-legged data 
structure or you -reshape- to a wide structure. 

A -reshape- would be a really good idea if most of your questions 
are about individuals, not legs. 

If you keep with this data structure, you can still do it. 

The principle is that under -by id:-, subscripts are determined within
groups of -id-. So [1] refers to the first leg and [2] to the second leg. 
If you are counting only, it doesn't match which leg you count first. 
For other purposes, you could specify -bysort id (leg)- and get the 
left leg first always (if -leg- is a string variable). 

     +-----------------+
     | id     leg   vi |
     |-----------------|
  1. | 40   right    . |
  2. | 40    left    . |
  3. | 46   right    1 |
  4. | 46    left    1 |
  5. | 47    left    0 |
     |-----------------|
  6. | 47   right    1 |
  7. | 48   right    . |
  8. | 48    left    . |
  9. | 55    left    1 |
 10. | 55   right    1 |
     |-----------------|
 11. | 57   right    0 |
 12. | 57    left    1 |
     +-----------------+

. bysort id : gen Nlegs = inrange(vi[1],1,.) + inrange(vi[2],1,.)

Here -inrange()- is 1 if 1 <= vi < . and 0 otherwise. Adding them 
up counts legs with ulcers. 

. l

     +-------------------------+
     | id     leg   vi   nlegs |
     |-------------------------|
  1. | 40   right    .       0 |
  2. | 40    left    .       0 |
  3. | 46   right    1       2 |
  4. | 46    left    1       2 |
  5. | 47    left    0       1 |
     |-------------------------|
  6. | 47   right    1       1 |
  7. | 48   right    .       0 |
  8. | 48    left    .       0 |
  9. | 55    left    1       2 |
 10. | 55   right    1       2 |
     |-------------------------|
 11. | 57   right    0       1 |
 12. | 57    left    1       1 |
     +-------------------------+

But when we count, we want to count individuals, 
not legs. This is where -egen, tag()- comes in. 

. egen tag = tag(id)

. tab nlegs if tag

      nlegs |      Freq.     Percent        Cum.
------------+-----------------------------------
          0 |          2       33.33       33.33
          1 |          2       33.33       66.67
          2 |          2       33.33      100.00
------------+-----------------------------------
      Total |          6      100.00

Nick 
n.j.cox@durham.ac.uk 

Raphael Fraser
 
> Below is a snippet of my data. Each subject has 2 observation where vi
> indicates whether an ulcer is present or not. I would like to count
> the number of patients with ulcers on one leg only and those with
> ulcers on both legs.
> left=1, right=0
> 
> id     leg   vi
>       40   right    .
>       40    left    .
>       46   right    1
>       46    left    1
>       47    left    0
>       47   right    1
>       48   right    .
>       48    left    .
>       55    left    1
>       55   right    1
>       57   right    0
>       57    left    1

*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index