问题描述
在 SAS 中,如果我有如下字符串或数组,
In SAS if I have a string or an Array like the following,
array x[4] $1 ('A' 'B' 'C' 'D');
我需要生成元素的所有唯一"排列,如下所示,
I need to generate all "Unique" permutations of the elements like the following,
[ABCD]
[ABC]
[BCD]
[ACD]
[ABD]
[AB]
[AC]
[AD]
[BC]
[BD]
[CD]
[A]
[B]
[C]
[D]
SAS 中是否有生成所有可能的数组组合的函数?
Is there a function in SAS for generating all possible combinations of the array?
推荐答案
对此略有不同的看法是使用 proc summary
.
A slightly different take on this is to use proc summary
.
创建一个虚拟数据集.将数组的每个元素分配给一个变量,以便我们可以将其输入到 proc summary
:
Create a dummy dataset. Assign each element of the array to a variable so we can feed it into proc summary
:
data tmp;
array arr[*] a b c d (1 1 1 1);
run;
运行proc summary
.
proc summary data=tmp noprint missing;
class a b c d;
output out=combinations;
run;
您还可以使用 proc summary
中的 ways
或 types
语句来限制您可能需要的任何组合.
You can also use the ways
or types
statements in proc summary
to limit any combinations you may want.
现在这样做的一个有趣的副作用是,您也可以在输出数据集中获得 _type_
列.在上面的示例中,将分配以下值:
Now the interesting side effect of doing this is that you get the _type_
column in the output dataset as well. In the example above the following values would be assigned:
D = 1
C = 2
B = 4
A = 8
所以如果输出数据集中的 _type_
值为 13,那么我们知道该行是由 A、B 和 D (8 + 4 + 1) 组合生成的.
So if the _type_
value in the output dataset is 13, then we know that the row was generated by combining A, B and D (8 + 4 + 1).
这篇关于在 SAS 中生成数组的所有唯一排列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!