SAS:如何计算平均间隔时间

时间:2018-08-17 01:48:39

标签: sas

如何计算平均间隔时间

Slug

我有一张看起来像这样的表,想为那些未接电话的人计算平均间隔时间。对于此示例,平均间隔时间为:

  

[(18:15:00-12:13:00)+(19:18:00-15:15:00)+(15:15:00-12:15:00)] / 3

它可以在mssql中工作,并且可以为每个客户创建一个column interval_time,然后进行总结。如何在sas中实现它?数据步骤或proc sql

// Use a list since the length of the file is unkown
List<Sleg> slegs = new ArrayList<Sleg>();

File slFile = new File("slFile.txt");

// Use try-with-resources block so the reader is closed automatically,
// no need to use Scanner since we're only interested in reading lines...
try (BufferedReader reader = new BufferedReader(new FileReader("slFile.txt"))) {

    // Read the file line by line
    String line;
    while ((line = reader.readLine()) != null) {
        // Split the line, convert values, and add new sleg.
        String[] numbers = line.trim().split(" ");
        int i = Integer.parseInt(numbers[0]);
        int j = Integer.parseInt(numbers[1]);
        double l = Double.parseDouble(numbers[2]);
        slegs.add(new Sleg(i, j , l));
    }

} catch (FileNotFoundException e) {
    System.out.println(slFile.toString() + " does not exist.");
} catch (IOException e) {
    // Handle any possible IOExceptions as well...
    System.out.println("Unable to read : " + slFile.toString());
} 

1 个答案:

答案 0 :(得分:0)

嵌套查询可用于为所需的选择和计算准备数据。一个重要的功能是认识到customer_id组的日期时间范围(max-min)与所有no的顺序间隔相加相同。

data have;
input customer_id date & yymmdd8. time & time8. answer $ missed_call_type $; 
format date yymmdd10. time time8.;
datetime = dhms(date,hour(time), minute(time), second(time));
format datetime datetime20.;
datalines;
101  2018/8/3  12:13:00  no      employee         
102  2018/8/3  12:15:00  no      customer         
103  2018/8/3  12:20:00  no      employee         
102  2018/8/3  15:15:00  no      customer         
101  2018/8/3  18:15:00  no      employee         
105  2018/8/3  18:18:00  no      customer         
102  2018/8/3  19:18:00  no      employee     
run;

proc sql;
  create table want as 
  select 
    sum(range) / sum (interval_count) as mean_interval_time format=time8.
  , sum(range) as sum_range format=time8.
  , sum(interval_count) as sum_interval_count
  , count(range) as group_count
  from
  ( select 
      max(datetime) - min(datetime) as range
    , count(*) - 1 as interval_count    
    from have
    group by customer_id
    having count(*) > 1
  );

如果答案为“是”,您不会解释会发生什么,因此实际查询可能比此处显示的更为复杂。