SAS proc SQL and arrays

前端 未结 2 1949
耶瑟儿~
耶瑟儿~ 2021-01-23 18:54

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 v

2条回答
  •  囚心锁ツ
    2021-01-23 19:32

    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;
    

提交回复
热议问题