简体   繁体   English

在每个时间段的每个 ID 中查找 SAS 中变量的最大值

[英]Finding the max value of a variable in SAS per ID per time period

 proc sql;
create table abc as select distinct formatted_date ,Contract, late_days
from merged_dpd_raw_2602
group by 1,2
;quit;

this gives me the 3 variables I\m working with这给了我正在使用的 3 个变量

they have the form他们有形式

|ID|Date in YYMMs.10| |ID|YYMMs.10 中的日期| number|号码|

proc sql;
create table max_dpd_per_contract as select distinct contract, max(late_days) as DPD_for_contract
from sasa
group by 1
;quit;

this gives me the maximum number for the entire period but how do I go on to make it per period?这给了我整个期间的最大数量,但我如何 go 在每个期间实现它?

I'm guessing the timeseries procedure should be used here.我猜应该在这里使用时间序列过程。

proc timeseries data=sasa
                out=sasa2;
by contract;
id formatted_date interval=day ACCUMULATE=maximum ;
trend maximum ;

var late_days;

run;

but I am unsure how to continue.但我不确定如何继续。

I want to to find the maximum value of the variable "late days" per a given time period(month).我想找到每个给定时间段(月)变量“迟到”的最大值。 So for contact A for the time period jan2018 the max late_days value is X.因此,对于 jan2018 期间的联系人 A,最大 late_days 值为 X。

how the data looks: https://imgur.com/iIufDAx数据看起来如何: https://imgur.com/iIufDAx

In SQL you will want to calculate your aggregate within a group that uses a computed month value.在 SQL 中,您将需要在使用计算的月份值的组内计算聚合。

Example:例子:

data have;
  call streaminit(2021);

  length contract date days_late 8;

  do contract = 1 to 10;
    days_late = 0;
    do date = '01jan2020'd to '31dec2020'd;
      if days_late then 
        if rand('uniform') < .55 then 
          days_late + 1;
        else
          days_late = 0;
      else
        days_late + rand('uniform') < 0.25;

      output;
    end;
  end;
  format date date9.;
run;

options fmterr;

proc sql;
  create table want as 
  select 
    contract
  , intnx('month', date, 0) as month format = monyy7.
  , max(days_late) as max_days_late
  from
    have
  group by
    contract, month
  ;

You will get the same results using Proc MEANS您将使用Proc MEANS获得相同的结果

proc means nway data=have noprint; 
  class contract date;
  format date monyy7.;
  output out=want_2 max(days_late) = max_days_late;
run;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM