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

Re: st: how do i increment a counter


From   Neil Shephard <[email protected]>
To   [email protected]
Subject   Re: st: how do i increment a counter
Date   Wed, 31 Aug 2005 20:21:32 +0000

On Wed, 31 Aug 2005 12:03:52 -0700
"aine dooley" <[email protected]> wrote:

> I am finding in Stata that there does not seem to be an easy way to 
> increment a counter despite countless trys and readings of the manal.
> 
> I want to do something llike the following, this is just an example:
> 
> generate byte total_sum =0
> 
> for each variable of varlist {
> if age > 50
>     total_sum = total_sum + age
> }
> 
> Stata doesn't seem to allow the if... then construct unless it is in a 
> program. Although I have written a simple program (crazy that I have to do 
> this) passing arguments, I still can't get it to work.
> 
> I must be missing something.

If you want the running total of each variable in your varlist (which you haven't specified in your example, so I've assumed you have variables a, b and c), you could try...

foreach x of varlist a b c{
     gen total_sum_`x' = sum(`x') if(age > 50)
}

Alternatively if you just want total_sum_a etc. to contain the total of all a's where age > 50 you could try...

foreach x of varlist a b c{
     egen total_sum_`x' = sum(`x') if(age > 50)
}

More info on what you are trying to achieve would help, and you may benefit frommanual [U] 14. Language Syntax, and the help pages...

-man foreach-
-man forval-
-man if-
-man -egen-

HTH's

Neil
-- 

*
*   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