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

不同变量的SAS计数-数据步骤,sas,Sas,我想在没有proc-sql的情况下在数据步骤中做同样的事情。 注意:variable1是一个字符变量 proc sql; select count(distinct(variable1)),variable2,varibale3 from tablename group by variable2,variable3; quit; TIA此解决方案基于这样一种假设,即您实际上希望在不使用PROC SQL的情况下获得结果 将解决方案分解为不同的步骤,使其非常简单且易于

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

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

TIA

此解决方案基于这样一种假设,即您实际上希望在不使用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;

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

我不会使用数据步骤,请使用双proc freq

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;

PROC-FREQ
中的
NLEVELS
选项是获取此信息的最简单方法。假设它是按variable2/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;

为什么是数据步骤而不是过程步骤?我认为您的第二个表应该是
variable2*variable3
?我对数据集进行了排序,但没有使用nodupkey选项,因此它现在无法给出结果fine@rocky如果此解决方案对您有所帮助,请访问此链接