计算给出标准的记录

时间:2014-04-29 12:29:41

标签: sql sas

我放弃了!我一直在努力使这项工作有一段时间,但我无法得到逻辑/或代码。

我要做的是计算定义时间段内每个日期的正在进行的案例。我的桌子基本上看起来像这样:

CaseId   OpenDate   CloseDate
1        01JAN2014  05JAN2014
2        02JAN2014  04JAN2014
3        02JAN2014  .
4        03JAN2014  04JAN2014
5        06JAN2014  08JAN2014
6        07JAN2014  .

我创建了一个数据集,迭代日期从今天-30到今天作为比较日期(CompDate)。

正在进行的案例的定义是(CompDate< = OpenDate和CompDate< CloseDate)或CloseDate =。

我的计划是将日期加入桌子并获得类似的内容

CompDate   OngoingCases
01JAN2014  1
02JAN2014  3
03JAN2014  4
04JAN2014  2
05JAN2014  1
06JAN2014  2
07JAN2014  3

到目前为止,我已经想出了这个代码,它给了我别的东西..

proc sql;
create table Ongoing as
select distinct
a.CompDate,
count(distinct case when (a.CompDate <= datepart(b.caseopendate) and (a.CompDate < datepart(b.caseclosedate) or b.caseclosedate = . )) then b.caseid end) as Cases
from List_of_dates as a
left outer join dcms_cases as b
on a.Date
where
. 
.
group by a.Date
;
quit;

4 个答案:

答案 0 :(得分:1)

我做了一些假设:

1)你的意思是说comp大于开放日期但小于关闭日期或关闭日期缺失

2)您希望最终结果集显示有多少正在进行的案例基于开放日期而不是复合日期,因为复合日期将是一个静态值......基于您的描述。

我在下面的代码中使用了您的示例并将其命名为“AA1”。它会给你下面的结果集。

Data AA1;
  set AA1;
  Format CompDate date9. Ongoing $3.;
  CompDate = today() - 100;  /*Change this to whatever your criteria is for the comparison*/
  If ((CompDate >= opendate) and  (CompDate < closedate)) or (closedate = .) then Ongoing = 'Y'; else Ongoing = 'N';
run;

/*Sort table in order to do a count of the ongoing cases*/

proc sort data = AA1;
  by caseID CompDate Ongoing;
run;

/*Count how many ongoing cases exist based on the Open date values */

data AA1(rename= (count=OngoingCases));
  set AA1;
  count + 1;
  by opendate;
  if first.opendate then count = 1;
run;

/*Clean up to keep the variables you want in your result set */

data final;
  set new(keep=opendate ongoingCases);
run;

enter image description here

答案 1 :(得分:1)

我认为您需要的是在ON语句中加入两个使用日期条件的表格。以下SQL将起到作用:

proc sql;
  create table Ongoing as
  select a.CompDate, count(b.CaseId) as OngoingCases
  from List_of_dates a
  left join dcms_cases b
  on b.OpenDate<=a.CompDate and (b.CloseDate>a.CompDate or b.CloseDate=.)
  group by a.CompDate
  ;
quit;

答案 2 :(得分:0)

一种方法是迭代数据集中每条记录的日期范围,并输出满足条件的记录......


%LET START = today()-30 ;
%LET END   = today() ;

data want ;
  set have ;
  do CompDate = &START to &END ;
    OngoingCase = (OpenDate <= CompDate < CloseDate) 
               or (OpenDate <= CompDate and missing(CloseDate)) ;
    if OngoingCase then output ;
  end ;
  format CompDate date9. ;
run ;

proc summary data=want nway ;
  class CompDate ;
  var OngoingCase ;
  output out=case_sum (drop=_:) sum= ;
run ;

答案 3 :(得分:0)

我稍微修改了当前查询并将CASE条件移动到WHERE子句。

proc sql;
create table Ongoing as
select distinct
a.CompDate,
count(distinct b.caseid ) as Cases
from List_of_dates as a
left outer join dcms_cases as b
on a.Date
where (a.CompDate <= datepart(b.caseopendate) and a.CompDate < datepart(b.caseclosedate)) 
or b.caseclosedate = . 
group by a.Date
;
quit;