将查询结果从SAS导出到Excel

时间:2014-09-19 00:44:28

标签: sql excel sas

我正在将input.txt导入SAS。

该文件的内容是:

SUBJECT GENDER HEIGHT WEIGHT
1 M 68.5 -155 
2 F 61.2 99 
3 F 63.0 115 
4 M 70.0 -205 
5 M 68.6 170 
6 F 65.1 -125 
7 M 72.4 220 
8 F 72.4 220 

我想根据WEIGHT列(如果它们是否为负数)将以下结果导出到Excel:

TOTAL NEGATIVE  % NEGATIVE
  8      -3         37,5%

我想到最简单的方法是创建3个SELECT COUNT(*)查询并将每个查询的结果放入一个变量,然后将这些变量打印到Excel中,但我不知道如何做到这一点准确。

此外,可能有一种最简单的方法。

顺便说一句,我是SAS的新手,几天后我一直在使用它。

任何见解?

2 个答案:

答案 0 :(得分:0)

关于SQL,不需要3个单独的查询。您应该可以使用CASE

在一个查询中完成所有这些操作
select count(*), 
       count(case when weight < 0 then 1 end) negativecount, 
       count(case when weight < 0 then 1 end)/count(*) negativepercentage
from yourtable

应该很容易根据需要格式化百分比。

答案 1 :(得分:0)

PROC SQL;
create table WANT as
select count(*) as total,
sum(weight<0) as negative,
calculated negative/calculated total as percent format=percent8.2
from have;
quit;

导出部分取决于您的环境。您可以通过转到文件&gt;导出并选择Excel作为目标来生成代码。