BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Tony_N
Calcite | Level 5

Hi,

I'm working with a dataset looks something like below:

accountmonth_endopen_indicator
130-Nov-19yes
131-Dec-19no
131-Jan-20no
129-Feb-20no
131-Mar-20no
230-Nov-19yes
231-Dec-19yes
231-Jan-20no
229-Feb-20no
231-Mar-20no
330-Nov-19yes
331-Dec-19yes
331-Jan-20yes
329-Feb-20no
331-Mar-20no

 

Field description

Account: unique account number

Month_end: date

Open_indicator: indicates whether an account is open or close at the end of the month

 

I'd like to get help with creating an additional column that gives a flag, let's say 1, ONLY on the first time that an account close, otherwise 0. Below is example of the output i'd like to achieve, where "close" field contains value 1 when an account closes for the first time.

accountmonth_endopen_indicatorclose
130-Nov-19yes0
131-Dec-19no1
131-Jan-20no0
129-Feb-20no0
131-Mar-20no0
230-Nov-19yes0
231-Dec-19yes0
231-Jan-20no1
229-Feb-20no0
231-Mar-20no0
330-Nov-19yes0
331-Dec-19yes0
331-Jan-20yes0
329-Feb-20no1
331-Mar-20no0

 

Thank you for your help!

1 ACCEPTED SOLUTION

Accepted Solutions
andreas_lds
Jade | Level 19

Posting data in usable form makes it easier to suggest code, so the following step in untested an i assume that data is sorted by account:

data want;
    set have;
    by account;
    length close isClosed 8;
    retain isClosed;
    drop isClosed;

    if first.account then do;
        isClosed = 0;
    end;

    if not isClosed and open_indicator = 'no' then do;
        close = 1;
        isClosed = 1;
    end;
    else do;
        close = 0;
    end;
run;

View solution in original post

5 REPLIES 5
PaigeMiller
Diamond | Level 26
/* UNTESTED CODE */
data want;
    set have;
    by account descending open_indicator;
    if first.open_indicator and open_indicator='no' then close=1;
    else close=0;
run;

Assumes the data set is properly sorted by account and month_end, and that once an account closes, it can't re-open.

--
Paige Miller
Tony_N
Calcite | Level 5
I got this error "BY variables are not properly sorted on data set", although account and month_end were sorted.
andreas_lds
Jade | Level 19

Posting data in usable form makes it easier to suggest code, so the following step in untested an i assume that data is sorted by account:

data want;
    set have;
    by account;
    length close isClosed 8;
    retain isClosed;
    drop isClosed;

    if first.account then do;
        isClosed = 0;
    end;

    if not isClosed and open_indicator = 'no' then do;
        close = 1;
        isClosed = 1;
    end;
    else do;
        close = 0;
    end;
run;
Tony_N
Calcite | Level 5
Your code works perfectly andreas_lds. Thank you so much!
Tony_N
Calcite | Level 5
Hi andreas_lds,

I'm trying to understand how the code you suggested works (since it worked nicely and I'd like to reuse it in a different situation). Would you mind explaining what the following snippet from the code does:

if not isClosed and open_indicator = 'no' then do;
close = 1;
isClosed = 1;
end;

Thanks