Bookmark and Share

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]

Re: st: use of subinstr


From   Eric Booth <eric.a.booth@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: use of subinstr
Date   Thu, 22 Mar 2012 16:52:51 -0500

<>

On Mar 22, 2012, at 4:30 PM, Nick Cox wrote:

> 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()
> }
> 


As always, thanks Nick.  The use of the old -for- command was just for fun; I guess I should put a disclaimer about it being undocumented (or avoid using it on SL).  (I only recently discovered it in reading your 2003 (vol 3, num 2) SJ article and thought it was interesting to toy with). 


> 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


Yes - I was echoing Tashi's code - and another way to do it could be:

foreach x of varlist var* {
   **code here**
   }	

((which is probably in your SJ article you recommend (but it's behind the paywall for me))).

- Eric

__
Eric A. Booth
Public Policy Research Institute 
Texas A&M University
ebooth@ppri.tamu.edu
+979.845.6754


*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index