[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: RE: RE: How to adjust the content of a local macro?

From   "Nick Cox" <>
To   <>
Subject   st: RE: RE: How to adjust the content of a local macro?
Date   Wed, 9 Dec 2009 17:24:19 -0000

Having looked again at the code, the problem appears to be identifying panels for which the number of non-missing values of -TotalReturn- is at least a predefined value stored in a local macro -requiredEstimationPeriod-. 

That is 

egen nvalid = count(TotalReturn), by(id) 
drop if nvalid < `requiredEstimationPeriod' 


Nick Cox

Martin answered the question here, but various secondary points arise from looking at the code. Most are on style and most are of some wider interest. 

1. The loop consists of repeated -drop-ping of observations not desired, working with the remaining subset and then a -restore- of the original. It is difficult to say in general what is most efficient and what most elegant but for a situation like that below I'd normally just add an extra condition excluding the observations not wanted, rather than repeatedly doing major surgery on the dataset. However, others could equally point out that applying -if- on a very large dataset can be time-consuming. 

2. If only the minimum and maximum are needed from a -summarize- it is best just to use a -meanonly- option. (The name -meanonly- is misleading, as I've had occasion to remark before.) 

3. Code like 

	local `minDate' = r(min)
	<stuff> if <stuff> date >= ``minDate'' 

looks legal but odd. You are probably using more levels of macros than you need. It's hard to tell because the code isn't completely self-contained (that's not a criticism; it wasn't necessary for your question). 

4. Code in which you loop over the contents of a local macro and change that macro within the loop can be tricky. Watch out! 

5. The -if- condition in 

	summarize totalReturn if totalReturn != .

is unnecessary as -summarize- always ignores missings. 

6. To get minimum and maximum dates in a panel, no looping is necessary as 

egen mindate = min(date), by(id)  
egen maxdate = max(date), by(id) 

will do it. Similarly it looks as if your main problem does not need any looping either, as it should yield to -egen- operations. Look at -egen, count()- in particular. 

7. More generally, it is not always positive to know too many other languages if they lead you to seek a Stata equivalent of other code when there's a Stataish way to do it without any real programming. 


Joachim Landström

I have what I hope to be a minor problem that I nevertheless fail to find a
solution to. Suppose that I have a local macro panelVar that contains panel
ids. Based on a selection criterion I wish to remove some panel ids from
panelVar. How do I do that? I use Stata/MP 10.1 in Windows XP 32-bit.

More specifically see example below. Suppose the panel id is called id and
the time series variable is date. Per id & date I have the actual content in
the form of totalReturn (tDelta is 7):

**** Begin Example ****
local estimationPeriod = 3

local requiredEstimationPeriod = `estimationPeriod' * floor( 365 /
``tDelta'' )

levelsof id, local(panelVar)


quietly foreach i of local panelVar ///
		restore, preserve		
		drop if id != `i'

		summarize date if totalReturn != .
		local `minDate' = r(min)
		local `maxDate' = r(max)
		summarize totalReturn if totalReturn != . ///
					& date >= ``minDate'' & date <=

		if  `r(N)' < `requiredEstimationPeriod' ///
			***** Here I wish to update the local macro panelVar
such `i' is removed *********
		else ///
**** End Example ****

*   For searches and help try:

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