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

From |
"aine dooley" <acd4983@hotmail.com> |

To |
statalist@hsphsun2.harvard.edu |

Subject |
Re: st: how do i increment a counter |

Date |
Wed, 31 Aug 2005 14:22:11 -0700 |

Thanks so much for taking the time to reply.

You are right, my example wasn't the best.

What I am actually trying to do is loop through a list of 6500 patients. Each patient has an exposure_time i.e. the period of time in days during which they are at risk for a fracture. For each age category (see below), I want to add up a cumulative time at risk. So for ages 51-75, I want to come out with a number that contains the total exposure_time for all patient in that age category.

I think numlist apparently only allows 1600 observations which is somehow incremented by using numlist, max('totobs`) though I haven't figured out the syntax.

I'm sure there is a more elegant way to do this, however ...

local age1_sum=0

local age2_sum=0

local age3_sum=0

local age4_sum=0

local totobs = _N

foreach obs of numlist 1/`totobs' {

if age[`obs']<=50 {

local age1_sum = `age1_sum' + exposure_time[`obs']

}

else if age[`obs'] >50 & age[`obs'] <= 75 {

local age2_sum = `age2_sum' + exposure_time[`obs']

}

else if age[`obs'] >75 & age[`obs'] <= 90 {

local age3_sum = `age3_sum' + exposure_time[`obs']

}

else local age4_sum = `age4_sum' + exposure_time[`obs']

}

-Annemarie

University of Washington, Seattle

From: Christian Holz <statalist@krueschan.de>_________________________________________________________________

Reply-To: statalist@hsphsun2.harvard.edu

To: statalist@hsphsun2.harvard.edu

Subject: Re: st: how do i increment a counter

Date: Wed, 31 Aug 2005 21:03:09 +0100

Annemarie,

you should also check the syntax of the if command (-help ifcmd-). The command that you want to be executed if the if statement is true should be either follow on the same line as the "if" or should be enclosed by { }.

That still does not, however, solve the problem, that your code looks quite confused.

In addition to Neil's interpretations of what you might want to achieve, it might also be to get a sum of the age of all observations in your dataset that are over 50 (assuming your age variable is "age"). In that case you could do

local total_sum=0

local totobs = _N

foreach obs of numlist 1/`totobs' {

if age[`obs']>50 {

local total_sum = `total_sum' + age[`obs']

}

}

di `total_sum'

generate total_sum=`total_sum'

C.

---

Christian Holz

Department of Sociology

University of Glasgow

Scotland, U.K.

aine dooley 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.

-Annemarie

_________________________________________________________________

Don’t just search. Find. Check out the new MSN Search! http://search.msn.click-url.com/go/onm00200636ave/direct/01/

*

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

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

Express yourself instantly with MSN Messenger! Download today - it's FREE! http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/

*

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

**Follow-Ups**:**Re: st: how do i increment a counter***From:*Christian Holz <statalist@krueschan.de>

**References**:**Re: st: how do i increment a counter***From:*Christian Holz <statalist@krueschan.de>

- Prev by Date:
**Re: st: Calculating Prevalence** - Next by Date:
**RE: st: how do i increment a counter** - Previous by thread:
**Re: st: how do i increment a counter** - Next by thread:
**Re: st: how do i increment a counter** - Index(es):

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