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: foreach global macro not working with multiple macros


From   Eric Booth <ebooth@ppri.tamu.edu>
To   "<statalist@hsphsun2.harvard.edu>" <statalist@hsphsun2.harvard.edu>
Subject   Re: st: foreach global macro not working with multiple macros
Date   Thu, 3 Feb 2011 15:47:23 +0000

<>
On Feb 3, 2011, at 5:45 AM, Seed, Paul wrote:

> **first 6 and last 6 lines are from log, remove those: 
> drop in 1/6  // Eric dropped only 4 lines.  I am not sure why.
> drop in `=_N+1-6'/l  // Using _N-6 would drop 7 lines, 
> // including one of the local macros!

Admittedly, the solution I posed was a kludge, aimed at keeping you from having to type out all your globals.   Your code comments ask why I dropped a certain # of lines, I think part of the problem was that I had -rmsg- set on which put in some extra lines and I probably miscounted some lines while trying out a couple of different scenarios to help you solve your problem-- the number of lines dropped did work for me at some point.  However, that's why I always put warnings in the commenting about checking this on your own.



Since the exchange yesterday, I've toyed with creating an .ado file that can unabbreaviate macros with a syntax similar to -unab- (so the syntax is currently:  mac_unab macname : varlist ) that will store the unabbreviated list in `r(mac_list)' , but I cannot find a way to capture the list of macros besides creating a logfile for the -macro li- output which must be run -noisily- from within the .ado file to capture the macros of interest in the log file.  I'd be interested to hear if someone knows about another approach to capture the output from -macro li- from inside a program without having it show up in the results window.  Also, if anyone is interested in taking a look at -mac_unab-, let me know.

- Eric


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


On Feb 3, 2011, at 5:45 AM, Seed, Paul wrote:

> I re-wrote Eric's program to collect local macro names
> beginning with c, rather than global.
> Like Nick Cox, I use locals more often.
> 
> There is one non-trivial change that I thought was worth sharing.
> 
> *******  Grabbing list of all local macros that start with "c"
> local cash "pocket asdf bank"
> local check "bank"
> local c3 "blah blah"
> local c99 "test"
> local z1 "ignore this one"
> 
> *******get names of all locals 
> 
> preserve
> 
> cap log close _all
> log using "macros.txt", text replace
> macro li
> log close _all
> 
> clear
> insheet using macros.txt, tab nonames
> **first 6 and last 6 lines are from log, remove those: 
> drop in 1/6  // Eric dropped only 4 lines.  I am not sure why.
> drop in `=_N+1-6'/l  // Using _N-6 would drop 7 lines, 
> // including one of the local macros!
> **keep if first two letters are _c
> keep if substr(v1, 1, 2)=="_c" 
> *get names of all locals that start with "c"**
> split v1, p(": ") // splits v1 into v11 (the local name, before the colon) & v12 (the description, after the colon).
> keep v11
> replace v11 = substr(v1,2,.)  // Drop the initial underscore
> levelsof v11, loc(local_c) clean
> global local_c `local_c'
> di `"$local_c"'
> restore
> ********
> 
> foreach y of global local_c  {
> display "`y'"
> }


On Feb 2, 2011, at 11:44 AM, Nick Mosely wrote:

> Statalist,
> 
> Thanks, guys. That does work, Eric. Unfortunately I have about 100
> macros that I would like to loop over. Their names all start with the
> letter p, so I was hoping to do something like the following:
> 
> foreach y of global p* {
> 
> I wonder, what is the use of -foreach- with macros if you can only use
> one at a time?
> I guess I'll have to find another way.
> 
> Thanks again for your help,
> 
> ~Nick

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