Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.


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

Re: st: Re: calculating proportions


From   Oliver Jones <ojones@wiwi.uni-bielefeld.de>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Re: calculating proportions
Date   Tue, 21 Feb 2012 16:39:24 +0100

Hi Pathemes,

your code looks good to me, but I'm no programmer.

I just have two very minor and highly subjective suggestions:


1.
in the foreach statement use a local to tell Stata the end of the numlist, i.e.
count if sex == 0
local N_male = `r(N)'
foreach i of numlist 1/`N_male' {
and by the way in this case it might be more appropriate to use forvalues insteed, which is the fastest way to loop over consecutive values, such as looping over numbers from 1 to k. (From the Stata Help to foreach)


2.
Indent after the foreach / forvalues statement, i.e.
forvalues i = 1/`N_male' {
	code
	code
	code
}

Best
Oliver


Am 21.02.2012 16:08, schrieb Pathmeswaran:
Hello Ekaterina,

I have developed a solution which works on the small example data set
given in Oliver Jones' response.
My code may not look very professional as I have just started using 'foreach'.
I would very much appreciate some feedback on my code.

Regards,
Pathmes


*************** example code begins **************************

version 8
clear all
set obs 7

gen byte id = _n

gen byte sex = 1
replace sex = 0 in 4/7
label def lbl_sex 0 "male" 1 "female"
label values sex lbl_sex

gen int income = 7
replace income = 10 in 3
replace income = 0 in 4
replace income = 7 in 5
replace income = 9 in 6
replace income = 11 in 7

list

* I have just copied Oliver's code

sort sex
gen idm=_n if sex==0
* idm is just a unique number for each male

gen fa=.
label variable fa "Number of females with higher income"

gen fb=.
label variable fb "Number of females with lower income"

gen fe=.
label variable fe "Number of females with equal income"

* The variables fa, fb&  fe are generated individually for each male
in the following loop
quietly{
foreach n of numlist 1/4 {
egen int feabove = rank(income) if sex==1 | (sex==0&  idm==`n'), field
replace fa= feabove-1 if idm==`n'
drop feabove

egen int febelow = rank(income) if sex==1 | (sex==0&  idm==`n'), track
replace fb=  febelow-1 if idm==`n'
drop febelow

replace fe=3-( fa+ fb) if idm==`n'
}
}

* Converting the numbers to proportions
gen fap=fa/3
label variable fap "Proportion of females with higher income"

gen fbp=fb/3
label variable fbp "Proportion of females with lower income"

gen fep=fe/3
label variable fep "Proportion of females with equal income"

**************** example code ends ***************************
*
*   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/

--
Universität Bielefeld
Fakultät für Wirtschaftswissenschaften
Lehrstuhl für Ökonometrie und Statistik
- -
Bielefeld University
Faculty of Business Administration and Economics
Chair of Econometrics and Statistics
- -
Raum / room:  V9-110
Tel / phone:  +49 (0)521 106 12801
- -
http://www.wiwi.uni-bielefeld.de/oekonometrie.html
---
*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index