最佳答案
乔是对的 - 没有内置函数可以执行此操作。我可以看到您在这里有两个选择:
- 将字符串拆分为数组并使用
call sortc
对数组进行排序。只要您首先定义了一个足够长度的数组,您就可以使用call pokelong
轻松地完成此操作。 - 实现 sorting algorithm你的选择。如果您选择走这条路,我建议使用
=
符号左侧的substr
来更改单个字符,而无需重写整个字符串。
以下是您可以如何执行#1 的示例。 #2 需要做更多的工作。
data _null_;
myword = 'apple';
array letters[5] $1;
call pokelong(myword,addrlong(letters1),5); /*Limit # of chars to copy to the length of array*/
call sortc(of letters[*]);
myword = cat(of letters[*]);
putlog _all_;
run;
注意对于此处使用的长度为 5 的数组,请确保在使用 call pokelong
时仅将字符串的前 5 个字符写入数组开头的内存,以避免溢出超出数组的末尾数组 - 否则,在处理 myword
的较长值时,您可能会覆盖内存的其他任意部分。这可能会导致不良的副作用,例如应用程序/系统崩溃。此外,这种填充数组的技术在 SAS University Edition 中不起作用 - 如果您使用该技术,则需要使用 do 循环。
我对此做了一个小测试 - 使用一台几年前的 PC 的单个 CPU 对长度为 100 的 2m 个随机单词(由从整个 ASCII 可打印范围中选择的字符组成)进行排序大约需要 15 秒 - 比它稍微少一点的时间创建测试数据集所需的时间。
data have;
length myword $100;
do i = 1 to 2000000;
do j = 1 to 100;
substr(myword,j,1) = byte(32 + int(ranuni(1) * (126 - 32)));
end;
output;
end;
drop i j;
run;
data want;
set have;
array letters[100] $1;
call pokelong(myword,addrlong(letters1),100); /*Limit # of chars to copy to the length of array*/
call sortc(of letters[*]);
myword = cat(of letters[*]);
drop letters:;
run;
关于string - 按字母顺序对字符串中的字母进行排序 - SAS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43812674/