proc sql用于创建具有计数值的列

时间:2014-02-22 13:27:04

标签: sql sas

我有一个名为mydata的数据集,其中包含变量:日期,唯一ID,x1,x2,x3和x4。

proc sql;
create table cat&yyyymmdd
as select unique ID, sum(x1+x2)/count(*) as PART1, sum(x3+x4)/count(*) as   from mydata&yyyymmdd group by unique ID;
run;

我希望在我的最终表中有part1,part2两个计数列(count1和count2)。基本上,按日期对每个唯一ID的PART1和PART2值对应的count(*)值。

希望这是有道理的。请帮忙!感谢..

1 个答案:

答案 0 :(得分:0)

count(*)值将是相同的 - 它们将是两种情况下ID的非唯一值的计数。以下查询说明(我猜):

proc sql;
create table cat&yyyymmdd as
   select UNIQUE_ID /* assume is correct spelling of "unique ID" */
      , sum(x1+x2)/count(*) as PART1
      , sum(x3+x4)/count(*) as PART2
      , count(*) as count1
      , count(*) as count2 /* !! */
  from mydata&yyyymmdd 
  group by 1; /* more robust syntax */
run; /* should be a quit; but a quit; is not necessary either */

请注意,“select UNIQUE ID”无效语法,但在创建索引时允许使用UNIQUE关键字:

CREATE <UNIQUE> INDEX index-name
  ON table-name ( column <, ... column>);