Statalist


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

st: RE: summing variables that have similar names


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: summing variables that have similar names
Date   Tue, 14 Jul 2009 19:36:25 +0100

Richard Goldstein and Martin Weiss have already pointed to -egen- and in
particular its -rowtotal()- function as the basis for a solution. 

What is key to unpicking May's misunderstanding is realising that the
-sum()- function gives (cumulative) sums over observations, not
variables. In fact, that is stated in the online help. 

May's intuition that a loop is needed is correct; it's just that -egen,
rowtotal()- does that loop for you. 

Rowwise operations were reviewed more generally in 

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 
[email protected] 

May Baydoun, PhD

I have several variables that are mutually exclusive. In other words, if
one has a value, the others are missing. I want to sum them into a
single variable so that all observations have a value. They start with
the same few letters, say xyz but have different letters or numbers
thereafter.

I tried: gen newvar=sum(xyz*) but it didn't work out. Is there another
way to do this? I am guessing a loop?


*
*   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–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index