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

# Re: st: Generate a variable using Freq. of one variable and Percent of another variable‏

 From Eric Booth To "" Subject Re: st: Generate a variable using Freq. of one variable and Percent of another variable‏ Date Wed, 20 Jul 2011 20:27:23 +0000

```<>

Try this example in a do-file:

*********************!
clear
**gen fake data q4 and q5:
set obs 450
forval n = 4/5 {
g q`n'a = int(runiform()*8)+2
g q`n'b = int(runiform()*7)+3
ta q`n'*
}

**create string proportion variable:
forval n = 4/5 {
g q`n'_num = .
levelsof q`n'b, loc(val)
foreach v in `val' {
egen q`n'_`v'a = count(q`n'a) if q`n'a==`v'
egen q`n'_`v'b = max(q`n'_`v'a)
replace q`n'_num = q`n'_`v'b if `v'==q`n'a
drop q`n'_`v'a q`n'_`v'b
}
recode q`n'_num (.=0)
count if !mi(q`n'b)
g q`n'_num2 = string(q`n'_num) + " / `r(N)'"
}
l in 1/10

********************!
- Eric
__
Eric A. Booth
Public Policy Research Institute
Texas A&M University
ebooth@ppri.tamu.edu

\
On Jul 20, 2011, at 3:03 PM, Jonathan Grondin wrote:

> Stata/MP 10.1 for Windows
> Born 10 Jun 2010
> ado -  10 Jun 2010
>
>
> Hi there,
>
> I have conducted a questionnaire and every question can be answered by a value of 1 through 10. I have two groups who have taken the questionnaire, group "a" and group "b" and I have divided every question into 2 variables.
> Question four would be divided this way: "q4a" and "q4b".
> For every student and every question, I wish to compare the proportion of student from one group who provided the same answer as the students in the other group.
> For example: Sam, a student in group "a", has answered "6" to question four. In group "b", out of ten students, three student have answered "6" to the same question. Thus, Sam's proportion for question four would be "3/10".
>
> Now, I want to generate a variable that would hold these proportions as values.
>
>
> Here is a tabulate of the two groups on question four to explain better my need. Following, I have provided an example of the variable I would like to generate.
>
>
> . tab  q4a
>
>        q4a |      Freq.     Percent        Cum.
> ------------+-----------------------------------
>           1 |         12        2.78        2.78
>           2 |         72       16.71       19.49
>           3 |        109       25.29       44.78
>           4 |        122       28.31       73.09
>           5 |         67       15.55       88.63
>           6 |         26        6.03       94.66
>           7 |          9        2.09       96.75
>           8 |          8        1.86       98.61
>           9 |          4        0.93       99.54
>          10 |          2        0.46      100.00
> ------------+-----------------------------------
>       Total |        431      100.00
>
>
> . tab  q4b
>
>       q4b |      Freq.     Percent        Cum.
> ------------+-----------------------------------
>           2 |          2        0.78        0.78
>           4 |          7        2.72        3.50
>           5 |         18        7.00       10.51
>           6 |         25        9.73       20.23
>           7 |         31       12.06       32.30
>           8 |         37       14.40       46.69
>           9 |         48       18.68       65.37
>          10 |         89       34.63      100.00
> ------------+-----------------------------------
>       Total |        257      100.00
>
>
>
> I would like to get a variable like this
>
>
>
> . tab  q4a
>
>        q4a |      Freq.     Percent        Cum.
> ------------+-----------------------------------
>           0 |         12        2.78        2.78
>       2/257 |         72       16.71       19.49
>           0 |        109       25.29       44.78
>       7/257 |        122       28.31       73.09
>      18/257 |         67       15.55       88.63
>      25/257 |         26        6.03       94.66
>      31/257 |          9        2.09       96.75
>      37/257 |          8        1.86       98.61
>      48/257 |          4        0.93       99.54
>      89/257 |          2        0.46      100.00
> ------------+-----------------------------------
>       Total |        431      100.00
>
>
> Any suggestions ?
>
> Thank you so much
>
>
> Jonathan
> *
> *   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/

*
*   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/
```