BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
epialy
Calcite | Level 5

I'm trying to write a program do so some simple calculations (projections across years). I could do the calculations manually, but that's time consuming and clunky:

 

data proj; 
	set combined;
	deaths2015=(pop1yr*mort_rate)/100000; 
	atrisk2015=pop1yr-deaths2015; 
	expected2015=(atrisk2015*rate_CR)/100000;

	deaths2016=((atrisk2015-expected2015)*mort_rate)/100000;
	atrisk2016=(atrisk2015-expected2015-deaths2016); 
	expected2016=(atrisk2016*rate_CR)/100000; 

	deaths2017=((atrisk2016-expected2016)*mort_rate)/100000; 
	atrisk2017=(atrisk2016-expected2016-deaths2016);
	expected2017=(atrisk2017*rate_CR)/1000000;
run; 

They need to go out until 2038. For 2016 and onward the calculations reference the previously calculated values. I imagine I could write a macro variables atrisk&i and expected&i to be able to increase the these by year, but I can't quite wrap my head about how to structure it so that it references the previously calculated value.

 

Thank you!!

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
data proj; 
	set combined;
	deaths2015=(pop1yr*mort_rate)/100000; 
	atrisk2015=pop1yr-deaths2015; 
	expected2015=(atrisk2015*rate_CR)/100000;

       array _deaths(2015:2038) deaths2015-deaths2038;
       array _atrisk(2015:2038) atrisk2015-atrisk2038;
       array _expected(2015:2038) expected2015-expected2038;

     do i=2016 to 2038;
    	_deaths(i)=((_atrisk(i-1)-_expected(i-1)*mort_rate)/100000;
	_atrisk(i)=(_atrisk(i-1)-_expected(i-1)-_deaths(i)); 
	_expected(i)=(_atrisk(i)*rate_CR)/100000; 
    end;

run;

Arrays, not macros.

 

Here's a tutorial on using Arrays in SAS
https://stats.idre.ucla.edu/sas/seminars/sas-arrays/

View solution in original post

2 REPLIES 2
Reeza
Super User
data proj; 
	set combined;
	deaths2015=(pop1yr*mort_rate)/100000; 
	atrisk2015=pop1yr-deaths2015; 
	expected2015=(atrisk2015*rate_CR)/100000;

       array _deaths(2015:2038) deaths2015-deaths2038;
       array _atrisk(2015:2038) atrisk2015-atrisk2038;
       array _expected(2015:2038) expected2015-expected2038;

     do i=2016 to 2038;
    	_deaths(i)=((_atrisk(i-1)-_expected(i-1)*mort_rate)/100000;
	_atrisk(i)=(_atrisk(i-1)-_expected(i-1)-_deaths(i)); 
	_expected(i)=(_atrisk(i)*rate_CR)/100000; 
    end;

run;

Arrays, not macros.

 

Here's a tutorial on using Arrays in SAS
https://stats.idre.ucla.edu/sas/seminars/sas-arrays/

ballardw
Super User

Large economy sized: Same operation , multiple variables on a single observation usually points to an Array or multiple array, based solution.

CFC_SAS_Communities_400x225.jpg

Call for content now open!

It's your turn to help shape SAS Innovate 2027. Share your expertise and inspire the SAS community.

Submit your proposal →

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 1803 views
  • 1 like
  • 3 in conversation