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: Remove numbers from variable name


From   Steve Samuels <[email protected]>
To   [email protected]
Subject   Re: st: Remove numbers from variable name
Date   Tue, 29 Jun 2010 07:34:59 -0400

di in r regexm("question16haveyouever", "^question[1-9][^1-9].*")

will properly handle the single digit numbers..

Steve

-- 
Steven Samuels
[email protected]
18 Cantine's Island
Saugerties NY 12477
USA
Voice: 845-246-0774
Fax:    206-202-4783


On Tue, Jun 29, 2010 at 7:22 AM, Martin Weiss <[email protected]> wrote:
>
> <>
>
> But careful! A "two-digit- question" would comply with the if-branch in
> Nick`s solution as well. So I am afraid it would not make it to the
> else-branch:
>
> *************
> di in r regexm("question16haveyouever", "^question[1-9].*")
> *************
>
>
>
> HTH
> Martin
>
>
> -----Ursprüngliche Nachricht-----
> Von: [email protected]
> [mailto:[email protected]] Im Auftrag von Nick Cox
> Gesendet: Dienstag, 29. Juni 2010 12:30
> An: [email protected]
> Betreff: RE: st: Remove numbers from variable name
>
> In terms of the other question, regular expressions offer one way to
> distinguish one from two digits:
>
> foreach v of var question* {
>        if regexm("`v'", "^question[1-9].*") {
>                local newname = substr("`v'", 10, .)
>                rename `v' `newname'
>        }
>        else if regexm("`v'", "^question[1-9][0-9].*") {
>                local newname = substr("`v'", 11, .)
>                rename `v' `newname'
>        }
>        else di "`v': problematic name"
> }
>
> Nick
> [email protected]
>
> Martin Weiss
>
> " Does not -subinstr- work for string observation, and not variable
> names?"
>
> -subinstr- substitutes parts of strings within strings. Since a variable
> name is just a string, I can use it for your purposes:
>
> *************
> clear*
> gen question5doyouhave=""
> gen question6areyou=""
>
> foreach var of varlist _all{
>        loc newname = substr(subinstr("`var'","question","",1),2,.)
>        rename `var' `newname'
> }
> *************
>
> For the -varname-s containing two digits, you would have to change the
> code
> to "),3,.)" at the end of the -local- line...
>
> Rasika Raghavan
>
> Richard,
>
> Thanks. It worked out fine. Can you suggest though a way in which I
> don't have to specify the variables? Why I am asking is that, for
> single digit questions it would be predrop(10) and for two digit
> question numbers, it would be predrop(11). While it was easy in my
> case, I can think of situations where this might work out to be
> tedious.
>
> Martin,
>
> The "are you" was a typo. But thanks, I had not realized something
> like -strtoname- existed.
>
> I did not quite follow "For your substitution, look at -subinstr()-.
> Why not put the question in the -var label- or a -note- to the
> variable?"
>
> Does not -subinstr- work for string observation, and not variable names?
>
>
> *
> *   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/
>
>
> *
> *   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/
>

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