proc 中每个变量的单独类意味着 [英] Separate class for each variable in proc means

查看:30
本文介绍了proc 中每个变量的单独类意味着的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一次对各种变量求和.此外,对于每个变量 yi 求和,都有一个链接的类变量 xi.

I need to do a sum on various variables at once. Further, for each variabled, yi summed there is a class variable linked, xi.

代码应该是这样的:

proc means data=test sum;
class x1-x100;
var y1-y100;
run;

var y1 没有使用 x1 类,而是使用 x1-x100 作为 var y1 的类,y2,..., y100 也是如此.我如何编写它,使得 var y1 具有类 x1,var y2 具有类 x2 等等?我的输出应该在一张大表中.

Instead of having class x1 for var y1, this uses x1-x100 as class for var y1, same for y2,..., y100. How do I write it, such that var y1 has class x1, var y2 has class x2 and so on? My output should be in one big table.

推荐答案

为什么不直接转置数据,而不是 100 对变量,而是一对变量和 100 行?让我们制作一些示例数据:

Why not just transpose the data so that instead of 100 pairs of variables you have one pair of variables and 100 rows? Let's make some sample data:

data have ;
  input x1-x2 y1-y2 ;
cards;
1 1 3 4
1 2 5 6
2 1 7 8
2 2 9 0
;

现在通过将 X 转换为 CLASS 变量并将 Y 转换为 VALUE 变量来转置它.我们将使用 INDEX 来记录这个观察结果代表了许多类/值对中的哪一个.

Now transpose it by turning the X's into CLASS variable and the Y's into VALUE variable. We will use INDEX to note which of the many class/value pairs this observation represents.

data tall ;
  set have ;
  array x x1-x2;
  array y y1-y2;
  do index=1 to dim(x);
     class=x(index);
     value=y(index);
     output;
  end;
run;

现在我们只使用PROC summary(又名PROC MEANS)来计算总和.

Now we just using PROC SUMMARY (aka PROC MEANS) to calculate the sum.

proc summary nway;
  class index class ;
  var value;
  output out=want sum=sum;
run;

我们可以打印结果:

proc print ;
  var index class sum ;
run;

  index    class    sum
    1        1        8
    1        2       16
    2        1       12
    2        2        6

您也可以在 PROC summary 之后进行转置,假设组合数量不超过可用内存量.

You could also do the transpose AFTER the PROC SUMMARY, assuming that the number of combinations does not exceed the amount of memory available.

首先使用 WAYS 1 运行 PROC 总结以限制使用的类变量的数量.使用 CHARTYPE 可以同时允许多个类变量.为了使结果以 X1,X2,...X100 的顺序出现,请以相反的顺序引用 CLASSVAR 语句中的变量.

First run the PROC SUMMARY using WAYS 1 to limit the number of class variables used. Use CHARTYPE to allow so many class variables at once. To get the results to appear in the order X1,X2,...X100 reference the variables in the CLASS and VAR statement in the opposite order.

proc summary data=have chartype ;
  class x2-x1;
  ways 1;
  var y2-y1;
  output out=step1 sum= ;
run;

现在获取结果并使用 _TYPE_ 变量将类变量与正确的 sum 变量匹配.

Now take the result and use the _TYPE_ variable to match the class variable with the proper sum variable.

data want ; 
  length class_name sum_name $32 ;
  set step1 ;
  array x x2-x1 ;
  array y y2-y1 ;
  i=indexc(_type_,'1') ;
  class_name = vname(x(i));
  sum_name=vname(y(i));
  class=x(i);
  sum=y(i);
  drop _type_ i x: y: ;
run;

结果:

class_
name     sum_name    _FREQ_    class    sum
 x1         y1          2        1        8
 x1         y1          2        2       16
 x2         y2          2        1       12
 x2         y2          2        2        6

这篇关于proc 中每个变量的单独类意味着的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆