Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
From | Nick Cox <njcoxstata@gmail.com> |
To | statalist@hsphsun2.harvard.edu |
Subject | Re: st: Using regexr() in local macros without using an = sign |
Date | Thu, 29 Nov 2012 10:13:16 +0000 |
You should look at -help macrolists-. (More generally, if you start browsing at -help macro- you will find several tools that you can use.) . local all "aname bname cname" . local b "bname" . local allbut : list all - b . mac li <snip> _allbut: aname cname _b: bname _all: aname bname cname You'd need to call that in a loop in your case, but you are presumably doing that already. I note also that you use if strmatch("`v'", "`i'") == 0 when if "`v'" != "`i'" is simpler, although as above the macrolist approach is better. If you are looking for the characters "b." in a name then foreach v of local varlist { if strpos("`v'","b.") local onlybases `onlybases' `v' else local nobases `nobases' `v' } is an alternative. Nick On Thu, Nov 29, 2012 at 8:56 AM, Abe N <aben468@yahoo.com> wrote: > Actually, I had to modify that last part in the loop to the following: > local elrest > foreach v of local rest { > if strmatch("`v'", "``i''")==0 local elrest `elrest' `v' > } > and it seems to be working thus far. Going to let this run overnight to > test and see if it works out tomorrow. THanks again! On Thu, 11/29/12, Abe N <aben468@yahoo.com> wrote: >> Thanks for the help Nick, the code >> you showed me is so much simpler than what I was >> trying. It seems to work, although I tweaked it a >> little bit, because I think it's more important that the >> variable have "b." in there somewhere. Here's the code >> I ended up using in case anyone else runs into this: >> >> foreach v of local varlist { >> if strmatch("`v'","*b.*") == 1 local >> onlybases `onlybases' `v' >> else local nobases `nobases' `v' >> } >> >> I've now run into a slightly different issue that maybe you >> can help me with. There is somewhere else in my >> program that I've evaluated a macro and again I run into the >> character limit. If you were to take the variables >> contained in onlybases from above, I'm running a loop where >> first for each variable I run a logistic by itself, then I >> run a logistic with all the variables in `onlybases' except >> without the variable. >> >> So first for each variable in `onlybases', I perform the >> following commands (``i'' is the variable here, used after >> tokenizing and the loop is done using a while command) >> while "``i''"~="" { >> logistic `dv' ``i'' if `touse' >> local elrest = regexr("`rest'", "``i''", >> "") >> logistic `dv' `elrest' `onlybases' if >> `touse' >> } >> >> Is there a way I could accomplish this without evaluating >> again with an =? Now that I think about it, maybe I >> should try repeating something like the above within this >> loop? Maybe: >> foreach v of local rest { >> if strmatch("`rest'", ``i'')==0 local elrest `elrest' >> `v' >> } >> I think that would but all the variables except ``i'' in a >> local, right? On Wed, 11/28/12, Nick Cox <njcoxstata@gmail.com> wrote: >> > If I understand your problem, a more basic approach is >> > something like >> > >> > fvexpand `varlist' >> > local varlist "`r(varlist)'" >> > foreach v of local varlist { >> > >> > if strpos("`v'", 1) == "0" local sheep >> > `sheep' `v' >> > >> > else local goats `goats' `v' >> > } >> > >> > You can use regular expressions if you like, but the >> key >> > issue appears >> > to be whether "0" is the first character. >> > >> > -tokenize- is not going to help much, if at all. You >> want to >> > look at >> > each name in a list and whether the names are in >> individual >> > macros or >> > just one long macro is not the main question. >> > >> > -regexr()- is a function, not a command. On Wed, Nov 28, 2012 at 4:55 AM, Abe N <aben468@yahoo.com wrote: >> > I've been trying to write a program that at the basics >> of it >> > runs a series of logistic regressions and while I got >> it to >> > work for the most part, I've been trying to do >> something >> > specifically with factor variables (I'm using version >> 11.2 >> > STATA/SE) >> > > >> > > What I'm trying to do currently I think is pretty >> > basic. I'm starting with a variable list that has >> > factor variables (ex. my current varlist will >> basically >> > contain: dependent_variable i.age_cat i.year female), >> > expanding it, and then trying to create two new macros, >> one >> > that contains the base variables (ex. 0b.age_cat >> 1998b.year) >> > and another that contains the rest (ex. died 1.age_cat >> > 2.age_cat 1999.year 2000.year 2001.year female). >> > > >> > > The way I went about this at first worked well for >> a >> > couple variables, but I was running into the issue of >> > character limits when you evaluate a macro with an = >> > sign. Here's the code I was using: >> > > fvexpand `varlist' >> > > local varlist = "`r(varlist)'" >> > > local nobases = "`varlist'" >> > > local loopvar = >> > regexm("`nobases'", "[a-zA-Z0-9]+[b][\.][a-zA-Z0-9_]+[ >> ]*") >> > > local onlybases >> > > while "`loopvar'"=="1" { >> > > local onlybases = >> > "`onlybases' "+regexs(0) >> > > local nobases = >> > regexr("`nobases'", "[a-zA-Z0-9]+[b][\.][a-zA-Z0-9_]+[ >> ]*", >> > "") >> > > local loopvar = >> > regexm("`nobases'", "[a-zA-Z0-9]+[b][\.][a-zA-Z0-9_]+[ >> ]*") >> > > } >> > > local varlist "`nobases'" >> > > Then when I run logistics later, I would >> basically >> > use: >> > > logistic `nobases' `onlybases' >> > > >> > > The problem with this method is the macro >> `nobases' >> > will cut off after a certain # of characters. So I >> > tried doing things without = signs, but I've been >> running >> > into a lot of problems because I'm using the regexr() >> > command and it doesn't seem to like me using that >> without an >> > = sign. >> > > >> > > Now that I've written this up, I'm wondering if >> this >> > would have been a lot easier using tokenize, but I >> think I >> > avoided it because I use it later for a very key aspect >> of >> > the program and I think I was worried it might mess >> with >> > some of my later loops. * * For searches and help try: * http://www.stata.com/help.cgi?search * http://www.stata.com/support/faqs/resources/statalist-faq/ * http://www.ats.ucla.edu/stat/stata/