简体   繁体   中英

SAS PROC SQL first value by group

I have a dataset like this:

DATA try;
INPUT month :DATE9. account_id mob;
FORMAT month DATE9.;
DATALINES;
01JAN2004 11 9
01FEB2004 11 10
01MAR2004 11 11
01JUN2008 11 0
01JAN2005 12 -1
01FEB2005 12 -1
01MAR2005 12 0
01APR2005 12 1
01MAY2005 12 2
01JUN2005 12 3
01DEC2006 12 0
;
RUN;

I want to create a variable origination_date such as minimum month - mob date for accounts for which the first (sorted by account_id, month) mob is higher than 0 and minimum month where mob=0 for accounts for which the first mob is lower or equal 0.

The expected output will therefore be:

DATA output;
INPUT month :DATE9. account_id mob origination_date :DATE9.;
FORMAT month DATE9. origination_date DATE9.;
DATALINES;
01JAN2004 11 9 01APR2003
01FEB2004 11 10 01APR2003
01MAR2004 11 11 01APR2003
01JUN2008 11 0 01APR2003
01JAN2005 12 -1 01MAR2005
01FEB2005 12 -1 01MAR2005
01MAR2005 12 0 01MAR2005
01APR2005 12 1 01MAR2005
01MAY2005 12 2 01MAR2005
01JUN2005 12 3 01MAR2005
01DEC2006 12 0 01MAR2005
;
RUN;

So far I have this code:

proc sql;
create table wanted as
select a.*,
coalesce( min(case when a.mob = 0 then a.month else . end),
          min(intnx('month', a.month, -a.mob)) ) as origination_date format date9.
from try a
group by account_id
order by account_id, month;
quit;

But this does not work for the account with account_id = 11 because I don't know how to add the condition on first mob : first mob lower or equal to zero. Do you know how can I achieve this within proc sql ? Basically, I only want to change the code such that min(case when a.mob = 0 then a.month else . end) would apply only for accounts with first mob lower or equal to zero.

I don't know how to do it in one step and without first. and last. variables in data step. May be in some databases you can do it (using Passthrough).

I think using sas you should do it in two steps:

1) Calculate origination_date for every observation depends on conditions.

2) Aggregate variables using proc sql .

data want1;
    set try;
    format origination_date date9.;
    by account_id month;
    retain flag origination_date;
    /*flag var description: 1-higher then 0, -1-less or equal 0*/  
    if first.account_id then do;
        if mob > 0 then flag=1; else flag=-1;
        origination_date="31DEC9999"d;
    end;
    if flag=1 then do;
        if origination_date > intnx('month',month,-mob) then
        origination_date=intnx('month',month,-mob);
    end;
    else do;
        if flag = -1 and mob=0 then do;
            if origination_date > month then
            origination_date=month;
        end;
    end;
run;


proc sql;
    create table want2 as
    select account_id,month,mob,min(origination_date)as origination_date format=date9.
    from want1
    group by account_id
    order by account_id,month;
quit;

OUTPUT:

+------------+-----------+-----+------------------+
| account_id |   month   | mob | origination_date |
+------------+-----------+-----+------------------+
|         11 | 01JAN2004 |   9 | 01APR2003        |
|         11 | 01FEB2004 |  10 | 01APR2003        |
|         11 | 01MAR2004 |  11 | 01APR2003        |
|         11 | 01JUN2008 |   0 | 01APR2003        |
|         12 | 01JAN2005 |  -1 | 01MAR2005        |
|         12 | 01FEB2005 |  -1 | 01MAR2005        |
|         12 | 01MAR2005 |   0 | 01MAR2005        |
|         12 | 01APR2005 |   1 | 01MAR2005        |
|         12 | 01MAY2005 |   2 | 01MAR2005        |
|         12 | 01JUN2005 |   3 | 01MAR2005        |
|         12 | 01DEC2006 |   0 | 01MAR2005        |
+------------+-----------+-----+------------------+

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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