Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


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

Re: st: More elegant code . . .?


From   Eric Booth <[email protected]>
To   "<[email protected]>" <[email protected]>
Subject   Re: st: More elegant code . . .?
Date   Sun, 5 Jun 2011 01:30:38 +0000

<>
My last post replaces "letter_auto_stop" if any variable letter* is equal to 1 -- after re-reading your post it's clear you want "letter_auto_stop" to equal 1 if all the letter* vars are equal to 1.  These may not be much more elegant, but here are 2 other ways I can think of:

*****************
**egenmore**
	**1st, install -egenmore- from SSC
findit egenmore
egen temp =  rcount(letter1-letter10) , cond(@==1) 
g letter_auto_stop = 1 if temp==10

**use a loop to build if statement**
g letter_auto_stop2 = .
forval n = 1/10 {
loc add "& letter`n' == 1 `add' "
	}
loc add:subinstr loc add "&" ""
di "`add'"

replace letter_auto_stop2 = 1 if `add'
*****************
- Eric
__
Eric A. Booth
Public Policy Research Institute
Texas A&M University
[email protected]


On Jun 4, 2011, at 8:04 PM, Eric Booth wrote:

> <>
> 
> Here are 2 examples:
> *************************!
> **replace & inlist**
> h inlist
> g letter_auto_stop = 0
> replace letter_auto_stop = 1 if inlist(1, letter1, letter2, letter3, letter4, letter5, letter6, letter7, letter8, letter9, letter10)
> 
> **loop**
> g letter_auto_stop2 = 0
> forval n = 1/10 {
> 	replace letter_auto_stop2 = 1 if letter`n' == 1
> 	}
> *************************!
> ^watch for line wrapping in the code above
> 
> - Eric
> __
> Eric A. Booth
> Public Policy Research Institute
> Texas A&M University
> [email protected]
> 
> 
> 	
> 	
> On Jun 4, 2011, at 6:18 PM, Michael Costello wrote:
> 
>> Statalisters,
>> 
>> I have some code that I would like to make more elegant (shorter,
>> while still fitting on one line):
>> recode letter_auto_stop 0=1 if letter1==0 & letter2==0 & letter3==0 &
>> letter4==0 & letter5==0 & letter6==0 & letter7==0 & letter8==0 &
>> letter9==0 & letter10==0
>> 
>> The line above works, but it's bulky.  I'd like to find something
>> better to use.  I tried:
>> recode letter_auto_stop 0=1 if letter1-letter10==0
>> 
>> but stata treated it as "recode letter_auto_stop 0=1 if letter1==0 &
>> letter10==0" ignoring letter2 through letter9 entirely.
>> 
>> Thanks for any help you can provide!
>> -Michael
>> 
>> --
>> Michael Costello
>> Mathematics & Statistics Teacher, Bethesda-Chevy Chase High School
>> Intern, RTI International
>> 
>> "To call in the statistician after the experiment is done may be no
>> more than asking him to perform a post-mortem examination: he may be
>> able to say what the experiment died of."  -Sir Ronald Aylmer Fisher,
>> FRS
>> 


__
*
*   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–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index