BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
89974114
Quartz | Level 8

I have a variable in SAS which represents the last repricing of an asset, a subset looks like:

 

Data Subset;
input ID Date_of_Last_Repricing Repricing_Frequency;
datalines; 1 2011 2 2 2013 2 3 2016 3 4 2017 1 5 2015 1 6 2015 1 7 2017 2 8 2017 2 9 2011 1 10 2010 1 11 2010 1 12 2011 2 13 2012 3
;
run;

I'm looking to create a loop that increases the variable date_of_last_repricing by the repricing frequency until it gets as close to 2017 as possible. For example the data set would look like:

 

Data want;
input ID Date_of_Last_Repricing Repricing_Frequency Year;
datalines;

1 2011 2 2017
2 2013 2 2017
3 2016 3 2016
4 2017 1 2017
5 2015 1 2017
6 2015 1 2017
7 2017 2 2017
8 2017 2 2017
9 2011 1 2017
10 2010 1 2017
11 2010 3 2016
12 2011 2 2017
13 2012 3 2015
;
run;

 

So far the code I've tried to use is 


data testing;
set test;

if dateoflastrepricing+repricing_schedule<2017
then do until sum(dateoflastrepricing+repricing_schedule)>2017;

sum(dateoflastrepricing+repricingschedule);
end;
run;
1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

No loop or accumulator needed:

 

year = Date_of_Last_Repricing + ( floor((2017-Date_of_Last_Repricing)/Repricing_Frequency) * Repricing_Frequency );

View solution in original post

3 REPLIES 3
ballardw
Super User

No loop or accumulator needed:

 

year = Date_of_Last_Repricing + ( floor((2017-Date_of_Last_Repricing)/Repricing_Frequency) * Repricing_Frequency );
89974114
Quartz | Level 8

nice ,ty.

Astounding
PROC Star

Based on the final observation in your data set, it looks like YEAR should never exceed 2017.  For that result:

 

data want;

set have;

year = date_of_last_repricing;

do k=1 to 100 while (year + repricing_frequency <= 2017);

   year = year + repricing_frequency;

end;

drop k;

run;

 

The purpose of the 1 to 100 loop is to prevent an infinite loop for bad data, such as one of the key variables having a missing value.

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
  • 3 replies
  • 801 views
  • 2 likes
  • 3 in conversation