Rearranging a Character Strings Into alphabetical order

Reply
New Contributor
Posts: 4

Rearranging a Character Strings Into alphabetical order

Hi everyone,

 

I was given a dataset with first name and last name variables and asked to concatonate these and then order the full name alphabetically. I have managed to concatonate the names but I am struggling to order the full name. Hopefully the example below will clear things up.

 

Data I had:

fname     sname

Jim         Smith

Tom        Bell

 

Data currently:

fullname

JimSmith

TomBell

 

Data I need:

fullname2

hiiJmmSt

BellmoT

 

Apologies if this has been answered but I have not found anything on my searches!

 

Thanks in advance

Regular Contributor
Regular Contributor
Posts: 185

Re: Rearranging a Character Strings Into alphabetical order

This looks like homework. My guess would be you would set up a loop to break each concatenated name into individual characters, dump them in an array one by one, sort the array, and output the contents.
New Contributor
Posts: 4

Re: Rearranging a Character Strings Into alphabetical order

I can see why it looks like homework! But no its for a work project and I am a relative beginner and it was causing me issues. I wouldnt have came to here without having a go myself.

Thanks for the advice, it is much appreciated!
PROC Star
Posts: 356

Re: Rearranging a Character Strings Into alphabetical order

Data currently;

input fullname $20.;

datalines;

JimSmith

TomBell

;

data want;

set currently;

array t(50) $  _temporary_;

call missing(of t(*));

do _n_=1 to length(strip(fullname));

t(_n_)=lowcase(char(fullname,_n_));

end;call sortc(of t(*));newname=cats(of t(*));run;

New Contributor
Posts: 4

Re: Rearranging a Character Strings Into alphabetical order

Posted in reply to novinosrin
Thank you so much. I will try to understand this.
SAS Super FREQ
Posts: 724

Re: Rearranging a Character Strings Into alphabetical order

Have a look at the CALL SORTC routine.
Define an array, put your chars into the array elements and then use the CALL SORTC.

What is the use case for this data manipulation?
New Contributor
Posts: 4

Re: Rearranging a Character Strings Into alphabetical order

Posted in reply to Bruno_SAS
Thanks for your advice. I dont want to say too much but its for data matching with another work data set.
Regular Contributor
Regular Contributor
Posts: 185

Re: Rearranging a Character Strings Into alphabetical order

data matching with another work data set.

 

Hmm.  "Tim Mott" and "Tom Mitt" will not match in name form, but will match in sorted anagram form. That could be interesting.

Contributor
Posts: 59

Re: Rearranging a Character Strings Into alphabetical order

[ Edited ]

@skibur  I think your question has been answered. Please close the thread. It's way too simple question

Regular Contributor
Regular Contributor
Posts: 185

Re: Rearranging a Character Strings Into alphabetical order

Not that simple for me.
Ask a Question
Discussion stats
  • 9 replies
  • 131 views
  • 1 like
  • 5 in conversation