Statalist


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

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


From   "Sergiy Radyakin" <[email protected]>
To   [email protected]
Subject   Re: st: Deleting Characters of a Var Name by index Number
Date   Thu, 1 May 2008 16:15:04 -0400

Hello Kyle,

I don't think I understand how you want to transform X42 to X5 based
on your description, but if you'd like to keep two first characters of
the variable names

// begin of file
sysuse auto
ds
local vars `r(varlist)'
foreach var of local vars {
  rename `var' `=substr("`var'",1,2)'
}
// end of file

Hope this helps,
   Sergiy Radyakin


On 5/1/08, Kyle Caswell <[email protected]> wrote:
> Hello.
>
> 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.
>
> I appreciate any tips!
>
> Best,
>
> Kyle
>
> Kyle
>
>
> *
> *   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