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!!
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/
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/
Large economy sized: Same operation , multiple variables on a single observation usually points to an Array or multiple array, based solution.
It's your turn to help shape SAS Innovate 2027. Share your expertise and inspire the SAS community.
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.
Ready to level-up your skills? Choose your own adventure.