sas - SAS中整个表的频率
问题描述
是否可以在 SAS 中获取整个表的频率?例如,我想计算整个表格中有多少是或否?谢谢
解决方案
组件对象具有hash
键并且可以跟踪使用在实例化时提供的标记属性.FIND
指定的键摘要变量中的引用。keysum:
该变量在按每个变量keysum
递增时将计算频率计数。1
suminc:
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;
推荐阅读
- javascript - 我需要帮助弄清楚如何在计算复数时修复输出
- python-3.x - PyQt5 存根文件
- java - 新行的正则表达式匹配
- java - 如何从扩展 SurfaceView 的另一个类开始扩展 SurfaceView 的类
- angular - 显示列表中一位用户的信息
- java - 在 oracle 数据库中的视图上的触发器中调用 java 类
- r - 如何提取数据框中列表的倒数第二个元素并将值放入单独的列中?
- java - 在 fxml 布局中使用常量
- xpath - 在使用下面提到的相对 xpath 时显示错误消息“不是有效的 XPath 表达式”
- python - 如何通过 MQTT 从 lambda 函数接收回消息?