【问题标题】:Calculate a new field from multiple observations sas从多个观测值 sas 中计算一个新字段
【发布时间】:2016-03-14 11:23:38
【问题描述】:

我正在努力寻找在 SAS 中如何最好地处理示例数据中“alex”在 2015 年有两个订单的情况,因此我希望出现一个新列,他的状态 =“活动”

有什么有用的提示可以让我走上正轨吗?谢谢

 ID     first_order      Order
 alex      01/01/2013     23/01/2015
 alex      01/01/2013     23/01/2015 
 alex      01/01/2013      03/04/2013

【问题讨论】:

  • 您希望status 列仅在 2015 年期间显示为活动状态?
  • 除了赋值变量还有什么难点?
  • 您可以使用 INTNX、DATDIF、...

标签: sas


【解决方案1】:

在此示例中,假设 Alex 在 2013 年下了一个订单,在 2015 年下了另一个订单。Bob 在 2013 年只下了一个订单。让我们创建一些数据:

data have;
    format id $4. first_order Order date9.;
    id = 'Alex'; first_order = '01JAN2013'd; Order = '23JAN2015'd; output;
    id = 'Alex'; first_order = '01JAN2013'd; Order = '03APR2013'd; output;
    id = 'Bob';  first_order = '01JAN2013'd; Order = '01JAN2013'd; output;
run;

下面的宏可用于得出客户在任何给定年份的状态。

%macro Active(year=);

proc sql;
    create table CustomerStatus as
    select id,
           case when max(year(Order)) = &year then 'Active' 
           else 'Inactive' end as Status_in_&year length=8 
    from have
    group by id
    order by id;

    create table want as
    select have.id, have.first_order, have.Order, CustomerStatus.Status_in_&year
    from have inner join CustomerStatus on have.id = CustomerStatus.id
    order by CustomerStatus.id;
quit;

%mend Active;

%Active(year=2015);

在表 CustomerStatus 中,我们获取所有客户最近一次订单的年份。如果这等于订单的年份,则客户状态为有效。最后将状态与 Orders 表连接起来。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-12
    • 1970-01-01
    • 1970-01-01
    • 2022-12-05
    • 2015-10-19
    • 1970-01-01
    • 2023-03-14
    相关资源
    最近更新 更多