Notice: On March 31, it was **announced** that Statalist is moving from an email list to a **forum**. The old list will shut down at the end of May, and its replacement, **statalist.org** is already up and running.

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

From |
"Nick Cox" <n.j.cox@durham.ac.uk> |

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

Subject |
RE: st: How do i substitute the return local of program in the if expression of the egen command? |

Date |
Wed, 31 Mar 2010 13:55:52 +0100 |

Thanks for this. I omitted to plug a review on working row-wise SJ-9-1 pr0046 . . . . . . . . . . . . . . . . . . . Speaking Stata: Rowwise (help rowsort, rowranks if installed) . . . . . . . . . . . N. J. Cox Q1/09 SJ 9(1):137--157 shows how to exploit functions, egen functions, and Mata for working rowwise; rowsort and rowranks are introduced Nick n.j.cox@durham.ac.uk A Loumiotis Thanks a lot for your help, especially to Nick for his detailed response and for the alternative ways to solve my questions! Everything is now clear! Antonis On Tue, Mar 30, 2010 at 3:36 PM, Nick Cox <n.j.cox@durham.ac.uk> wrote: > Several questions are bundled up together here at different levels. Let me try to separate them. Let's put aside questions about ` ' (right) and ' ' (wrong) on one side as understood. > > 1. The underlying problems > ========================== > > Antonis wants the row total of variables if and only if all variables are zero or positive. Otherwise put: if the row minimum is zero or positive. Given the second formulation, no program is needed: > > egen rowmin = rowmin(<varlist>) > egen rowtotal = rowtotal(<varlist>) if rowmin >= 0 > > Antonis' other code looks like more -egen- calls once he's done this. Correct me if I'm wrong. > > 2. Why a call to r() didn't work initially > ========================================== > > Maarten explained this. Antonis wants the incarnation of his r() result as a local macro and must use ` ' to get its contents. > > 3. Why the second call to r() still does not work > ================================================= > > This is more subtle. The first call to -egen- internally calls -count-. A side-effect of that is that existing r() results are zapped. Thus by the time of his second -egen- call the r() result Antonis wants no longer exists. He is then calling -egen- with an empty -if- condition, which is illegal. > > 4. What could be done instead > ============================= > > If you wanted to program this, and didn't like the solution in #1 for some reason, it is better to join the two programs into one based on something like > > gen rowtotal = 0 > foreach v of local varlist { > replace rowtotal = rowtotal + cond(`v' >= 0, `v', .) > } > > Adding one missing whenever a variable is negative should be sufficient to get the result desired. > > Antonis had a helper program that given (say) > > local varlist var1 var2 var3 var4 var5 > > produces > > var1 >= 0 & var2 >= 0 & var3 >= 0 & var4 >= 0 & var5 >= 0 > > There are tricks to do that more directly. > > local cond : subinstr local varlist " " ">= 0 & ", all > local cond `cond' >= 0 > > That is, replace each internal space by ">= 0 &" and then append a final ">= 0". > > Nick > n.j.cox@durham.ac.uk > > Martin Weiss > > set trace on > > then, and see where the thing goes awry, i.e. post the lines surrounding the > error... > > A Loumiotis > > Sorry.... I mistyped the wrong left single quotation mark in my email > reply, but in my code I used the correct left quotation mark "`" and I > still get for the !(`r(andpve)') if expression > > invalid syntax > r(198); > > Martin Weiss > >> Seems the left single quote is not correct. Replace "'" with "`", w/o the >> quotation marks, of course... > > A Loumiotis > >> Thanks for your help! >> >> using `r(andpve)' resolves the first problem... >> >> but for !('r(andpve)') i get the following >> >> invalid syntax >> r(198); > > Maarten buis > > A Loumiotis wrote: > >>>> I have written a program (with the name andpve) that >>>> generates an expression of the form: >>>> >>>> var1>=0 & var2>=0 & ... & varN>=0 >>>> >>>> and returns this value as a local with name r(andpve). >>>> >>>> I then want to use r(andpve) in another progam (rowctotal) >>>> where U substitute it in the if expression of the egen command. >>>> >>>> egen `left'_a = rowtotal(`right') if r(andpve), missing >>>> egen `left'_b = rowmin(`right') if !(r(andpve)) >>> >>> egen `left'_a = rowtotal(`right') if `r(andpve)', missing >>> egen `left'_b = rowmin(`right') if !(`r(andpve)') > >>>> But the egen command does not seem understand the r(andpve) >>>> as an if >>>> expression or the !(r(andpve)) as an if expression. >>>> >>>> What did I do wrong? I'm new to Stata. The code is >>>> below. >> >>>> program rowctotal, rclass >>>> version 11 >>>> gettoken left right: 0 >>>> capture drop `left' >>>> andpve `right' >>>> di r(andpve) >>>> egen `left'_a = rowtotal(`right') if r(andpve), >>>> missing >>>> egen `left'_b = rowmin(`right') if !(r(andpve)) >>>> egen `left' = rowtotal(`left'_*), missing >>>> *drop `left'_* >>>> end >>>> >>>> program andpve, rclass >>>> local k = 1 >>>> while "``k''" != "" { >>>> local ++k >>>> } >>>> local --k >>>> local i = 1 >>>> local andpve "" >>>> while `i' <= `k'-1 { >>>> local andpve "`andpve'``i''>=0 & " >>>> local ++i >>>> } >>>> local andpve "`andpve'``i''>=0" >>>> return local andpve `andpve' >>>> end * * 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/

**References**:**st: How do i substitute the return local of program in the if expression of the egen command?***From:*A Loumiotis <antonis.loumiotis@gmail.com>

**Re: st: How do i substitute the return local of program in the if expression of the egen command?***From:*Maarten buis <maartenbuis@yahoo.co.uk>

**Re: st: How do i substitute the return local of program in the if expression of the egen command?***From:*A Loumiotis <antonis.loumiotis@gmail.com>

**Re: st: How do i substitute the return local of program in the if expression of the egen command?***From:*A Loumiotis <antonis.loumiotis@gmail.com>

**RE: st: How do i substitute the return local of program in the if expression of the egen command?***From:*"Nick Cox" <n.j.cox@durham.ac.uk>

**Re: st: How do i substitute the return local of program in the if expression of the egen command?***From:*A Loumiotis <antonis.loumiotis@gmail.com>

- Prev by Date:
**st: -findname- available from SSC** - Next by Date:
**st: RE: psmatch2-identifying matched pairs** - Previous by thread:
**Re: st: How do i substitute the return local of program in the if expression of the egen command?** - Next by thread:
**st: Re: AW: string variable question** - Index(es):