BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
arde
Obsidian | Level 7

Hi.

 

I have data in a cell that  I would like alphabetized:

 

Have:

John C Plus,B,A
Sarah D,A Plus,C Minus

 

Want:

John A,B,C Plus
Sarah A Plus,C Minus,D

 

 

Thank you in advance for your efforts

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Split the words out into separate variables.

Sort those variables

Recombine the variables in order into the current variable

remove the other variables.

 

You really should provide data in the form of working data step code if you want tested code. at least provide data set names and variable names. Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the </> icon or attached as text to show exactly what you have and that we can test code against.

Untested as no data set provided:

data want;
    set have;
   /* the 5 below should be replaced the maximum number of 
     "words" in the list, 10 should be replaced with the maximum
     length of any of the words
   */
    array t ( 5) $ 10;
   /* countw here gets the number of elements separated by comma*/
    do i=1 to countw(yourvariablename,',');
       /* get the ith "word" separated by commas and assign to an
          element of the array*/
       t[i] = scan(yourvariablename,i,',');
   end;
   /* sort the values this actually places and blanks at the front*/
   call sortc(of t(*));
   /* catx combines values after removing trailing spaces placing the 
      first  parameter, comma in this case between results*/
   yourvariablename = catx(',', of t(*));
  /* drop the unwanted variables the 5 here should match the number of elements defined for the array*/
   drop i  t1-t5;
run;

of t(*) is the way to refer to all elements of an array for those functions that will accept multiple variables.

View solution in original post

4 REPLIES 4
ballardw
Super User

Split the words out into separate variables.

Sort those variables

Recombine the variables in order into the current variable

remove the other variables.

 

You really should provide data in the form of working data step code if you want tested code. at least provide data set names and variable names. Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the </> icon or attached as text to show exactly what you have and that we can test code against.

Untested as no data set provided:

data want;
    set have;
   /* the 5 below should be replaced the maximum number of 
     "words" in the list, 10 should be replaced with the maximum
     length of any of the words
   */
    array t ( 5) $ 10;
   /* countw here gets the number of elements separated by comma*/
    do i=1 to countw(yourvariablename,',');
       /* get the ith "word" separated by commas and assign to an
          element of the array*/
       t[i] = scan(yourvariablename,i,',');
   end;
   /* sort the values this actually places and blanks at the front*/
   call sortc(of t(*));
   /* catx combines values after removing trailing spaces placing the 
      first  parameter, comma in this case between results*/
   yourvariablename = catx(',', of t(*));
  /* drop the unwanted variables the 5 here should match the number of elements defined for the array*/
   drop i  t1-t5;
run;

of t(*) is the way to refer to all elements of an array for those functions that will accept multiple variables.

Kurt_Bremser
Super User

A method where the number of elements must not be determined beforehand:

data long;
set have;
if _n_ = 1 then call symputx("l",vlength(string));
do i = 1 to countw(string,",");
  s = scan(string,i,",");
  output;
end;
keep name s;
run;

proc sort data=long;
by name s;
run;

data want;
set long;
by name;
length string $&l.;
retain string;
if first.name
then string = s;
else string = catx(",",string,s);
if last.name;
keep name string;
run;

SAS INNOVATE 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 171 views
  • 0 likes
  • 3 in conversation