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

st: RE: 2k problem in forvalues


From   "Nick Winter" <[email protected]>
To   <[email protected]>
Subject   st: RE: 2k problem in forvalues
Date   Wed, 27 Nov 2002 10:50:26 -0500

> -----Original Message-----
> From: Hans J. Baumgartner [mailto:[email protected]] 
> Sent: Wednesday, November 27, 2002 10:44 AM
> To: statalist
> Subject: st: 2k problem in forvalues
> 
> 
> I would like to use -forvalues- to run loops over the years 84-02. It 
> works fine from 84-99 but in 2000 I get problems. I have tried to run 
> the loop 00-02 separately to avoid the "2k flip". The thing 
> is, however 
> that -foreach- and forvalues- writes 00 as 0 and 01 as 1.
> 
> Any suggestions on this?
> Hans

Hi,

In a related problem, when I needed to refer to things like "xxx01"
through "xxx23", I have done the following:

	forvalues i=0/20 {
		local j : di %02.0f `i'
		di "Item number `i' is xxx`j'"
	}

See -help local- on the "local j: ..." line; this macro extended
function assigns the output of the display command -- which formats the
number to have a leading zero -- to the macro j.

You could avoid doing things in two steps with something clever/tricky
like:

	forvalues i=84/102 {
		local j=mod(`i',100)
		local j : di %02.0f `j'
		di "year is `j'"
	}

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