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

st: RE: RE: if "macro" == value {


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: RE: if "macro" == value {
Date   Tue, 3 Feb 2004 10:37:18 -0000

James Harris posted this code. 

foreach ICD in i21 {
	foreach age in 25 35 45 {
		foreach sex in m f {
			local deaths = "`ICD'_`age'`sex'" 
			local population = "pop`age'`sex'"
			macro list
			list year `ICD'_`age'`sex' pop`age'`sex' `deaths' `population'
			poisson `deaths' year, exposure (`population')
		}
	}
}

Phil Ryan answered the associated question. I just want 
to comment on one detail. 

In the macro assignment

local deaths = "`ICD'_`age'`sex'" 

what you're doing is just asking Stata to insert strings 
in appropriate places; there is no use of string 
functions or operators. In such cases the = sign 
is not necessary and you can just go 

local deaths "`ICD'_`age'`sex'" 

Going further: there is a positive benefit 
to doing this when you are dealing with very 
long strings. It is not pertinent here, as the 
string concerned is a variable name, but there 
is a limit to string expression lengths which can be handled
(it varies: see help on -limits-). In other problems
this can lead to bizarre bugs when long strings are 
truncated. 

In short,  

local deaths = "`ICD'_`age'`sex'" 
local deaths "`ICD'_`age'`sex'" 

are not exactly equivalent. It is a better habit 
to use the second form. (As said, this hinges 
on not using here any string operators or functions.) 

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

<<winmail.dat>>




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