SAS proc SQL 和数组 [英] SAS proc SQL and arrays

查看:52
本文介绍了SAS proc SQL 和数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个新手 SAS 问题.我有一个数据集,其中包含数值变量 v1-v120V 和一个分类变量 Z(比如三个可能的值).对于 Z 的每个可能值,我想获得另一组变量 w1-w120,其中 w{i}=sum(v{i}}/V,其中总和是 Z 的给定值的总和.因此,在这种情况下,我正在寻找 3*120 矩阵.我可以在数据步骤中执行此操作,但是会喜欢用 Proc SQLProc MEANS 来做,因为实际数据集中的分类变量数量适中.提前致谢.

This is a newbie SAS question. I have a dataset with numerical variables v1-v120, V and a categorical variable Z(with say three possible values). For each possible value of Z, I would like to get another set of variables w1-w120, where w{i}=sum(v{i}}/V, where the sum is a sum over a given value of Z. Thus I am looking for 3*120 matrix in this case. I can do this in data step, but would like to do it by Proc SQL or Proc MEANS, as the number of categorical variables in the actual dataset is moderately large. Thanks in advance.

推荐答案

这是一个使用 proc sql 的解决方案.您也可以使用输出数据集和 'by' 语句对 proc 执行类似的操作.

Here's a solution using proc sql. You could probably also do something similar with proc means using an output dataset and a 'by' statement.

data t1;
    input z v1 v2 v3;
    datalines;
        1 2 3 4
        2 3 4 5
        3 4 5 6
        1 7 8 9
        2 4 7 9
        3 2 2 2
    ;
run;

%macro listForSQL(varstem1, varstem2, numvars);
    %local numWithCommas;
    %let numWithCommas = %eval(&numvars - 1);
    %local i;
    %do i = 1 %to &numWithCommas;
        mean(&varstem1.&i) as &varstem2.&i,
    %end;
    mean(&varstem1.&numvars) as &varstem2.&numvars
%mend listForSQL;

proc sql;
    create table t2 as
        select
            z,
            %listForSQL(v, z, 3)
        from t1
        group by z
    ;
quit;

这篇关于SAS proc SQL 和数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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