DATA Step, Macro, Functions and more

increase values until a limit in SAS

Accepted Solution Solved
Reply
Contributor
Posts: 65
Accepted Solution

increase values until a limit in SAS

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;

Accepted Solutions
Solution
‎03-19-2018 10:26 AM
Super User
Posts: 13,941

Re: increase values until a limit in SAS

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


All Replies
Solution
‎03-19-2018 10:26 AM
Super User
Posts: 13,941

Re: increase values until a limit in SAS

No loop or accumulator needed:

 

year = Date_of_Last_Repricing + ( floor((2017-Date_of_Last_Repricing)/Repricing_Frequency) * Repricing_Frequency );
Contributor
Posts: 65

Re: increase values until a limit in SAS

nice ,ty.

Super User
Posts: 6,934

Re: increase values until a limit in SAS

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.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 104 views
  • 2 likes
  • 3 in conversation