data GN_Drugbug_Modified;
set GN_Drugbug_Filtered;
by hospital drug bug;
retain temp_ns_n 0 temp_it_n 0 new_ps_n;
/* Initialize retained variables for each hospital-drug group */
if first.drug then do;
temp_ns_n = 0;
temp_it_n = 0;
new_ps_n = .;
end;
/* Summing for Escherichia coli and Escherichia coli ESBL for specific hospitals */
if hospital in ("Hospital1", "Hospital2", "Hospital3") and
bug in ("Escherichia coli", "Escherichia coli ESBL") then do;
temp_ns_n + ns_n;
temp_it_n + it_n;
end;
/* Calculate new ps_n for Escherichia coli */
if last.drug and bug = "Escherichia coli" then new_ps_n = temp_ns_n / temp_it_n;
/* Apply new ps_n and recalculate ns_n for Escherichia coli */
if bug = "Escherichia coli" then do;
ps_n = new_ps_n;
ns_n = ps_n * temp_it_n;
end;
/* Only keep rows that are not Escherichia coli ESBL */
if bug ne "Escherichia coli ESBL" then output;
run;
SAS is an extremely powerful software for doing statistical analysis. You do not have to (and should not) try to program sums by group yourself because SAS has already programmed this for you. This is done in PROC MEANS and PROC SUMMARY.
Please state clearly the statistics and other information you would like from this data.
SAS is an extremely powerful software for doing statistical analysis. You do not have to (and should not) try to program sums by group yourself because SAS has already programmed this for you. This is done in PROC MEANS and PROC SUMMARY.
Please state clearly the statistics and other information you would like from this data.
Thank you Paige for your hint. I solved the issue using proc summary.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.