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

# Re: st: egen rowtotal

 From Nick Cox To statalist@hsphsun2.harvard.edu Subject Re: st: egen rowtotal Date Sun, 15 Jul 2012 21:33:02 +0100

Steve Samuels' suggestion of recoding all the 99s to missing is
evidently a good one. But whether you do it with -recode- (as here) or
with -mvdecode- (as he suggested), no -foreach- loop is necessary in
either case. Both commands take a varlist.

Nick

On Sun, Jul 15, 2012 at 8:43 PM, Nikolaos Pandis <npandis@yahoo.com> wrote:
> Thank you all for your help
>
> I ended up using the following:
> **************************************
> foreach myvar of varlist  structured- reg_num{
>   recode `myvar' 99=0
> }
> egen sum = rowtotal(structured- reg_num )
> ********************************************
> the results make sense (the value of zero is not a problem as I am calculating a score from the sum of 1s, 2s and 3s)
>
>
> From: Nick Cox <njcoxstata@gmail.com>
> To: statalist@hsphsun2.harvard.edu
> Sent: Sunday, July 15, 2012 8:48 PM
> Subject: Re: st: egen rowtotal
>
> I think that if you choose the -egen- turning then the road ahead is
> blocked, short of an enormously complicated -if- condition. But it  is
> easy enough to work this out from first principles.
>
> gen sum = 0
>
> qui foreach v of var structured-reg_num {
>     replace sum = sum + `v' if inlist(`v', 1, 2, 3)
> }
>
> replace sum = . if sum == 0
>
> For a more systematic discussion, see
>
> SJ-9-1  pr0046  . . . . . . . . . . . . . . . . . . .  Speaking Stata: Rowwise
>         (help rowsort, rowranks if installed) . . . . . . . . . . .  N. J. Cox
>         Q1/09  SJ 9(1):137--157
>         shows how to exploit functions, egen functions, and Mata
>         for working rowwise; rowsort and rowranks are introduced
>
>
> Nick
>
> On Sun, Jul 15, 2012 at 6:08 PM, Nikolaos Pandis <npandis@yahoo.com> wrote:
>
>> I am using the following command to get a row total for 16 variables:
>>
>> egen sum = rowtotal(structured- reg_num )
>>
>> the possible values per variable are 1, 2, 3 or 99  and I would like the row total by adding only the 1s, the 2s and the 3s and to excluded the 99s.
*
*   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/