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 the variable based on other variables


From   Nick Cox <[email protected]>
To   "[email protected]" <[email protected]>
Subject   Re: st: sum the variable based on other variables
Date   Tue, 25 Mar 2014 09:01:06 +0000

You are using -egen- to create variables, each of which just holds a
constant. That is not necessary. The sum is left in memory as r(sum)
after -summarize-.

I don't know what you want to do with your millions of sums, so this
code is only a start.

forv i=1/3 {
  forv j=1/3 {
  if `i'~=`j' {
    sum  v`i' if v`i'==v`j', meanonly
    di r(sum)
}
}
}

Nick
[email protected]

On 25 March 2014 04:19, wanhaiyou <[email protected]> wrote:

> I want to creat the sum of one variable based on other variables.For example,
> I have the following dataset
> v1 v2 v3
> 1 1 0
> 0 0 1
> 1 0 1
> 1 1 1
> 0 1 1
> 0 1 0
>
> If v1=v2, then sum the corresponding value of v1
> v1 v2
> 1 1   equal
> 0 0   equal
> 1 0   not equal
> 1 1   equal
> 0 1   not equal
> 0 1   not equal
> if v1=v3, then sum the corresponding value of v1
> ....
>
> if v3==v1,then sum the corresponding value of v3
> if v3=v2,then sum the corresponging value of v3
>
> I have written the following codes
>
> forv i=1/3 {
>   forv j=1/3 {
>   if `i'~=`j' {
>   egen vsum`i'`j'=total(v`i') if v`i'==v`j'
> }
> }
> }
>
> However, I have 5000 variables but the max number of variables is 32,767.
> Therefore, this codes cannot be used.
*
*   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