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.

# Re: st: Re: calculating proportions

 From Nick Cox To statalist@hsphsun2.harvard.edu Subject Re: st: Re: calculating proportions Date Tue, 21 Feb 2012 18:24:14 +0000

```The short answer is No if by "rank" you mean -egen-'s -rank()- function.

But I would not even look in this direction without working out some
rules first. Let's say that ~ is a fuzzy equals.  It won't be
transitive. Do you feel comfortable with 500 ~ 451 and 501 ~ 500 but
501 !~ 451? Remember that your code is likely to hinge on binary
comparisons, but they can't be guaranteed consistent.

I think there are at least two more positive answers here.

1. People in this field don't bother with looking at exact matches, as
they know they don't mean much.

2. If you want to go further in this direction, bin your incomes first
and then re-do the analysis. But you will be throwing away detail.

Nick

On Tue, Feb 21, 2012 at 5:53 PM, Ekaterina Hertog
<ekaterina.hertog@sociology.ox.ac.uk> wrote:
> Dear all, an additional question, is there any way to make rank relax its
> definition of what is equal, what is above and what is below a given figure?
> At the moment using the code that Pathmes and Oliver suggested I have got
> for every man the proportion of the total sample of women who earns as much,
> who earns more and who earns less than him.
> But the definition of equal income is very strict. Say for a man who earns
> 500 a woman who makes 499 will be judged to earn less than him and a woman
> who earns 501 will be judged to earn more. If possible I believe for my
> analysis it would be more useful to assume that anyone who earns +50 or -50
> of own income has the same income. Say for a man who earns 500 women who
> earn at least 451 and no more than 549 will be judged to have the same
> income. the above and below definitions will need to be adjusted
> accordingly.
> I looked at the options for rank, but they do not seem to provide an answer.
> Could anything be possibly done with cond?
> thanks a lot for all the help so far,
> katya
>
>
>
> On 21/02/2012 17:22, Ekaterina Hertog wrote:
>>
>> Dear Pathmes and Oliver,
>> thanks a lot, great code and it solves my problems! And I learned more
>> than 3 things from it
>> Just a small hitch:
>> converting numbers to proportions syntax:
>>
>> gen fap=fa/`N_female'
>>
>> gives an error message: 'invalid syntax'.
>> I went around it by creating a variable which is a constant and equals a
>> total number of women in the population and using it instead of the
>> local macro. I clumsy way to do it.
>> katya
>>
>> On 21/02/2012 16:45, Pathmeswaran wrote:
>>>
>>> Thanks Oliver.
>>>
>>> With your help I was able to learn 3 things on my first day in statalist!
>>>
>>>    forvalues
>>>    `r(N)'
>>>    indentation in foreach/ forvalue loops
>>>
>>> *************** 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 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{
>>>        count if sex == 0
>>>        local N_male = `r(N)'
>>>
>>>        count if sex == 1
>>>        local N_female = `r(N)'
>>>
>>>        forvalues i = 1/`N_male' {
>>>                egen int feabove = rank(income) if sex==1 | (sex==0&
>>> idm==`i'), field
>>>                replace fa= feabove - 1 if idm==`i'
>>>                drop feabove
>>>
>>>                egen int febelow = rank(income) if sex==1 | (sex==0&
>>> idm==`i'), track
>>>                replace fb=  febelow - 1 if idm==`i'
>>>                drop febelow
>>>
>>>                replace fe=`N_female' - ( fa+ fb) if idm==`i'
>>>        }
>>> }
>>>
>>> * Converting the numbers to proportions
>>>
>>> gen fap=fa/`N_female'
>>> label variable fap "Proportion of females with higher income"
>>>
>>> gen fbp=fb/`N_female'
>>> label variable fbp "Proportion of females with lower income"
>>>
>>> gen fep=fe/`N_female'
>>> label variable fep "Proportion of females with equal income"
>>>
>>> **************** example code ends ***************************
>>>
>>>
>>> With regards,
>>>
>>> Pathmes
>>>
>>>
>>> Prof A Pathmeswaran
>>> Professor in Public Health
>>> Faculty of Medicine, University of Kelaniya
>>> Ragama, Sri Lanka
>>> +94 11 295 3411
>>>
>>> "Not everything that counts can be counted and not everything that can
>>> be counted counts"
>>>
>>>
>>>
>>>
>>>
>>> On 21 February 2012 21:09, Oliver Jones<ojones@wiwi.uni-bielefeld.de>
>>> wrote:
>>>>
>>>> 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
>>>>
>>>>
>>> *
>>> *   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/
>
>
> *
> *   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/
```