Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: Re: RE: RE: Deleting Characters of a Var Name by index Number


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: Re: RE: RE: Deleting Characters of a Var Name by index Number
Date   Thu, 1 May 2008 21:25:21 +0100

Bedtime reading: 

SJ-2-4  pr0007  . . . . . Speaking Stata:  On getting functions to do
the work
        . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  Y.
Lee Fox
        Q4/02   SJ 2(4):411--427                                 (no
commands)
        tips for effectively using the Stata built-in functions
        and egen functions

Accessible at
<http://www.stata-journal.com/sjpdf.html?articlenum=pr0007>

-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of Kyle Caswell
Sent: 01 May 2008 21:18
To: [email protected]
Subject: st: Re: RE: RE: Deleting Characters of a Var Name by index
Number

Sorry about that.  Typing too fast.

foreach var of varlist X31 X42 X53{
    rename `var' = [X and only the second to last character from the
*left* 
of `var']
}

That is, X31 would then be renamed X1 and X42 would be X2 and so on.

I was unaware of the substr option.Nick's example will work fine.
Thanks 
for the quick feedback! {I am new to STATA.}

Best,

Kyle


----- Original Message ----- 
From: "Nick Cox" <[email protected]>
To: <[email protected]>
Sent: Thursday, May 01, 2008 4:10 PM
Subject: st: RE: RE: Deleting Characters of a Var Name by index Number


> No. Belay that. I don't understand your example and your rule now that
I
> look at it again.
>
> -----Original Message-----
> From: [email protected]
> [mailto:[email protected]] On Behalf Of Nick Cox
> Sent: 01 May 2008 21:00
> To: [email protected]
> Subject: st: RE: Deleting Characters of a Var Name by index Number
>
> What you want may be called a macro in other languages, but it is not
in
> Stata.
> There is no need to write any new command, program or do file.
>
> foreach var of var <whatever> {
> local newvar = substr("`var'", 1, length("`var'")-2) +
> substr("`var'", -1, 1)
> rename `var' `newvar'
> }
>
> Alternatively, check out -renvars- (-search- for locations). Use the
> -map()- option.
>
> Nick
> [email protected]
>
> Kyle Caswell
>
> I would like to write a macro that renames variables by deleting a
> specific
> character of a variable name for a group of variables.
>
> For example, suppose that I have variables X31 X42 X53 and so on.   I
> would
> like to wite a macro that does something like the following:
>
> foreach var of varlist X31 X42 X53{
>    rename `var' = [X and only the second to last character from the
> *left*
> of `var']
> }
>
> That is, X31 would then be renamed X1 and X42 would be X5 and so on.
>
>
> *
> *   For searches and help try:
> *   http://www.stata.com/support/faqs/res/findit.html
> *   http://www.stata.com/support/statalist/faq
> *   http://www.ats.ucla.edu/stat/stata/
>
> *
> *   For searches and help try:
> *   http://www.stata.com/support/faqs/res/findit.html
> *   http://www.stata.com/support/statalist/faq
> *   http://www.ats.ucla.edu/stat/stata/
> 

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index