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

I am not sure how exactly to explain this.

 

I have a dataset of loans with ids, monthly indicators of time, and monthly indicators of how much is owed.

 

I want to count how many times a given loan occurs in the dataset for the previous 6 months. I also want to calculate the difference between the current observation's amount owed and the mean amount owed over those six months.

 

Here's what the data looks like. I want to calculate pmts and omb_n_change. 

 

loan_idcalendar_timeomb_npmtsomb_n_change
19616583600
1971656461165646 - 165836
1981656462165646 - average of (165646,165836)
1991656463165646 - average of (165646,165646,165836)
111016195700
11111617481161748 - 161957
11121615372161537 - average of (161748,161957)
11131615373161537 - average of (161537,161748,161957)
11141615374161537 - average of (161537,161537,161748,161957)
11151613265161326 - average of (161537,161537,161537,161748,161957)
11161613266161326 - average of (161326,161537,161537,161537,161748,161957)
11171611136161113 - average of (161326,161326,161537,161537,161537,161748)

 

Loan_id is an id for each loan, calendar_time indicates what month that payment was made, and omb_n indicates how much was still owed that month. So basically I want to count how many times loan_id "1" occurred in my data over calendar_time - 6 as well as the mean value of omb_n over that time period (then take the difference between omb_n and that mean).

 

I tried using a hash iterator but am a rookie with those so I got lost before I could really get anything coherent.

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

Assuming your table is not big. Otherwise, you need Hash Table.

 

data have;
input loan_id calendar_time omb_n;
cards;
1 96 165836
1 97 165646
1 98 165646
1 99 165646
1 110 161957
1 111 161748
1 112 161537
1 113 161537
1 114 161537
1 115 161326
1 116 161326
1 117 161113
;

proc sql;
create table want as
select *,
(select count(*) from have where loan_id=a.loan_id and calendar_time between a.calendar_time-1 and a.calendar_time-6) as pmts,
(select mean(omb_n) from have where loan_id=a.loan_id and calendar_time between a.calendar_time-1 and a.calendar_time-6) as average,
 omb_n-calculated average as  omb_n_change
 from have as a
;
quit;

View solution in original post

5 REPLIES 5
KachiM
Rhodochrosite | Level 12

Your calendar_time 117 is more than 6 months from 110. Your illustration for 117 violates 6-month interval.

uwabe3
Calcite | Level 5
Alright well I guess that would be a different number then.
andreas_lds
Jade | Level 19

Title, description and table don't match. I don't see "calculate ... on how many times that ID is present in data". Please explain. And when you do that, post data in usable form and show exactly, what you expect as result. 

Ksharp
Super User

Assuming your table is not big. Otherwise, you need Hash Table.

 

data have;
input loan_id calendar_time omb_n;
cards;
1 96 165836
1 97 165646
1 98 165646
1 99 165646
1 110 161957
1 111 161748
1 112 161537
1 113 161537
1 114 161537
1 115 161326
1 116 161326
1 117 161113
;

proc sql;
create table want as
select *,
(select count(*) from have where loan_id=a.loan_id and calendar_time between a.calendar_time-1 and a.calendar_time-6) as pmts,
(select mean(omb_n) from have where loan_id=a.loan_id and calendar_time between a.calendar_time-1 and a.calendar_time-6) as average,
 omb_n-calculated average as  omb_n_change
 from have as a
;
quit;

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 5 replies
  • 818 views
  • 1 like
  • 4 in conversation