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   Eric Booth <[email protected]>
To   [email protected]
Subject   Re: st: use of subinstr
Date   Thu, 22 Mar 2012 15:53:30 -0500

<>

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'
}
************************



- Eric


__
Eric A. Booth
Public Policy Research Institute 
Texas A&M University
[email protected]
+979.845.6754





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. 
> 
> 
> 
> Thanx,
> 
> Tashi
> 
>  		 	   		  
> *
> *   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/


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