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: Using regexr() in local macros without using an = sign


From   Abe N <[email protected]>
To   [email protected]
Subject   Re: st: Using regexr() in local macros without using an = sign
Date   Thu, 29 Nov 2012 00:35:36 -0800 (PST)

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?

Thanks again for the help!
Abe Noorbakhsh
[email protected]

--- On Wed, 11/28/12, Nick Cox <[email protected]> wrote:

> From: Nick Cox <[email protected]>
> Subject: Re: st: Using regexr() in local macros without using an = sign
> To: [email protected]
> Date: Wednesday, November 28, 2012, 1:07 AM
> Abe N.: You are asked to use your
> _full real name_.
> 
> 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.
> 
> Nick
> 
> On Wed, Nov 28, 2012 at 4:55 AM, Abe N <[email protected]>
> wrote:
> 
> > This is my first time posting something, so I hope this
> works and sorry in advance if I messed something up. 
> 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.  Anyway, any help would be much
> appreciated.
> >
> > Thanks,
> >
> > Abe N.
> > [email protected]
> 
> *
> *   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/
> 

*
*   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/


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