SAS:选择多列值的频率

时间:2012-08-13 18:11:30

标签: sql sas proc-sql

我有this问题,但在SAS中。要使用此问题中提供的示例,我有5列名称(name_1,name_2等),并希望输出一个列表,其中的名称按频率的降序列出:

John     502
Robert   388
William  387
...
...       1

我接受了上面引用的问题的答案,并用“proc sql”包围它。和“退出;”:

proc sql;
create table freqs as
SELECT name, COUNT(1)
FROM (           SELECT name_1 AS name FROM mytable
     UNION ALL SELECT name_2 AS name FROM mytable
     UNION ALL SELECT name_3 AS name FROM mytable
     UNION ALL SELECT name_4 AS name FROM mytable
     UNION ALL SELECT name_5 AS name FROM mytable
   ) AS myunion
 GROUP BY name
 ORDER BY COUNT(1) DESC
;
quit;

但我得到了:

ERROR: Summary functions are restricted to the SELECT and HAVING clauses only.

我正在使用SAS 9.2。

思考?谢谢你的帮助!

3 个答案:

答案 0 :(得分:4)

您只需要更改ORDER BY表达式以引用第二列。我还建议您将COUNT表达式结果分配给SAS变量名称(可能是“freq”):

proc sql; 
   create table freqs as 
   SELECT name
        , COUNT(*) as freq
   FROM (
      SELECT           name_1 AS name FROM mytable
      UNION ALL SELECT name_2 AS name FROM mytable
      UNION ALL SELECT name_3 AS name FROM mytable
      UNION ALL SELECT name_4 AS name FROM mytable
      UNION ALL SELECT name_5 AS name FROM mytable
      ) AS myunion  
   GROUP BY name
   ORDER BY freq DESC;
quit; 

仅供参考:您也可以说ORDER BY 2 DESC给出相对参考。

答案 1 :(得分:2)

Proc SQL不允许按顺序计数(1)。试试这个:

proc sql;
    create table freqs as
        SELECT name, COUNT(1) as freqs
        FROM (SELECT name_1 AS name FROM mytable UNION ALL
              SELECT name_2 AS name FROM mytable UNION ALL
              SELECT name_3 AS name FROM mytable UNION ALL
              SELECT name_4 AS name FROM mytable UNION ALL
              SELECT name_5 AS name FROM mytable
             ) AS myunion
         GROUP BY name
         ORDER BY 2 DESC ;
 quit;

我认为它允许列引用。

答案 2 :(得分:0)

如果数据集不是太大,以下内容也可能有效:

data mytable;
 input (name1-name5) (: $17.) @@;
 cards;
 john henry bob jerry james gary bill john mark gabe
 ;
run;

proc sql;
select 'do name = '
||catq("A2SC", name1,name2,name3,name4,name5)
||'; output; end;' into : nlist separated by ' ' from mytable
 ;
quit;

data test;
&nlist
Run;

proc freq order = freq;
tables name;
run;