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, is already up and running.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: Using regexr() in local macros without using an = sign

From   Nick Cox <>
Subject   Re: st: Using regexr() in local macros without using an = sign
Date   Wed, 28 Nov 2012 09:07:17 +0000

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.


On Wed, Nov 28, 2012 at 4:55 AM, Abe N <> 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.

*   For searches and help try:

© Copyright 1996–2016 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index