不同变量的SAS计数 - 数据步骤

时间:2017-05-05 16:56:01

标签: sas

我想在没有proc sql的数据步骤中做同样的事情。 注意:variable1是一个字符变量

proc sql;
 select count(distinct(variable1)),variable2,varibale3      
        from tablename group by variable2,variable3;
quit;

TIA

3 个答案:

答案 0 :(得分:0)

此解决方案基于您不使用PROC SQL实际需要结果的假设。

在不同的步骤中打破解决方案,让您更容易理解。: -

/*Step 1 This step just keeps the required variables in the dataset*/

    data tablename(keep=variable1 variable2 variable3);
    set tablename;
    run;

/*Step 2 Sorting and using noduprecs to keep distinct records in the table*/ 
    Proc sort data=tablename nodupkey;
    by _all_;
    run;

/*Step 3 Creating a tag as 1, so as to enable us to sum or count the distinct values*/
    Data tablename;
    set tablename;
    tag=1;
    run;

/*Step 4 Using Proc means to sum the tag inside var on the combination of Variable1 and Variable3 which is inside the Class*/  

/*I have used PROC MEANS here. You can do the same thing using PROC TABULATE or PROC REPORT*/

    proc means data=tablename sum;
      class variable2 variable3;
      var tag;
    run;

如果您还有任何问题,请告诉我

答案 1 :(得分:0)

我不会使用数据步骤,请使用双proc频率。

proc freq data=have noprint;
table variable2*variable3*variable1 /out= first_summary;
run;

proc freq data=first_summary noprint;
table variable2*variable3 / out=want;
run;

proc print data=want;
run;

答案 2 :(得分:0)

NLEVELS中的

PROC FREQ选项是获得此选项的最简单方法。假设它按变量2 / variable3排序,它很简单。以下是使用sashelp.class的示例;这里sex代表变量2和变量3,而age代表计数变量。

proc sort data=sashelp.class out=class;
by sex;
run;

ods output nlevels=levels_age_sex;  *nlevels table is output;
proc freq data=class nlevels;       *nlevels here asked for output;
by sex;                             *grouping variable;
tables age/noprint;                 *do not actually want table;
run;
ods output close;                   *technically unnecessary but I like symmetry;