Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: Adding variables with common prefix


From   "David Harrison" <[email protected]>
To   <[email protected]>
Subject   st: RE: Adding variables with common prefix
Date   Tue, 30 May 2006 13:21:27 +0100

You need:

foreach v of varlist var* { 
etc

An alternative way would be to generate a new variable containing the
sum of these variables, and look at the first observation of  the new
variable. This is quicker to program, but presumably would be more
computationally intensive.

E.g.

egen vsum = rsum(var*)
scalar answer = vsum[1]

David

-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of R.E. De Hoyos
Sent: 30 May 2006 13:10
To: [email protected]
Subject: st: Adding variables with common prefix


I have a large list of variables with a common prefix, say -var- (var1,
var2, var3...). I want to generate a scalar with the sum of these
variable's first observation. I tried the following loop, but for some
reason that I can't see it is not working, it returns an invalid syntax
error message:

local sum = 0
foreach v of var* {
    local sum = `sum' + `v'[1]
}
scalar answer = `sum'

What is wrong with this code? Is there an alternative way of adding up
variables with a common prefix?

Rafa
*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/

______________________________________________________________________
This email has been scanned by the MessageLabs Email Security System.
For more information please visit http://www.messagelabs.com/email
______________________________________________________________________

______________________________________________________________________
This email has been scanned by the MessageLabs Email Security System.
For more information please visit http://www.messagelabs.com/email 
______________________________________________________________________

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   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