DATA Step, Macro, Functions and more

SAS Macro Do Loop

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

Posts: 9,365

Re: SAS Macro Do Loop


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)?


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;


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,


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.

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.



Super User
Posts: 23,663

Re: SAS Macro Do Loop

Hi Todd,

I really recommend you read this paper:

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

Hope that helps.

Ask a Question
Discussion stats
  • 6 replies
  • 4 in conversation