Change order of text in variable names
I have the following dataset:
* Example generated by -dataex-. To install: ssc install dataex
clear
input float(MA_234_AAF_US AL_87665_ACH_USA TX_3_GH_US LA_689_KLO_US KY_3435_Z_USA)
9.96567 10.559998 12.935112 13.142867 9.35608
9.758375 9.856 10.002945 8.090142 10.313352
11.594983 9.274136 12.486753 6.661111 10.529528
10.354564 9.893115 10.625778 13.265523 7.405652
12.7978 10.76272 11.527348 10.112844 11.64973
10.63846 11.040354 8.569465 8.781206 11.448466
9.254233 13.808356 10.817062 9.545164 8.759109
11.8417 10.15155 12.72436 11.102546 11.506034
9.864883 9.864952 14.45111 10.12562 9.753519
9.965327 11.517155 9.910269 8.988406 11.359774
end
I would like to change the order of the text in the variable names like this:
US_MA_AAF_234 USA_AL_ACH_87665 US_TX_GH_3 US_LA_KLO_689 USA_KY_Z_3435
I have tried the code provided in the answers in this question:
- Remove middle character from variable names
However, I could not make it work.
stata
add a comment |
I have the following dataset:
* Example generated by -dataex-. To install: ssc install dataex
clear
input float(MA_234_AAF_US AL_87665_ACH_USA TX_3_GH_US LA_689_KLO_US KY_3435_Z_USA)
9.96567 10.559998 12.935112 13.142867 9.35608
9.758375 9.856 10.002945 8.090142 10.313352
11.594983 9.274136 12.486753 6.661111 10.529528
10.354564 9.893115 10.625778 13.265523 7.405652
12.7978 10.76272 11.527348 10.112844 11.64973
10.63846 11.040354 8.569465 8.781206 11.448466
9.254233 13.808356 10.817062 9.545164 8.759109
11.8417 10.15155 12.72436 11.102546 11.506034
9.864883 9.864952 14.45111 10.12562 9.753519
9.965327 11.517155 9.910269 8.988406 11.359774
end
I would like to change the order of the text in the variable names like this:
US_MA_AAF_234 USA_AL_ACH_87665 US_TX_GH_3 US_LA_KLO_689 USA_KY_Z_3435
I have tried the code provided in the answers in this question:
- Remove middle character from variable names
However, I could not make it work.
stata
add a comment |
I have the following dataset:
* Example generated by -dataex-. To install: ssc install dataex
clear
input float(MA_234_AAF_US AL_87665_ACH_USA TX_3_GH_US LA_689_KLO_US KY_3435_Z_USA)
9.96567 10.559998 12.935112 13.142867 9.35608
9.758375 9.856 10.002945 8.090142 10.313352
11.594983 9.274136 12.486753 6.661111 10.529528
10.354564 9.893115 10.625778 13.265523 7.405652
12.7978 10.76272 11.527348 10.112844 11.64973
10.63846 11.040354 8.569465 8.781206 11.448466
9.254233 13.808356 10.817062 9.545164 8.759109
11.8417 10.15155 12.72436 11.102546 11.506034
9.864883 9.864952 14.45111 10.12562 9.753519
9.965327 11.517155 9.910269 8.988406 11.359774
end
I would like to change the order of the text in the variable names like this:
US_MA_AAF_234 USA_AL_ACH_87665 US_TX_GH_3 US_LA_KLO_689 USA_KY_Z_3435
I have tried the code provided in the answers in this question:
- Remove middle character from variable names
However, I could not make it work.
stata
I have the following dataset:
* Example generated by -dataex-. To install: ssc install dataex
clear
input float(MA_234_AAF_US AL_87665_ACH_USA TX_3_GH_US LA_689_KLO_US KY_3435_Z_USA)
9.96567 10.559998 12.935112 13.142867 9.35608
9.758375 9.856 10.002945 8.090142 10.313352
11.594983 9.274136 12.486753 6.661111 10.529528
10.354564 9.893115 10.625778 13.265523 7.405652
12.7978 10.76272 11.527348 10.112844 11.64973
10.63846 11.040354 8.569465 8.781206 11.448466
9.254233 13.808356 10.817062 9.545164 8.759109
11.8417 10.15155 12.72436 11.102546 11.506034
9.864883 9.864952 14.45111 10.12562 9.753519
9.965327 11.517155 9.910269 8.988406 11.359774
end
I would like to change the order of the text in the variable names like this:
US_MA_AAF_234 USA_AL_ACH_87665 US_TX_GH_3 US_LA_KLO_689 USA_KY_Z_3435
I have tried the code provided in the answers in this question:
- Remove middle character from variable names
However, I could not make it work.
stata
stata
edited Nov 14 '18 at 21:55
Pearly Spencer
10.9k173461
10.9k173461
asked Oct 9 '18 at 13:02
user10478681user10478681
333
333
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
All three approaches from the other question can work.
The only challenging bit here is that different parts of the variable names
have different length.
As such, the best approach is probably the use of the built-in command rename:
clear
input float(MA_234_AAF_US AL_87665_ACH_USA TX_3_GH_US LA_689_KLO_US KY_3435_Z_USA)
9.96567 10.559998 12.935112 13.142867 9.35608
end
rename (*_#_*_*) (*[4]_*[1]_*[3]_#[2])
list, abbreviate(20) noobs
+-------------------------------------------------------------------------------+
| US_MA_AAF_234 USA_AL_ACH_87665 US_TX_GH_3 US_LA_KLO_689 USA_KY_Z_3435 |
|-------------------------------------------------------------------------------|
| 9.96567 10.56 12.93511 13.14287 9.35608 |
+-------------------------------------------------------------------------------+
You need to study carefully the use of specifiers in help rename group.
add a comment |
Here is an alternative approach.
It's inferior to using rename in one line, which addresses the purpose well. Scrutiny will show the necessary correspondence with that approach. It hinges on the names being elements separated by underscores, which are removed and then reinserted.
clear
input float(MA_234_AAF_US AL_87665_ACH_USA TX_3_GH_US LA_689_KLO_US KY_3435_Z_USA)
9.96567 10.559998 12.935112 13.142867 9.35608
end
foreach name of var * {
local new = subinstr("`name'", "_", " ", .)
tokenize `new'
rename `name' `4'_`1'_`3'_`2'
}
describe, fullnames
Contains data
obs: 1
vars: 5
size: 20
-------------------------------------------------------------------------------------------
storage display value
variable name type format label variable label
-------------------------------------------------------------------------------------------
US_MA_AAF_234 float %9.0g
USA_AL_ACH_87665
float %9.0g
US_TX_GH_3 float %9.0g
US_LA_KLO_689 float %9.0g
USA_KY_Z_3435 float %9.0g
-------------------------------------------------------------------------------------------
EDIT:
As @PearlySpencer points out, the statements within the loop
local new = subinstr("`name'", "_", " ", .)
tokenize `new'
rename `name' `4'_`1'_`3'_`2'
could be replaced by
tokenize `name', parse(_)
rename `name' `7'_`1'_`5'_`3'
The difference is that the underscores will get placed in local macros 2, 4, 6.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52721745%2fchange-order-of-text-in-variable-names%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
All three approaches from the other question can work.
The only challenging bit here is that different parts of the variable names
have different length.
As such, the best approach is probably the use of the built-in command rename:
clear
input float(MA_234_AAF_US AL_87665_ACH_USA TX_3_GH_US LA_689_KLO_US KY_3435_Z_USA)
9.96567 10.559998 12.935112 13.142867 9.35608
end
rename (*_#_*_*) (*[4]_*[1]_*[3]_#[2])
list, abbreviate(20) noobs
+-------------------------------------------------------------------------------+
| US_MA_AAF_234 USA_AL_ACH_87665 US_TX_GH_3 US_LA_KLO_689 USA_KY_Z_3435 |
|-------------------------------------------------------------------------------|
| 9.96567 10.56 12.93511 13.14287 9.35608 |
+-------------------------------------------------------------------------------+
You need to study carefully the use of specifiers in help rename group.
add a comment |
All three approaches from the other question can work.
The only challenging bit here is that different parts of the variable names
have different length.
As such, the best approach is probably the use of the built-in command rename:
clear
input float(MA_234_AAF_US AL_87665_ACH_USA TX_3_GH_US LA_689_KLO_US KY_3435_Z_USA)
9.96567 10.559998 12.935112 13.142867 9.35608
end
rename (*_#_*_*) (*[4]_*[1]_*[3]_#[2])
list, abbreviate(20) noobs
+-------------------------------------------------------------------------------+
| US_MA_AAF_234 USA_AL_ACH_87665 US_TX_GH_3 US_LA_KLO_689 USA_KY_Z_3435 |
|-------------------------------------------------------------------------------|
| 9.96567 10.56 12.93511 13.14287 9.35608 |
+-------------------------------------------------------------------------------+
You need to study carefully the use of specifiers in help rename group.
add a comment |
All three approaches from the other question can work.
The only challenging bit here is that different parts of the variable names
have different length.
As such, the best approach is probably the use of the built-in command rename:
clear
input float(MA_234_AAF_US AL_87665_ACH_USA TX_3_GH_US LA_689_KLO_US KY_3435_Z_USA)
9.96567 10.559998 12.935112 13.142867 9.35608
end
rename (*_#_*_*) (*[4]_*[1]_*[3]_#[2])
list, abbreviate(20) noobs
+-------------------------------------------------------------------------------+
| US_MA_AAF_234 USA_AL_ACH_87665 US_TX_GH_3 US_LA_KLO_689 USA_KY_Z_3435 |
|-------------------------------------------------------------------------------|
| 9.96567 10.56 12.93511 13.14287 9.35608 |
+-------------------------------------------------------------------------------+
You need to study carefully the use of specifiers in help rename group.
All three approaches from the other question can work.
The only challenging bit here is that different parts of the variable names
have different length.
As such, the best approach is probably the use of the built-in command rename:
clear
input float(MA_234_AAF_US AL_87665_ACH_USA TX_3_GH_US LA_689_KLO_US KY_3435_Z_USA)
9.96567 10.559998 12.935112 13.142867 9.35608
end
rename (*_#_*_*) (*[4]_*[1]_*[3]_#[2])
list, abbreviate(20) noobs
+-------------------------------------------------------------------------------+
| US_MA_AAF_234 USA_AL_ACH_87665 US_TX_GH_3 US_LA_KLO_689 USA_KY_Z_3435 |
|-------------------------------------------------------------------------------|
| 9.96567 10.56 12.93511 13.14287 9.35608 |
+-------------------------------------------------------------------------------+
You need to study carefully the use of specifiers in help rename group.
edited Dec 6 '18 at 14:02
answered Oct 9 '18 at 13:11
Pearly SpencerPearly Spencer
10.9k173461
10.9k173461
add a comment |
add a comment |
Here is an alternative approach.
It's inferior to using rename in one line, which addresses the purpose well. Scrutiny will show the necessary correspondence with that approach. It hinges on the names being elements separated by underscores, which are removed and then reinserted.
clear
input float(MA_234_AAF_US AL_87665_ACH_USA TX_3_GH_US LA_689_KLO_US KY_3435_Z_USA)
9.96567 10.559998 12.935112 13.142867 9.35608
end
foreach name of var * {
local new = subinstr("`name'", "_", " ", .)
tokenize `new'
rename `name' `4'_`1'_`3'_`2'
}
describe, fullnames
Contains data
obs: 1
vars: 5
size: 20
-------------------------------------------------------------------------------------------
storage display value
variable name type format label variable label
-------------------------------------------------------------------------------------------
US_MA_AAF_234 float %9.0g
USA_AL_ACH_87665
float %9.0g
US_TX_GH_3 float %9.0g
US_LA_KLO_689 float %9.0g
USA_KY_Z_3435 float %9.0g
-------------------------------------------------------------------------------------------
EDIT:
As @PearlySpencer points out, the statements within the loop
local new = subinstr("`name'", "_", " ", .)
tokenize `new'
rename `name' `4'_`1'_`3'_`2'
could be replaced by
tokenize `name', parse(_)
rename `name' `7'_`1'_`5'_`3'
The difference is that the underscores will get placed in local macros 2, 4, 6.
add a comment |
Here is an alternative approach.
It's inferior to using rename in one line, which addresses the purpose well. Scrutiny will show the necessary correspondence with that approach. It hinges on the names being elements separated by underscores, which are removed and then reinserted.
clear
input float(MA_234_AAF_US AL_87665_ACH_USA TX_3_GH_US LA_689_KLO_US KY_3435_Z_USA)
9.96567 10.559998 12.935112 13.142867 9.35608
end
foreach name of var * {
local new = subinstr("`name'", "_", " ", .)
tokenize `new'
rename `name' `4'_`1'_`3'_`2'
}
describe, fullnames
Contains data
obs: 1
vars: 5
size: 20
-------------------------------------------------------------------------------------------
storage display value
variable name type format label variable label
-------------------------------------------------------------------------------------------
US_MA_AAF_234 float %9.0g
USA_AL_ACH_87665
float %9.0g
US_TX_GH_3 float %9.0g
US_LA_KLO_689 float %9.0g
USA_KY_Z_3435 float %9.0g
-------------------------------------------------------------------------------------------
EDIT:
As @PearlySpencer points out, the statements within the loop
local new = subinstr("`name'", "_", " ", .)
tokenize `new'
rename `name' `4'_`1'_`3'_`2'
could be replaced by
tokenize `name', parse(_)
rename `name' `7'_`1'_`5'_`3'
The difference is that the underscores will get placed in local macros 2, 4, 6.
add a comment |
Here is an alternative approach.
It's inferior to using rename in one line, which addresses the purpose well. Scrutiny will show the necessary correspondence with that approach. It hinges on the names being elements separated by underscores, which are removed and then reinserted.
clear
input float(MA_234_AAF_US AL_87665_ACH_USA TX_3_GH_US LA_689_KLO_US KY_3435_Z_USA)
9.96567 10.559998 12.935112 13.142867 9.35608
end
foreach name of var * {
local new = subinstr("`name'", "_", " ", .)
tokenize `new'
rename `name' `4'_`1'_`3'_`2'
}
describe, fullnames
Contains data
obs: 1
vars: 5
size: 20
-------------------------------------------------------------------------------------------
storage display value
variable name type format label variable label
-------------------------------------------------------------------------------------------
US_MA_AAF_234 float %9.0g
USA_AL_ACH_87665
float %9.0g
US_TX_GH_3 float %9.0g
US_LA_KLO_689 float %9.0g
USA_KY_Z_3435 float %9.0g
-------------------------------------------------------------------------------------------
EDIT:
As @PearlySpencer points out, the statements within the loop
local new = subinstr("`name'", "_", " ", .)
tokenize `new'
rename `name' `4'_`1'_`3'_`2'
could be replaced by
tokenize `name', parse(_)
rename `name' `7'_`1'_`5'_`3'
The difference is that the underscores will get placed in local macros 2, 4, 6.
Here is an alternative approach.
It's inferior to using rename in one line, which addresses the purpose well. Scrutiny will show the necessary correspondence with that approach. It hinges on the names being elements separated by underscores, which are removed and then reinserted.
clear
input float(MA_234_AAF_US AL_87665_ACH_USA TX_3_GH_US LA_689_KLO_US KY_3435_Z_USA)
9.96567 10.559998 12.935112 13.142867 9.35608
end
foreach name of var * {
local new = subinstr("`name'", "_", " ", .)
tokenize `new'
rename `name' `4'_`1'_`3'_`2'
}
describe, fullnames
Contains data
obs: 1
vars: 5
size: 20
-------------------------------------------------------------------------------------------
storage display value
variable name type format label variable label
-------------------------------------------------------------------------------------------
US_MA_AAF_234 float %9.0g
USA_AL_ACH_87665
float %9.0g
US_TX_GH_3 float %9.0g
US_LA_KLO_689 float %9.0g
USA_KY_Z_3435 float %9.0g
-------------------------------------------------------------------------------------------
EDIT:
As @PearlySpencer points out, the statements within the loop
local new = subinstr("`name'", "_", " ", .)
tokenize `new'
rename `name' `4'_`1'_`3'_`2'
could be replaced by
tokenize `name', parse(_)
rename `name' `7'_`1'_`5'_`3'
The difference is that the underscores will get placed in local macros 2, 4, 6.
edited Nov 14 '18 at 22:31
answered Oct 9 '18 at 13:28
Nick CoxNick Cox
25.1k42038
25.1k42038
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52721745%2fchange-order-of-text-in-variable-names%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown