Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: Save the percentage of each response category into new variables


From   <[email protected]>
To   [email protected]
Subject   st: Save the percentage of each response category into new variables
Date   Fri, 9 Sep 2011 11:14:40 -0500 (CDT)

Hi all,

I have a panel data where I want to compare the percentage of people belonging to each category of the marital status variable in each group and each time point. There are five categories in the marriage variable and I have two time points and two groups each with say, 50 people.

Basically the question is how to save the output from "by time: tab marstat group" as percentages and into new variables.

The "collapse" function doesn't seem to have an option for calculating the percentage directly, so I did the following:

tab marstat, gen(marstat)

foreach v of varlist marstat1-marstat5{
replace `v'=. if `v'==0
}

collapse (count) marstat1-marstat5, by (group time)

foreach v of varlist marstat1-marstat5 {
gen `v'_pct=`v'/50
}

drop marstat1-marstat5

Is there a better way to achieve this? I need to do this for a huge dataset and I kind of feel there should already exist a way to do this kind of transformation. Thanks a lot in advance for your help!
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index