Solved
New Contributor
Posts: 3

# How do I correctly use DO Loops for a recursive process

[ Edited ]

Hi there!

I am trying to create projected revenue streams for loans within a given portfolio.  PROC LOAN didn't seem like the right approach because the data set is vast and each loan can differ in structure.  I'm playing around with DO Loops thinking I can write a process where the calculations fill-in recursively.  Below is the only thing I've gotten to partially work.  It partially works because the variables are actually being created, but it's the same event for every row (see attached current output example).  The amounts are made up so the term and balance remaining won't equate in the example.

Any help would be greatly appreciated!

``````DATA test;
SET schedule;
BY MAST_CTRCT_ID;
BOM_PRINCIPAL = EQUIPMENT_COST;
i=MONTH;
EXIT=TRM_DUR_CT;
DO i=1 to EXIT;
BOM_PRINCIPAL=EQUIPMENT_COST;
INT_PMT=(EQUIPMENT_COST * ((INT_RATE/100)/12));
PP_PMT=CORE_PMT_USD_AMT - (EQUIPMENT_COST * ((INT_RATE/100)/12));
EOM_PRINCIPAL= EQUIPMENT_COST + (EQUIPMENT_COST * ((INT_RATE/100)/12)) - CORE_PMT_USD_AMT;        IF i > TRM_DUR_CT THEN i = EXIT;        END;        DROP i;        DROP EXIT;RUN;``````

Accepted Solutions
Solution
2 weeks ago
New Contributor
Posts: 3

## Re: How do I correctly use DO Loops for a recursive process

Solved it! I needed to add a statement in the beginning of the second DO loop to initiate the counter.

``````DATA test;
SET schedule;
BY MAST_CTRCT_ID MONTH;
IF FIRST.MAST_CTRCT_ID THEN DO;
BOM_PRINCIPAL=EQUIPMENT_COST;
INT_PMT=(EQUIPMENT_COST* ((INT_RATE/100)/12));
PP_PMT=CORE_PMT_USD_AMT - (EQUIPMENT_COST * ((INT_RATE/100)/12));
EOM_PRINCIPAL= (EQUIPMENT_COST + (EQUIPMENT_COST * ((INT_RATE/100)/12)) - CORE_PMT_USD_AMT);
END;
ELSE DO;
EOM_PRINCIPAL + 0;
BOM_PRINCIPAL=EOM_PRINCIPAL;
INT_PMT=BOM_PRINCIPAL * ((INT_RATE/100)/12);
PP_PMT=CORE_PMT_USD_AMT - (BOM_PRINCIPAL * ((INT_RATE/100)/12));
EOM_PRINCIPAL= (BOM_PRINCIPAL + (BOM_PRINCIPAL * ((INT_RATE/100)/12)) - CORE_PMT_USD_AMT);
END;
RUN;``````

All Replies
New Contributor
Posts: 3

## Re: How do I correctly use DO Loops for a recursive process

[ Edited ]

I realized in my first post that I wasn't initializing values for the different variables, so I have added another DO loop.  In the newest version the program correctly sets the starting values but doesn't complete the table.

Clarification of variable names:

Core_Pmt = the payment received that includes principal and interest

BOM_PRINCIPAL = Beginning of the month principal balance

INT_PMT = Interest payment

PP_PMT = Principal payment

EOM_PRINCIPAL = End of month principal balance

``````DATA test;
SET schedule;
BY MAST_CTRCT_ID;
IF MONTH = 1 THEN DO;
BOM_PRINCIPAL=EQUIPMENT_COST;
INT_PMT=(EQUIPMENT_COST* ((INT_RATE/100)/12));
PP_PMT=CORE_PMT_USD_AMT - (EQUIPMENT_COST * ((INT_RATE/100)/12));
EOM_PRINCIPAL= (EQUIPMENT_COST + (EQUIPMENT_COST * ((INT_RATE/100)/12)) - CORE_PMT_USD_AMT);
END;
IF MONTH ne 1 THEN DO;
BOM_PRINCIPAL=LAG(EOM_PRINCIPAL);
INT_PMT=BOM_PRINCIPAL * ((INT_RATE/100)/12);
PP_PMT=CORE_PMT_USD_AMT - (BOM_PRINCIPAL * ((INT_RATE/100)/12));
EOM_PRINCIPAL= (BOM_PRINCIPAL + (BOM_PRINCIPAL * ((INT_RATE/100)/12)) - CORE_PMT_USD_AMT);
END;
RUN;``````
Solution
2 weeks ago
New Contributor
Posts: 3

## Re: How do I correctly use DO Loops for a recursive process

Solved it! I needed to add a statement in the beginning of the second DO loop to initiate the counter.

``````DATA test;
SET schedule;
BY MAST_CTRCT_ID MONTH;
IF FIRST.MAST_CTRCT_ID THEN DO;
BOM_PRINCIPAL=EQUIPMENT_COST;
INT_PMT=(EQUIPMENT_COST* ((INT_RATE/100)/12));
PP_PMT=CORE_PMT_USD_AMT - (EQUIPMENT_COST * ((INT_RATE/100)/12));
EOM_PRINCIPAL= (EQUIPMENT_COST + (EQUIPMENT_COST * ((INT_RATE/100)/12)) - CORE_PMT_USD_AMT);
END;
ELSE DO;
EOM_PRINCIPAL + 0;
BOM_PRINCIPAL=EOM_PRINCIPAL;
INT_PMT=BOM_PRINCIPAL * ((INT_RATE/100)/12);
PP_PMT=CORE_PMT_USD_AMT - (BOM_PRINCIPAL * ((INT_RATE/100)/12));
EOM_PRINCIPAL= (BOM_PRINCIPAL + (BOM_PRINCIPAL * ((INT_RATE/100)/12)) - CORE_PMT_USD_AMT);
END;
RUN;``````
☑ This topic is solved.