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

From |
Nick Winter <[email protected]> |

To |
[email protected] |

Subject |
Re: st: Using variables in numlist of forvalue command |

Date |
Wed, 08 Jun 2005 11:42:40 -0400 |

How about something like this:

sum daysupply , meanonly

forvalues daynum = 2/`r(max)' {

replace date`daynum' = date1 + `daynum'-1 if daysupply>=`daynum'

}

The first line summarizes daysupply, which puts the maximum value of daysupply in `r(max)'. (The 'meanonly' option suppresses the output of the sum command, as well as speeding it up a bit by not calculating the variance of daysupply).

Then you loop through the values of daynum that you need (i.e., from 2 through the maximum value of daysupply. The -if- condition in the replace command ensures that the command is only executed for the appropriate observations.

(If you knew beforehand that the maximum value of daysupply was 10, as in your example, you could code thta directly:

forvalues daynum = 2/10 {

replace date`daynum' = date1 + `daynum'-1 if daysupply>=`daynum'

}

but that would be less general, and would fail if you ran it again later with a dataset that had larger values of daynum.

--Nick Winter

At 11:33 AM 6/8/2005, you wrote:

I'm trying to change several variables in each observation. The problem is that the number of variables I want to update is different for each observation and this number is a variable in that observation. I've tried using a forvalues loop, which works if I specify a stop and start number, but I cannot find a way to put a variable in the numlist allowing this to change for each observation.

Here's the command that works:

forvalues daynum = 2/10 {

replace date`daynum'=date1+`daynum'-1

}

But when I replace the 10 with the variable that tells me how many date variables to change, I get a syntax error.

forvalues daynum = 2/daysupply {

replace date`daynum'=date1+`daynum'-1

}

Any suggestions?

Thanks,

_Shawn

-------------------------------------------------------- Nicholas Winter 607.255.8819 t Assistant Professor 607.255.4530 f Department of Government [email protected] e 308 White Hall falcon.arts.cornell.edu/nw53 w Cornell University Ithaca, NY 14853-4601 * * 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/

**References**:

- Prev by Date:
**st: string with embedded blanks in logical expression** - Next by Date:
**st: Using variables in numlist of forvalue command** - Previous by thread:
**st: Using variables in numlist of forvalue command** - Index(es):

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