Contributor
Posts: 20

# SAS Macro Do Loop

I have a cross-sectional time series data set. I have a cusip and time id. I would like to write a loop that performs a calculation every time period until the stopping command for every cusip. For example the data set looks like this:

Cusip   Time   Yield  Coupon    Principal

x9900   1         0.02   0.01         10,000

x9900   2         0.02   0.015          10,000

x9900   3         0.02    0.022         10,000

5H33    1         0.03     0.025         5,000

5h33    2          0.03    0.033         5,000

5h33    3          0.03    0.029         5,000

I would like to write a loop that multiplies Principal times coupon for every time period (by cusipu) but stops when the coupon is greater than the yield. The reason I would like a loop is because I want the calculation of principal times coupon to stop permanently for a cusip when the coupon is greater than the yield. For example, for the cusip 5h33 the coupon is greater than the yield at time 2. So the calculation should stop at time 2. However, at time 3 the coupon is less than the yield. Without a loop the multiplication continues, however, I want it to stop.

Any help please would be great.

Todd Feldman

Assistant Professor

SAS Super FREQ
Posts: 9,365

## Re: SAS Macro Do Loop

Hi:

I don't understand what you need a Macro loop for. What is your desired output? The way I understand your explanation, you would only multiply on two of these observations:

Cusip    Time    Yield    Coupon    Principal     do_this  Result?

x9900#      1      0.02     0.010      10,000      no mult    ??

x9900#      2      0.02     0.015      10,000      no mult    ??

x9900#      3      0.02     0.022      10,000      multiply   ??

5H33       1      0.03     0.025       5,000      no mult    ??

5h33       2      0.03     0.033       5,000      multiply   ??

5h33       3      0.03     0.029       5,000      no mult    ??

But if you have 6 rows in your data, what do you envision on each row? A calculated field that is cumulative based on multiplying or not multiplying??? And for each CUSIP, the calculated amount starts over at 0? Can you explain what you would want to see at the end of this process?? 6 rows in and 6 rows out or 6 rows in and 2 rows out (one for each CUSIP)?

cynthia

Super User
Posts: 23,663

## Re: SAS Macro Do Loop

Cynthia is correct you don't need macros. What you do need is to set a flag when the coupon is greater than yield and then retain it until the next by group. At the start of the next by group you set the flag back to the initial value.

Key ideas: By Group and Retain.

Untested, but hopefully gives you the idea.

If the coupon is greater than the yield then the return is 0, else it is the principal * yield.

data want;

set have;

by cusip;

retain flag;

if first.cusip then  flag=0;

if coupon > yield then flag=1;

if flag ne 1 then return=principal*coupon;

else return=0;

run;

Contributor
Posts: 20

## Re: SAS Macro Do Loop

Thank you for the reply. The flag worked perfectly. I was trying to program a loop for a long time and ust out of curiosity how would a loop work? I could not figure it out.

Thank you,

Todd

PROC Star
Posts: 8,163

## Re: SAS Macro Do Loop

In order to show you how you could do what you want with a loop, you first have to provide an example dataset WITH desired results.

Do you realize that the datastep, itself, functions via a loop?  You can introduce additional loops, when necessary, but that will get confusing unless we know what you are trying to use it for.

It would also help if you posted the code that you wrote in your own attempts to introduce a loop in the process.

Contributor
Posts: 20

## Re: SAS Macro Do Loop

Here is an example of the data set. The result is cash_interest. Not sure if my attempts will help. I kept getting weird results. I appreciate the comments.

 Cusip Date month Ending_Principal yield coupon Cash_Interest 05922KHH5 12/31/2012 0 45100000 0.053 0 \$0 05922KHH5 1/31/2013 1 45100000 0.053 0.00245 \$110,495 05922KHH5 2/28/2013 2 45100000 0.053 0.00245 \$110,495 05922KHH5 3/31/2013 3 45100000 0.053 0.00509 \$229,559 05922KHH5 4/30/2013 4 45100000 0.053 0.00463 \$208,813 05922KHH5 5/31/2013 5 45100000 0.053 0.00528 \$238,128 05922KHH5 6/30/2013 6 44000000 0.053 0.00594 \$0 05922KHH5 7/30/2013 7 44000000 0.053 0.06 \$0 07133AET5 12/31/2012 0 75000000 0.015 0 \$0 07133AET5 1/31/2013 1 75000000 0.015 0.0037 \$277,500 07133AET5 2/28/2013 2 75000000 0.015 0.00522 \$391,500 07133AET5 3/31/2013 3 75000000 0.015 0.00742 \$556,500 07133AET5 4/30/2013 4 75000000 0.015 0.00911 \$683,250 07133AET5 5/31/2013 5 75000000 0.015 0.01277 \$957,750 07133AET5 6/30/2013 6 75000000 0.015 0.01559 \$0 07133AET5 7/31/2013 7 75000000 0.015 0.01621 \$0 07133AET5 8/31/2013 8 75000000 0.015 0.014 \$0

When

Super User
Posts: 23,663

## Re: SAS Macro Do Loop

Hi Todd,

I really recommend you read this paper:

http://www2.sas.com/proceedings/sugi31/246-31.pdf

As Art has mentioned, the datastep already loops through each record so you don't need a loop.

Hope that helps.

Discussion stats
• 6 replies
• 335 views
• 0 likes
• 4 in conversation