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
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
;;;;
Is the value a character string? Like "- X = D; K; B; S; O" ?
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;
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
;;;;
Hi andreas,
Thanks for this code and it worked.
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'));
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!
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.
Ready to level-up your skills? Choose your own adventure.