Hi,
I'm working with a dataset looks something like below:
account | month_end | open_indicator |
1 | 30-Nov-19 | yes |
1 | 31-Dec-19 | no |
1 | 31-Jan-20 | no |
1 | 29-Feb-20 | no |
1 | 31-Mar-20 | no |
2 | 30-Nov-19 | yes |
2 | 31-Dec-19 | yes |
2 | 31-Jan-20 | no |
2 | 29-Feb-20 | no |
2 | 31-Mar-20 | no |
3 | 30-Nov-19 | yes |
3 | 31-Dec-19 | yes |
3 | 31-Jan-20 | yes |
3 | 29-Feb-20 | no |
3 | 31-Mar-20 | no |
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.
account | month_end | open_indicator | close |
1 | 30-Nov-19 | yes | 0 |
1 | 31-Dec-19 | no | 1 |
1 | 31-Jan-20 | no | 0 |
1 | 29-Feb-20 | no | 0 |
1 | 31-Mar-20 | no | 0 |
2 | 30-Nov-19 | yes | 0 |
2 | 31-Dec-19 | yes | 0 |
2 | 31-Jan-20 | no | 1 |
2 | 29-Feb-20 | no | 0 |
2 | 31-Mar-20 | no | 0 |
3 | 30-Nov-19 | yes | 0 |
3 | 31-Dec-19 | yes | 0 |
3 | 31-Jan-20 | yes | 0 |
3 | 29-Feb-20 | no | 1 |
3 | 31-Mar-20 | no | 0 |
Thank you for your help!
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;
/* 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.
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;
Catch the best of SAS Innovate 2025 — anytime, anywhere. Stream powerful keynotes, real-world demos, and game-changing insights from the world’s leading data and AI minds.
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.