1

如果您能就以下问题给我一些提示(如何操作或查看哪些程序),我将不胜感激:

例如,如果我有一个包含(对于每个品牌)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)和其中一些缺失值的数据集。这就是为什么我想计算所有可能的字符变量组合的数值变量的平均值

非常感谢您的任何想法。

最好的,弗拉达

4

2 回答 2

3

假设我了解您的问题,PROC MEANS 应该可以满足您的需求。

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 交叉发布)

于 2013-05-27T15:23:35.887 回答
3

您将需要阅读有关SAS 程序的内容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”语句中列出的每个变量的 MEAN 统计量都有一个观察值。在此示例中,将有 300 个观察值(您会注意到它大于原始数据集)。

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

  • _ FREQ _ - 组合中的观察数
  • _ TYPE _ - 特定组合的标识符(基于CLASS变量)

_ TYPE _变量在您的情况下特别有用。它是基于类语句中列出的变量数量的数值。因为您有四个类变量,_ TYPE _将有 16 个值,范围从 0 到 15。例如,解释变量Char1Char2组合的 12 个观察值将具有_ TYPE _=12

这是SAS 版本 9.3 中 PROC MEANS 的在线文档的链接。

于 2013-05-27T15:23:47.683 回答