Notice: On March 31, it was **announced** that Statalist is moving from an email list to a **forum**. The old list will shut down on April 23, 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 <njcoxstata@gmail.com> |

To |
statalist@hsphsun2.harvard.edu |

Subject |
Re: st: use of subinstr |

Date |
Thu, 22 Mar 2012 21:30:24 +0000 |

Eric's code should crack the problem nicely. I add some detailed comments: 1. for N in num 1/100: g varN = runiform() //old school 1 line loop I recommend against recommending old commands that are now undocumented. This should be in any Stata >= 7 forval N = 1/100 { g var`N' = runiform() } 2. On ds var* local varlist `r(varlist)' di `"`varlist'"' foreach x of local varlist { What follows is more direct. -ds- is several dozen lines of code to interpret. All you want to do is expand the wildcard. See also Stata Journal 10(3): 503-4. Stata tip 91: Putting unabbreviated varlists into local macros unab varlist : var * foreach x of local varlist { Nick On Thu, Mar 22, 2012 at 8:53 PM, Eric Booth <eric.a.booth@gmail.com> wrote: > <> > > Hi Tashi: > > The subinstr() string function is truncating your string to 244 chars. One short fix is to change that line to using an extended macro function (see -help extended_fcn-) since macros can store more information. > > Another fix is to approach your problem other ways, I've suggested one way below, but there are likely more efficient ways to do this task: > > ************************* > **fake data: > clear > set obs 100 > for N in num 1/100: g varN = runiform() //old school 1 line loop > > > **1. your example edited: > ds var* > local varlist `r(varlist)' > di `"`varlist'"' > foreach x of local varlist { > *local varlist_new=subinstr("`varlist'", "`x'","",1) > *instead use extended_fcn: > loc varlist_n: subinstr loc varlist "`x'" " ", > *di "`varlist_n'" //too much output for my taste > egen ave_`x'=rowmean(`varlist_n') > } > > > > ****** > ****drop ave_* > /* you could drop ave_* here, but I use -assert- to check the loops are equivalent below */ > ****** > > > > **2. Here's another approach: > ds var1-var100 > local varlist `r(varlist)' > foreach x of local varlist { > ds `x' ave*, not //assumes these are the only var names in your dataset > egen ave2_`x' = rowmean(`r(varlist)') > assert ave2_`x' == ave_`x' > } > ************************ > > > > > On Mar 22, 2012, at 3:17 PM, tashi lama wrote: > >> Hello All, >> >> I have a very large dataset with about 51 variables. I would like to write a program which would calculate rowmean but leaving one variable each time. Here is my code >> >> >> >> ds var* >> local varlist `r(varlist)' >> foreach x of local varlist { >> local varlist_new=subinstr("`varlist'", "`x'","",1) >> di "`varlist_new'" >> egen ave_`x'=rowmean(`varlist_new') >> } >> >> >> >> >> >> >> This works great if my variables are less than 244 char. However, my variables are way bigger than 244 chars. What option do I have now? My dataset has missing values if that matters. * * 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: use of subinstr***From:*Eric Booth <eric.a.booth@gmail.com>

**References**:**st: use of subinstr***From:*tashi lama <ltashi32@hotmail.com>

**Re: st: use of subinstr***From:*Eric Booth <eric.a.booth@gmail.com>

- Prev by Date:
**Re: st: use of subinstr** - Next by Date:
**Re: AW: st: error message "too many weights"** - Previous by thread:
**Re: st: use of subinstr** - Next by thread:
**Re: st: use of subinstr** - Index(es):