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

From |
"Martin Weiss" <martin.weiss1@gmx.de> |

To |
<statalist@hsphsun2.harvard.edu> |

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

Date |
Wed, 9 Dec 2009 18:19:31 +0100 |

<> " Code in which you loop over the contents of a local macro and change that macro within the loop can be tricky. Watch out!" Note I distinguished between the -local- hailing from the -levelsof- operation ("panelVar" with the insidious capitalization) and the -local- being subtracted from ("newpanelVar") in my reply. I do support Nick`s sentiment that the whole setup looks way too complicated. If Joachim could describe his problem in plain terms and provide an excerpt of the data, we could try to make life easier for him :-) HTH Martin -----Ursprüngliche Nachricht----- Von: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] Im Auftrag von Nick Cox Gesendet: Mittwoch, 9. Dezember 2009 18:15 An: statalist@hsphsun2.harvard.edu Betreff: st: RE: How to adjust the content of a local macro? 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 n.j.cox@durham.ac.uk 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/ * * 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/

**Follow-Ups**:**Re: st: AW: RE: How to adjust the content of a local macro?***From:*Joachim LandstrÃ¶m <joachim.landstrom@fek.uu.se>

**References**:**st: How to adjust the content of a local macro?***From:*Joachim Landström <joachim.landstrom@fek.uu.se>

**st: RE: How to adjust the content of a local macro?***From:*"Nick Cox" <n.j.cox@durham.ac.uk>

- Prev by Date:
**st: RE: RE: How to adjust the content of a local macro?** - Next by Date:
**Re: st: How do I calculate prvalues for specific time points in my survival analysis?** - Previous by thread:
**AW: st: RE: RE: How to adjust the content of a local macro?** - Next by thread:
**Re: st: AW: RE: How to adjust the content of a local macro?** - Index(es):

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