Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: creating a cumulative variable

From   Sarah Mustillo <>
Subject   Re: st: creating a cumulative variable
Date   Thu, 11 Jul 2002 14:11:33 -0400

Nick and Nick -

I implemented your solution to my cumulative variable problem, which worked very nicely, thank you very much. Beyond that, however, I wanted to thank you for pointing me towards the "foreach" command. I can't believe I have never used this before! Already it has simplified my life tremendously. Thank you.



foreach v of var <varlist> {
	by id : gen C`v' = sum(`v')

You must plug in your <varlist>. It can be in abbreviated form,
using * ? and/or -, etc.

Naturally, you can use your own naming convention, but
this is usually easiest with just some short prefix or suffix
added to the original variable name to give a new name.

This particular problem can also be done with -for-.
My main reservation about -for- is that it doesn't
grow gracefully when extended to more complicated
problems, whereas -foreach- typically does.


*   For searches and help try:

Sarah Mustillo, Ph.D
Center for Developmental Epidemiology
Department of Psychiatry and Behavioral Sciences
Duke University Medical Center

*   For searches and help try:

© Copyright 1996–2015 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index