Statalist


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

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


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: How to adjust the content of a local macro?
Date   Wed, 9 Dec 2009 17:15:13 -0000

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. 

Nick 
[email protected] 

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)

preserve 

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 <=
``maxDate''

		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:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   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