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
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
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;
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
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.
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
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.
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.