BookmarkSubscribeRSS Feed
toddtodd
Calcite | Level 5

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

6 REPLIES 6
Cynthia_sas
SAS Super FREQ

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

Reeza
Super User

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;

toddtodd
Calcite | Level 5

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

art297
Opal | Level 21

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.

toddtodd
Calcite | Level 5

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

Reeza
Super User

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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 6 replies
  • 992 views
  • 0 likes
  • 4 in conversation