在SAS中需要相同的输出

时间:2019-07-11 06:14:17

标签: sas

所以我有以下数据:

Data Cricket; 
  input match $; 
  cards; 
    IndVsPak 
    NezVsAus 
    PakVsInd 
    WesVsPak 
    WesVsAus 
    IndVsPak 
    AusVsNez 
; run;

需要的输出:

Match Count 
IndVsPak 3 
NezVsAus 2 
WesVsPak 1 
WesVsAus 1

请帮助提供代码,以何种方式获得上述输出?

3 个答案:

答案 0 :(得分:3)

尝试一下:

Data Cricket; 
  input match $; 
  cards; 
IndVsPak 
NezVsAus 
PakVsInd 
WesVsPak 
WesVsAus 
IndVsPak 
AusVsNez 
;
run;

/*standardise team order within each match - easier to do in data step*/
data temp /view = temp;
  set cricket;
  team1 = substr(match,1,3);
  team2 = substr(match,6,3);
  call sortc(of team:);
  match_sorted = cats(team1,'Vs',team2);
run;

proc sql noprint;
  create table want as
  select match_sorted, count(match_sorted) as freq 
    from temp 
    group by match_sorted
    order by freq descending
   ;
quit;

输出:

  match_
  sorted     freq

 IndVsPak      3
 AusVsNez      2
 AusVsWes      1
 PakVsWes      1

这是我尝试完全在proc sql中进行的操作:

proc sql noprint;
  create table want as
  select 
    ifc(
      team1 < team2, 
      cats(team1, 'Vs', team2), 
      cats(team2, 'Vs', team1)
    ) as match_sorted length=8, 
    count(calculated match_sorted) as freq
    from (
      select
        substr(match,1,3) as team1,
        substr(match,6,3) as team2
        from cricket
    ) 
    group by match_sorted
    order by freq descending
   ;
quit;

它使用一个计算字段-SAS特定的sql功能的一部分。您可以通过将整个内容设置为产生match_sorted的子查询来消除这种情况,也可以展平查询并为所有内容使用计算字段。

答案 1 :(得分:2)

美好的一天,在SAS中,几乎所有事情都是通过 PROCS 完成的。一种执行动作的宏。

在这种情况下,我建议使用 Proc freq

Data Cricket; 
    input match $10.; 
    cards; 
    IndVsPak 
    NezVsAus 
    PakVsInd
    WesVsPak 
    WesVsAus 
    IndVsPak 
    AusVsNez 
; run;


proc freq data=Cricket noprint; 
    table match /  out= freqs ; 
run;

您可以通过删除noprint-option查看输出。

答案 2 :(得分:2)

如果您更喜欢使用SQL,这也将起作用:

PROC SQL;
  SELECT match, count(*) AS cnt FROM cricket GROUP BY match;
QUIT;