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]
Re: st: Save the percentage of each response category into new variables
From
Nick Cox <[email protected]>
To
[email protected]
Subject
Re: st: Save the percentage of each response category into new variables
Date
Fri, 9 Sep 2011 18:16:25 +0100
-collapse- is a command, not a function. Look at its sibling
-contract- for percents. That said, saving to percents alone will
inhbit most kinds of analysis.
Nick
On Fri, Sep 9, 2011 at 5:14 PM, <[email protected]> wrote:
> 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/