简体   繁体   English

SAS:选择多列值的频率

[英]SAS: Selecting frequency of multi-column value

I have this problem, but in SAS. 我有这个问题,但在SAS。 To use the example provided in this question, I have 5 columns of names (name_1, name_2, etc.), and want to output a list in which the names are listed in descending order of frequency: 要使用此问题中提供的示例,我有5列名称(name_1,name_2等),并希望输出一个列表,其中的名称按频率的降序列出:

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

I took the answer to the question referenced above, and surrounded it with "proc sql;" 我接受了上面引用的问题的答案,并用“proc sql”包围它。 and "quit;": 和“退出;”:

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;

but am getting: 但我得到:

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

I am using SAS 9.2. 我使用的是SAS 9.2。

Thoughts? 思考? Thanks for the help! 谢谢您的帮助!

You just need to change your ORDER BY expression to reference the second column. 您只需要更改ORDER BY表达式以引用第二列。 I'd also suggest that you assign the COUNT expression result to a SAS variable name (perhaps "freq"): 我还建议您将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; 

FYI: You can also say ORDER BY 2 DESC to give a relative reference. 仅供参考:您也可以说ORDER BY 2 DESC给出相对参考。

Proc SQL does not allow the count(1) in the order by. Proc SQL不允许按顺序计数(1)。 Try this instead: 试试这个:

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;

I think it allows the column reference. 我认为它允许列引用。

The following might work as well if the dataset is not too big: 如果数据集不是太大,以下可能也会起作用:

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM