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

RE: st: subscripts for loop?


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   RE: st: subscripts for loop?
Date   Thu, 1 Apr 2004 10:26:29 +0100

Clearly, it is the leading zeros which 
are the challenge here. 

Another way of doing it is from considering the effects of 

	forval i = 1/18 { 
		local j : di %02.0f `i' 
		di "`j'" 
	} 

So some loop like 

	forval i = 1/18 { 
		local j : di %02.0f `i' 
		su b0_`j' 
	} 

should be appropriate, where you put whatever
you want to do to the variable. 

Strictly, all macros contain strings, so you can 
concatenate them by putting them side by side -- except
that in context Stata will often go for the numeric 
interpretation of a string. 

One exposition linking a introduction to local macros 
to one on -for*- is 

How to face lists with fortitude. Stata Journal 
2(2): 202-222 (2002) 

A version of that is accessible at (e.g.) 
http://fmwww.bc.edu/RePEc/usug2002/fortitude.pdf

A sequel to that is 

Problems with lists. Stata Journal 3(2): 
185-202 (2003) 

Nick 
n.j.cox@durham.ac.uk 

P.S. strictly Jesper's loop will display the first value of 
each variable. 

Jesper Sorensen (and Tom Steichen similarly) 
 
> It's not clear what you want to do with the variables, but this loop 
> will _display_ them, just make arbitrary changes within the inner 
> loops.
> 
> forvalues i=1(1)18 {
> 	if `i'<10  {
> 		di b0_0`i'
> 	}
> 	else {
> 		di b0_`i'
> 	}
> }
> 

Kenley Barrett 

> >I'm stumped on a programming problem. I need to refer to a sequence 
> >of variables b0_01, b0_02, b0_03 . . .  b0_18, where the subscript 
> >identifies an individual child of a given
> >woman and the first part of the variable refers to some 
> characteristic of
> >that child. (The woman is the unit of observation, and for each woman
> >there are up to 18 children observed.) My idea was to do a 
> loop with j=1,
> >then j = j+1, etc., referring to b0_j. But this won't work. 
> My boyfriend
> >suggested doing the j loop and then concatenating to b0_, but I can't
> >find anything in stata on how to concatenate a number to a string.

*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index