1

I need to sort a string variable letters in alphabetical order in Stata. Can someone suggest a command or a method to do it?

For example: I have a string variable with 1000 observations. So the method would sort the characters (letters) like this:

School--chloos 
sort--orst
akramabad-dabamarka
5
  • Try searching for "stata sort list alphabetical order" on google, if you have any further issues with this subject. Commented Nov 20, 2017 at 11:42
  • dabamarka is wrong! Commented Nov 20, 2017 at 12:39
  • dabamarka is akramabad reversed, not alphabetised. Commented Nov 20, 2017 at 12:52
  • Thank you! I searched it before. I could only get commands and techinque to sort a list of variables and a string scalar (one only). However, I need to convert all the string scalar (1000 scalars) to alphabital order each in a newly generated column. Commented Nov 20, 2017 at 13:17
  • Each constant in a separate variable (not column)? If you mean that. my advice is that it's a bad idea. In any case, an answer was posted before your comment. Commented Nov 20, 2017 at 13:44

1 Answer 1

2

For a dataset that size, the easiest way is possibly just to expand data briefly to a version with each character in a separate observation. Your question leaves open your rules on lower and upper case, but I'll take your example "School" to "chloos" literally as implying working with lower case.

clear 
input str9 sandbox 
"School" 
"sort" 
"akramabad" 
end 

gen length = length(sandbox) 
gen id = _n 
expand length 
bysort id : gen char = substr(lower(sandbox), _n, 1)
sort id char 
bysort id (char) : gen newbox = char[1] 
by id: replace newbox = newbox[_n-1] + char if _n > 1 
by id: replace newbox = newbox[_N] 
by id: keep if _n == 1 
drop length char 

list 

     +----------------------------+
     |   sandbox   id      newbox |
     |----------------------------|
  1. |    School    1      chloos |
  2. |      sort    2        orst |
  3. | akramabad    3   aaaabdkmr |
     +----------------------------+

Creating separate variables for each letter and sorting them within observations would also seem possible.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you @Nick! This worked really well. Appreciate your help!
Good. Accepting the answer would give you extra reputation (and me too).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.