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

Hi,

I am using EG running SAS 9.4 version and need to know is they any SAS function to sort a value within variable or any other way to achieve the desired output shown below. Thanks for your help.

Input raw value - X = D; K; B; S; O

Desired output value - X = B; D; K; O; S

 

1 ACCEPTED SOLUTION

Accepted Solutions
andreas_lds
Jade | Level 19

Afaik there is no function that can do this.

Maybe the way the data is read can be changed:

 

data have;
   length Key Value1-Value26 $ 1;
   infile datalines4 delimiter="=;" truncover;
   input Key Value1-Value26;
   
   array values Value1-Value26;
   
   call sortc(of values[*]);
   
   want = catx(' = ', Key, catx(';', of values[*]));
   
   /*drop Key Value:;*/
   
   datalines4;
X = D; K; B; S; O
;;;;

 

View solution in original post

5 REPLIES 5
PeterClemmensen
Tourmaline | Level 20

Is the value a character string? Like "- X = D; K; B; S; O" ?

PaigeMiller
Diamond | Level 26
data have;
    x='D; K; B; S; O;';
run;
data want;
    set have;
    array z{1:100} $ _temporary_;
    do i=1 to countw(x,' ');
        z(i)=scan(x,i,' ');
    end;
    call sortc(of z(*));
    x=catx(' ',of z(*));
    drop i;
run;

 

 

--
Paige Miller
andreas_lds
Jade | Level 19

Afaik there is no function that can do this.

Maybe the way the data is read can be changed:

 

data have;
   length Key Value1-Value26 $ 1;
   infile datalines4 delimiter="=;" truncover;
   input Key Value1-Value26;
   
   array values Value1-Value26;
   
   call sortc(of values[*]);
   
   want = catx(' = ', Key, catx(';', of values[*]));
   
   /*drop Key Value:;*/
   
   datalines4;
X = D; K; B; S; O
;;;;

 

1239
Calcite | Level 5

Hi andreas,

Thanks for this code and it worked.

FreelanceReinh
Jade | Level 19

Hi @1239,

 

If the "raw values" in your real data are very similar to your example, i.e., the items to be sorted are single characters without duplicates (or you want to remove duplicates, if any), you could combine a few functions to get the desired result.

 

Example for single letters (all uppercase as in your example or all lowercase) in an ASCII environment:

x=prxchange('s/(\w)/$1; /',-1,compress(collate(65),x,'k'));

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 1366 views
  • 5 likes
  • 5 in conversation