首页 > 解决方案 > SAS中整个表的频率

问题描述

是否可以在 SAS 中获取整个表的频率?例如,我想计算整个表格中有多少是或否?谢谢

标签: sasfrequency

解决方案


组件对象具有hash键并且可以跟踪使用在实例化时提供的标记属性.FIND指定的键摘要变量中的引用。keysum:该变量在按每个变量keysum递增时将计算频率计数。1suminc:

data have;
  * Words array from Abstract;
  * "How Do I Love Hash Tables? Let Me Count The Ways!";
  * by Judy Loren, Health Dialog Analytic Solutions;
  * SGF 2008 - Beyond the Basics;
  * https://support.sas.com/resources/papers/proceedings/pdfs/sgf2008/029-2008.pdf;

  array words(17) $10 _temporary_ (
    'I' 'love' 'hash' 'tables'
    'You' 'will' 'too' 'after' 'you' 'see'
    'what' 'they' 'can' 'do' '--' 'Judy' 'Loren'
  );


  call streaminit(123);
  do row = 1 to 127;
    attrib  RESPONSE1-RESPONSE20 length = $10;
    array RESPONSE RESPONSE1-RESPONSE20;
    do over RESPONSE;
      RESPONSE = words(rand('integer', 1, dim(words)));
    end;
    output;
  end;
run;

data _null_;
  set have;

  if _n_ = 1 then do;
    length term $10;
    call missing (term);
    retain one 1;
    retain count 0;

    declare hash bins(suminc:'one', keysum:'count');
    bins.defineKey('term');
    bins.defineData('term');
    bins.defineDone();
  end;

  set have end=lastrow;
  array response response1-response20;

  do over response;
    if bins.find(key:response) ne 0 then do;
      bins.add(key:response, data:response, data:1);
    end;
  end;

  if lastrow;

  bins.output(dataset:'all_freq');
run;

频率表


原始答案,假设只有是和否

是的。您可以排列值,为每个 No/Yes 值计算为 0/1 标志,然后使用 SUM 计算 0 和 1。SUM 仅在处理 0 和 1 时才计算 FREQ。

例子:

data have;
  call streaminit(123);
  do row = 1 to 100;
    attrib  ANSWER1-ANSWER20 length = $3;
    array ANSWER ANSWER1-ANSWER20;
    do over ANSWER; ANSWER = ifc(rand('uniform') > 0.15,'Yes','No'); end;
    output;
  end;
run;

data want(keep=freq_1 freq_0);
  set have end=lastrow;
  array ANSWER ANSWER1-ANSWER20;
  array X(20) _temporary_;

  do over ANSWER; x(_I_) = ANSWER = 'Yes'; end;

  freq_1          + sum (of X(*));
  freq_0 + dim(X) - sum (of X(*));

  if lastrow;
run;

在此处输入图像描述


推荐阅读