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]

Re: st: sum of count using a loop


From   Keniajin Wambui <[email protected]>
To   [email protected]
Subject   Re: st: sum of count using a loop
Date   Fri, 2 Aug 2013 18:01:13 +0300

Hi Nick and Sergiy;
Both tabm and matrix solution worked perfectly for me.
Thanks  big way

On Wed, Jul 31, 2013 at 11:44 AM, Nick Cox <[email protected]> wrote:
> Sergiy Radyakin and I responded to this with, respectively, code and a
> pointer to an existing program.
>
> What remains of some interest is how to program it yourself. I think I
> see Keniajin's basic idea, and here is one way to take it forward.
>
> Set up a matrix for the counts with initial values 0.
>
> mat counts = J(4,1,0)
>
> Loop over the possible values and the possible variables. Count
> occurrences, adding the result to the count so far.
>
> qui forval i = 1/4 {
>       foreach v of var *source {
>              count if `v' == `i'
>              mat counts[`i', 1] = counts[`i', 1] + r(N)
>      }
> }
>
> mat li counts
>
> Nick
> [email protected]
>
>
> On 30 July 2013 15:12, Keniajin Wambui <[email protected]> wrote:
>> I have a data set with a unique pid and a list of 32 variables with
>> the same suffix i.e maize_source, peas_source, sukuma_source,
>> cabbage_source.....
>> All the 32 variables have  option 1,2,3 or 4. I want to do a sum of
>> all the counts of each option for across all the variables using stata
>> 12 on windows 7
>> i.e
>> option    total
>> 1           4000
>> 2             568
>> 3          1200
>> 4          14000
>>
>> I am trying
>>
>> foreach var of varlist  maize_source - peas_source {
>> count if `var'==1
>>     replace total=_N+_n if `var'==1
>> countif `var'==1
>>         }
>> but its giving a count with a wrong total
>>
>> I will appreciate the support
>> --
>> Mega Six Solutions
>> Web Designer and Research Consultant
>> Kennedy Mwai
>> 25475211786
>> *
>> *   For searches and help try:
>> *   http://www.stata.com/help.cgi?search
>> *   http://www.stata.com/support/faqs/resources/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/faqs/resources/statalist-faq/
> *   http://www.ats.ucla.edu/stat/stata/



-- 
Mega Six Solutions
Web Designer and Research Consultant
Kennedy Mwai
25475211786
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


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