DATA Step, Macro, Functions and more

SAS Macro Do Loop

Reply
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: 8,744

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: 17,959

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: 7,366

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.

CusipDatemonthEnding_PrincipalyieldcouponCash_Interest
05922KHH512/31/20120451000000.0530$0
05922KHH51/31/20131451000000.0530.00245$110,495
05922KHH52/28/20132451000000.0530.00245$110,495
05922KHH53/31/20133451000000.0530.00509$229,559
05922KHH54/30/20134451000000.0530.00463$208,813
05922KHH55/31/20135451000000.0530.00528$238,128
05922KHH56/30/20136440000000.0530.00594$0
05922KHH57/30/20137440000000.0530.06$0
07133AET512/31/20120750000000.0150$0
07133AET51/31/20131750000000.0150.0037$277,500
07133AET52/28/20132750000000.0150.00522$391,500
07133AET53/31/20133750000000.0150.00742$556,500
07133AET54/30/20134750000000.0150.00911$683,250
07133AET55/31/20135750000000.0150.01277$957,750
07133AET56/30/20136750000000.0150.01559$0
07133AET57/31/20137750000000.0150.01621$0
07133AET58/31/20138750000000.0150.014$0

When

Super User
Posts: 17,959

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.

Ask a Question
Discussion stats
  • 6 replies
  • 299 views
  • 0 likes
  • 4 in conversation