SAS:为给定变量集的所有组合创建平均值

SAS:为给定变量集的所有组合创建平均值,sas,Sas,如果您能给我一些提示,告诉我如何做,或者看一下哪些程序,我将非常感激 关于以下问题: 例如,如果我有一个包含每个品牌4个字符变量和3个数字变量的数据集,那么我想根据字符变量的所有可能组合来计算数字变量的几个平均值,不管某些字符变量是否缺失 Brand Char1 Char2 Char3 Char4 NumVar1 NumVar2 NumVar3 A a xx 3 a 0.471 0.304 0.267 A b xy 3 s 0.08

如果您能给我一些提示,告诉我如何做,或者看一下哪些程序,我将非常感激 关于以下问题:

例如,如果我有一个包含每个品牌4个字符变量和3个数字变量的数据集,那么我想根据字符变量的所有可能组合来计算数字变量的几个平均值,不管某些字符变量是否缺失

 Brand  Char1 Char2 Char3 Char4 NumVar1 NumVar2 NumVar3
    A   a   xx  3   a   0.471   0.304   0.267
    A   b   xy  3   s   0.086   0.702   0.872
    A   c   xz  3   a   0.751   0.962   0.080
    A   d   xx  2   s   0.711   0.229   0.474
    A   a   xy  3   a   0.160   0.543   0.256
    A   b   xz  1   s   0.200   0.633   0.241
    A   c   xx  3   a   0.765   0.511   0.045
    A   d   xy  4   s   0.397   0.815   0.950
    A   a   xz  1   a   0.890   0.757   0.483
    A   b   xx  3   a   0.575   0.625   0.341
    A   c   xy  3   a   0.595   0.047   0.584
    A   d   xz  1   s   0.473   0.806   0.329
    A   a   xx  2   s   0.062   0.161   0.018
    A   b   xy  2   s   0.935   0.990   0.072
    A   c   xz  4   s   0.564   0.490   0.112
    A   d   xx  2   a   0.251   0.228   0.215
    A   a   xy  4   a   0.551   0.778   0.605
    A   b   xz  1   s   0.887   0.392   0.866
    A   c   xx  1   s   0.238   0.569   0.245
    A   d   xz  1   a   0.736   0.961   0.627
因此,我想计算以下内容,这些内容不是写在sas符号中,而是逻辑上:

%let numeric_var = NumVar1 NumVar2 NumVar3; *macro of all numerical variables;

*compute mean values for each NumVar by all combinations of Char.variables;

compute mean(&numeric_var) by Char1 Char2 Char3 Char4 
compute mean(&numeric_var) by Char1 Char2 Char3 
compute mean(&numeric_var) by Char1 Char2 
compute mean(&numeric_var) by Char1 
compute mean(&numeric_var) by Char1 Char2       Char4
compute mean(&numeric_var) by Char1             Char4
compute mean(&numeric_var) by Char1       Char3 Char4  
etc.
sas中有没有比手工输入所有这些组合更有效的方法来计算所有这些平均值

原则上,最后我想合并两个数据集:一个数据集如上所述;另一个数据集只有字符变量品牌Char1 Char2 Char3 Char4,其中一些变量缺少值。这就是为什么我想计算所有可能的字符变量组合中数值变量的平均值

非常感谢您的建议

最好的,
Vlada

PROC的意思是,如果我理解你的问题,你应该完成你需要的

proc means data=have;
class char1 char2 char3 char4;
types char1*char2*char3*char4 
      char1*char2*char3 
      char2*char3*char4 ... etc... ; *or use the various WAYS statements to get all combinations of a particular number of variables, or use _ALL_ to get all combinations;
var num1 num2 num3;
output out=want mean=;
run;
如果字符变量可能缺少值,则需要使用/missing;在CLASS语句上


基本上是从SAS-L交叉发布的

PROC意味着,假设我理解您的问题,您应该完成您需要的任务

proc means data=have;
class char1 char2 char3 char4;
types char1*char2*char3*char4 
      char1*char2*char3 
      char2*char3*char4 ... etc... ; *or use the various WAYS statements to get all combinations of a particular number of variables, or use _ALL_ to get all combinations;
var num1 num2 num3;
output out=want mean=;
run;
如果字符变量可能缺少值,则需要使用/missing;在CLASS语句上


基本上是从SAS-L交叉发布的

你会想读一些关于PROC MEANS的书,这是我最喜欢的SAS程序之一。例如,考虑这一点:

data have;
   input Brand $ Char1 $ Char2 $ Char3 $ Char4 $
         NumVar1 NumVar2 NumVar3;
   datalines;
    A   a   xx  3   a   0.471   0.304   0.267
    A   b   xy  3   s   0.086   0.702   0.872
    A   c   xz  3   a   0.751   0.962   0.080
    A   d   xx  2   s   0.711   0.229   0.474
    A   a   xy  3   a   0.160   0.543   0.256
    A   b   xz  1   s   0.200   0.633   0.241
    A   c   xx  3   a   0.765   0.511   0.045
    A   d   xy  4   s   0.397   0.815   0.950
    A   a   xz  1   a   0.890   0.757   0.483
    A   b   xx  3   a   0.575   0.625   0.341
    A   c   xy  3   a   0.595   0.047   0.584
    A   d   xz  1   s   0.473   0.806   0.329
    A   a   xx  2   s   0.062   0.161   0.018
    A   b   xy  2   s   0.935   0.990   0.072
    A   c   xz  4   s   0.564   0.490   0.112
    A   d   xx  2   a   0.251   0.228   0.215
    A   a   xy  4   a   0.551   0.778   0.605
    A   b   xz  1   s   0.887   0.392   0.866
    A   c   xx  1   s   0.238   0.569   0.245
    A   d   xz  1   a   0.736   0.961   0.627
run;

proc means noprint data=have completetypes;
   class Char1 Char2 Char3 Char4;
   var NumVar1 NumVar2 NumVar3;
   output out=want mean=mNumVar1 mNumVar2 mNumVar3;
run;
如前所述,该过程将创建一个名为want的输出数据集,对class语句中列出的每个变量组合进行一次观察,并对var语句中列出的每个变量进行平均统计。在本例中,将有300个观测值,您将注意到它们比原始数据集大

此外,输出数据集将包含两个自动变量:

_频率-组合中的观察数 _类型\基于类变量的特定组合的标识符 _TYPE_u变量在您的情况下特别有用。它是一个基于class语句中列出的变量数量的数值。因为您有四个类变量,_TYPE_uu将有16个值,范围从0到15。例如,考虑变量Char1和Char2组合的12个观测值将具有_TYPE_u=12


到SAS 9.3版中PROC MEANS的在线文档。

您将需要阅读一些有关PROC MEANS的内容,这是我最喜欢的SAS程序之一。例如,考虑这一点:

data have;
   input Brand $ Char1 $ Char2 $ Char3 $ Char4 $
         NumVar1 NumVar2 NumVar3;
   datalines;
    A   a   xx  3   a   0.471   0.304   0.267
    A   b   xy  3   s   0.086   0.702   0.872
    A   c   xz  3   a   0.751   0.962   0.080
    A   d   xx  2   s   0.711   0.229   0.474
    A   a   xy  3   a   0.160   0.543   0.256
    A   b   xz  1   s   0.200   0.633   0.241
    A   c   xx  3   a   0.765   0.511   0.045
    A   d   xy  4   s   0.397   0.815   0.950
    A   a   xz  1   a   0.890   0.757   0.483
    A   b   xx  3   a   0.575   0.625   0.341
    A   c   xy  3   a   0.595   0.047   0.584
    A   d   xz  1   s   0.473   0.806   0.329
    A   a   xx  2   s   0.062   0.161   0.018
    A   b   xy  2   s   0.935   0.990   0.072
    A   c   xz  4   s   0.564   0.490   0.112
    A   d   xx  2   a   0.251   0.228   0.215
    A   a   xy  4   a   0.551   0.778   0.605
    A   b   xz  1   s   0.887   0.392   0.866
    A   c   xx  1   s   0.238   0.569   0.245
    A   d   xz  1   a   0.736   0.961   0.627
run;

proc means noprint data=have completetypes;
   class Char1 Char2 Char3 Char4;
   var NumVar1 NumVar2 NumVar3;
   output out=want mean=mNumVar1 mNumVar2 mNumVar3;
run;
如前所述,该过程将创建一个名为want的输出数据集,对class语句中列出的每个变量组合进行一次观察,并对var语句中列出的每个变量进行平均统计。在本例中,将有300个观测值,您将注意到它们比原始数据集大

此外,输出数据集将包含两个自动变量:

_频率-组合中的观察数 _类型\基于类变量的特定组合的标识符 _TYPE_u变量在您的情况下特别有用。它是一个基于class语句中列出的变量数量的数值。因为您有四个类变量,_TYPE_uu将有16个值,范围从0到15。例如,考虑变量Char1和Char2组合的12个观测值将具有_TYPE_u=12


在SAS 9.3版中,将同一个问题在多个论坛上交叉发布并没有错,但您可能会注意到,您这样做是为了在一个地方得到更好的答案,在另一个论坛上找到他们的搜索者能够找到解决方案。在多个论坛上交叉发布同一个问题没有错,但你可能会注意到你这样做了,如果你在一个地方得到了更好的答案,在其他论坛上找到它们的搜索者能够找到解决方案。正如Joe在下面提到的,您可能应该使用missing来捕获任何缺少变量的组合。正如Joe在下面提到的,您可能应该使用missing来捕获任何缺少变量的组合。