BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
sas51
Fluorite | Level 6
Hi all, I am currently studying a data on accounts defaulting on their payment, to put it simply, my data has two two fields:
Account Number (ACCOUNT) , Default Position (DEFAULT).

Default Position is a range of numbers tagged to a date, i.e., 1 as 31December2013, 2 as 31January2014 etc.

Apologies if my explanation is rather messy. To put in simpler terms, I want to mark (keep) the default position of each account and track for 10 positions, any default positions in between are ignored (drop), and then upon ending the tracking for 10 positions before, I'll search for the next upcoming default and then marked and the process continues.

Example of my data looks like this:
ACCOUNT DEFAULT
12345 2
12345 3
12345 15
12345 16
12345 35
23456 1
...

Using the Account Number 12345 above as my example:

I would keep the first record (default position 2) as this is the first default, and I will want to track for 10 months up to position 12. I don't want to keep the second record (default position 3) as the distance from the first record default position is still <10 positions. After tracking for 10 months from default position 2, I would look for the next default position, and I want to keep the third record as default position 15 is now >10 positions away from my first default position (default position 2+10). After, I will track for 10 positions from my new default position 15, hence I will not keep my forth record as default position 16 is still <10 positions away from my new record of default (default 15+10). I will track my default for 10 months from default position 15 up to 15+10. Finally, as the previous default tracking ended, I will search for the next upcoming default event and I will keep the fifth record as it is >10 position away from my default 15.

I'm trying to do it with a do loop but I can't seem to manage after the first do iteration.

Would kindly appreciate assistance on this. Thank you in advance.
1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

No DO loop necessary, the data step loops over observations on its own.

Use a retained variable to keep the start point for each period:

data have;
input account default;
datalines;
12345 2
12345 3
12345 15
12345 16
12345 35
23456 1
;

data want;
set have;
by account;
retain start;
if first.account
then do;
  start = default;
  output;
end;
else do;
  if default gt start + 10
  then do;
    output;
    start = default;
  end;
end;
drop start;
run;

View solution in original post

1 REPLY 1
Kurt_Bremser
Super User

No DO loop necessary, the data step loops over observations on its own.

Use a retained variable to keep the start point for each period:

data have;
input account default;
datalines;
12345 2
12345 3
12345 15
12345 16
12345 35
23456 1
;

data want;
set have;
by account;
retain start;
if first.account
then do;
  start = default;
  output;
end;
else do;
  if default gt start + 10
  then do;
    output;
    start = default;
  end;
end;
drop start;
run;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 1 reply
  • 293 views
  • 2 likes
  • 2 in conversation