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

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

From |
"Martin Weiss" <martin.weiss1@gmx.de> |

To |
<statalist@hsphsun2.harvard.edu> |

Subject |
AW: st: Remove numbers from variable name |

Date |
Tue, 29 Jun 2010 13:31:08 +0200 |

<> So, having switched the branches of the (programming) -if-, here is a sensible solution that puts the question into a -note-. (The maximum length of -varname-s is 32, the maximum for the -note- ranges from 8,681 to 67,784, depending on Rasika`s flavour of Stata.) On the other hand, given that the -varname-s must have been acceptable when they found their way into Stata, they must already comply with the 32 limit, so Rasika should decide what to do. In my view, it amounts to a slight abuse of -varname-s to fill them with the string of the question, but hey, that is just me... ************* clear* //generate data gen question5doyouhave="" gen question6areyou="" gen question16haveyouever="" foreach v of var question* { if regexm("`v'", "^question[1-9][0-9].*") { di in r "if-branch" local newname = substr("`v'", 1, 10) local notetxt = substr("`v'", 11, .) ren `v' `newname' note `newname': The question was: `notetxt' } else if regexm("`v'", "^question[1-9].*") { di in r "else-branch" local newname = substr("`v'", 1, 9) local notetxt = substr("`v'", 10, .) ren `v' `newname' note `newname': The question was: `notetxt' } else di "`v': problematic name" } //inspect the result: d note l ************** HTH Martin -----Ursprüngliche Nachricht----- Von: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] Im Auftrag von Martin Weiss Gesendet: Dienstag, 29. Juni 2010 13:23 An: statalist@hsphsun2.harvard.edu Betreff: AW: st: Remove numbers from variable name <> 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: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] Im Auftrag von Nick Cox Gesendet: Dienstag, 29. Juni 2010 12:30 An: statalist@hsphsun2.harvard.edu 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 n.j.cox@durham.ac.uk 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/

**References**:**Re: st: Remove numbers from variable name***From:*Rasika Raghavan <rasikarag@gmail.com>

**AW: st: Remove numbers from variable name***From:*"Martin Weiss" <martin.weiss1@gmx.de>

**RE: st: Remove numbers from variable name***From:*"Nick Cox" <n.j.cox@durham.ac.uk>

**AW: st: Remove numbers from variable name***From:*"Martin Weiss" <martin.weiss1@gmx.de>

- Prev by Date:
**RE: st: Remove numbers from variable name** - Next by Date:
**st: Marginal-effect calculations after 'xtlogit, fe' ?** - Previous by thread:
**RE: st: Remove numbers from variable name** - Next by thread:
**Re: st: Remove numbers from variable name** - Index(es):