Statalist The Stata Listserver


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

st: calculating totals and crossing them with all other variables


From   Stephanie Premji <[email protected]>
To   [email protected]
Subject   st: calculating totals and crossing them with all other variables
Date   Fri, 24 Mar 2006 09:51:16 -0800 (PST)

Hi everyone, 

I'm just learning Stata and I have a database that has
columns for age, sex, etc. and columns for number of
cases, number of days, etc. (workers' compensation
data). My age column for instance has the following
categories:
15-24
25-44
45+

And my sex column has the following:
Men
Women

All the variables are crossed together, which means
that line 1 is 15-24, Men, Nb of cases, line 2 is
25-44, Men, Nb of cases, line 3 is 45+, Men, Nb of
cases, line 4 is 15-24, Women, Nb of cases, etc.
(I have 5 such variables and 4 variables with data
such as number of cases, days, etc. so 9 columns). I
don't have lines for the combinations that produce 0
cases (data was given to me that way).

I am trying to calculate totals and cross them with
all other variables:
Ex of a line: Total-Age, Men, number of cases, etc.
Ex of another line: 15-24, Total-sex, number of cases,
etc.

This is what I came up with but it doesn't work...For
some reason I end up with duplicates even though my
database doesn't have any to begin with. I'm wondering
if there is an easier way to do this: 

For age (GrAge)
* I generate variables with the sum of all
combinations:
sort  Region CTI3 sexe Gr_prof
by Region CTI3 sexe Gr_prof : egen
tot_nb_cases=sum(Nb_cases)
by Region CTI3 sexe Gr_prof : egen
tot_nb_days=sum(Nb_days)
by Region CTI3 sexe Gr_prof : egen
tot_nb_perma=sum(Nb_permanent)
by Region CTI3 sexe Gr_prof : egen
tot_sum_perma=sum(Sum_permanent)

* For each combination, I create a fake variable (var)
that is equal to 1 for the last line:
sort  Region CTI3 sexe Gr_prof
by Region CTI3 sexe Gr_prof : gen var=1 if _n==_N

* I create another line exactly the same
expand 2 if var==1

* For each combination, I re-create a variable var
that is equal to 1 for the last line 
sort  Region CTI3 sexe Gr_prof GrAge
drop var
by Region CTI3 sexe Gr_prof : gen var=1 if _n==_N

* I then transform the last line:
replace GrAge=1 if var==1
replace Nb_cases=tot_nb_cases if var==1
replace Nb_days=tot_nb_days if var==1
replace Nb_permanent=tot_nb_perma if var==1
replace Sum_permanent=tot_sum_perma if var==1

drop  var tot_sum_perma tot_nb_perma tot_nb_days
tot_nb_cases

Does anyone have a suggestion? Thanks, I really
appreciate it!

Stephanie Premji
PhD Candidate
Environmental Sciences
Montreal, Quebec, Canada
 
 

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 
*
*   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