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: use of subinstr


From   Nick Cox <[email protected]>
To   [email protected]
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 <[email protected]> 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/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index