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
PROC Star

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
Super User

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
PROC Star

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'));

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
  • 5 replies
  • 285 views
  • 5 likes
  • 5 in conversation