Mean/Median/SD calculation help

6 views
Skip to first unread message

gopilth

unread,
Sep 17, 2010, 6:13:26 PM9/17/10
to Stata Users Forum
Hi,

I have 100 subjects with 6 visits over a period of 6 month and their
weight recorded at each visit. how can I calculate mean and SD of each
subject's weight (all 6 visits),for all 100 subjects. Any help is
appreciated.

Also,

I have 100 subjects with 6 visits over a period of 6 months. some may
have less then 6 visits. how can i calculate the total subjects with
more than or equal to 2 visits and also the median.

Eric A. Booth

unread,
Sep 18, 2010, 9:03:12 AM9/18/10
to gopilth, Stata Users Forum
<>

It's easier to answer these questions if you provide an example/snippet of your data.
I assume your data is in long format, if so, section 1 below creates some fake data that matches your description (unbalanced panels of 100 subjects with somewhere between 1 and 6 visits).

For your 2 questions, the trick is using the -bysort- prefix along with -egen- to calculate a "panel" mean, sd, etc.
See -help egen- and -help bysort- for more information:


*******************! Paste the code below into the do-file editor and click "do"
clear
//1. create some fake date data to work with//
set obs 100
g subject = _n
expand 6 if inrange(subject, 1, 20)
expand 4 if inrange(subject, 71, 98)
expand 2 if inrange(subject, 31, 69)
expand 1 if inrange(subject, 21, 30)
sort subject
**at this point you should have 100 subjects
**with somewhere between 1 and 6 visits

bys subject: g date = date("01/01/2008", "MDY")+ _n
format date %td
bys subject: g weight = 125+int((200-125+1)*runiform())

/*
Question 1: "How can I calculate mean and SD of each


subject's weight (all 6 visits),for all 100 subjects."

*/

bys subject: egen mean_weight = mean(weight)
bys subject: egen sd_weight = sd(weight)

/*
Question 2: "Some may have less then 6 visits.
How can i calculate the total subjects with


more than or equal to 2 visits and also the median"

*/

bys subject: g number_of_visits = _n

**count and median from -summary-**
sum number_of_visits, d
di "Median Visits is: " `r(p50)'
count if number_of_visits >= 2

***or using -tabstat-***
tabstat number_of_visits, stat(median min max N)
*******************!

~ Eric

__
Eric A. Booth
Public Policy Research Institute
Texas A&M University
ebo...@ppri.tamu.edu
Office: +979.845.6754

> --
> You received this message because you are subscribed to the Google Groups "Stata Users Forum" group.
> To post to this group, send email to stata-us...@googlegroups.com.
> To unsubscribe from this group, send email to stata-users-fo...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/stata-users-forum?hl=en.
>


Reply all
Reply to author
Forward
0 new messages