<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Code for data aggregation and sum in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Code-for-data-aggregation-and-sum/m-p/916890#M361157</link>
    <description>&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;I have the following conditions and the code I created. for a data set with hospital, drug, bug, it_n, ns_n, and ps_n variables. But for #2 it's not populating the correct calculation required for ps_n value for Escherichia coli. Here's the requirements:&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV class=""&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;For only 3 hospitals Hospital1, Hospital2, and Hospital3&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV class=""&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;If "bug" = Escherichia coli and "bug" = Escherichia coli ESBL &lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV class=""&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;1. Aggregate ns_n for Escherichia coli + ns_n for Escherichia coli ESBL, same thing for it_n&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV class=""&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;2.&amp;nbsp;&lt;FONT color="#000000"&gt;Calculate the new ps_n for "Escherichia coli" as the sum of ns_n&amp;nbsp;divided by the sum of it_n&lt;/FONT&gt;.&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV class=""&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;3. Delete the row with Escherichia coli ESBL.&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV class=""&gt;&lt;FONT face="arial,helvetica,sans-serif" color="#000000"&gt;4. Recalculate&amp;nbsp;ns_n for the "Escherichia coli" row based on the new ps_n and the sum of it_n.&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV class=""&gt;&lt;FONT face="arial,helvetica,sans-serif" color="#000000"&gt;&lt;FONT face="arial,helvetica,sans-serif" color="#000000"&gt;Is there anything to change the code to get the correct values&lt;/FONT&gt;&lt;/FONT&gt;&lt;PRE&gt;&lt;CODE class=""&gt;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;

&lt;/CODE&gt;&lt;/PRE&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;</description>
    <pubDate>Tue, 20 Feb 2024 00:57:03 GMT</pubDate>
    <dc:creator>mayasak</dc:creator>
    <dc:date>2024-02-20T00:57:03Z</dc:date>
    <item>
      <title>Code for data aggregation and sum</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Code-for-data-aggregation-and-sum/m-p/916890#M361157</link>
      <description>&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;I have the following conditions and the code I created. for a data set with hospital, drug, bug, it_n, ns_n, and ps_n variables. But for #2 it's not populating the correct calculation required for ps_n value for Escherichia coli. Here's the requirements:&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV class=""&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;For only 3 hospitals Hospital1, Hospital2, and Hospital3&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV class=""&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;If "bug" = Escherichia coli and "bug" = Escherichia coli ESBL &lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV class=""&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;1. Aggregate ns_n for Escherichia coli + ns_n for Escherichia coli ESBL, same thing for it_n&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV class=""&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;2.&amp;nbsp;&lt;FONT color="#000000"&gt;Calculate the new ps_n for "Escherichia coli" as the sum of ns_n&amp;nbsp;divided by the sum of it_n&lt;/FONT&gt;.&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV class=""&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;3. Delete the row with Escherichia coli ESBL.&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV class=""&gt;&lt;FONT face="arial,helvetica,sans-serif" color="#000000"&gt;4. Recalculate&amp;nbsp;ns_n for the "Escherichia coli" row based on the new ps_n and the sum of it_n.&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV class=""&gt;&lt;FONT face="arial,helvetica,sans-serif" color="#000000"&gt;&lt;FONT face="arial,helvetica,sans-serif" color="#000000"&gt;Is there anything to change the code to get the correct values&lt;/FONT&gt;&lt;/FONT&gt;&lt;PRE&gt;&lt;CODE class=""&gt;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;

&lt;/CODE&gt;&lt;/PRE&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;</description>
      <pubDate>Tue, 20 Feb 2024 00:57:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Code-for-data-aggregation-and-sum/m-p/916890#M361157</guid>
      <dc:creator>mayasak</dc:creator>
      <dc:date>2024-02-20T00:57:03Z</dc:date>
    </item>
    <item>
      <title>Re: Code for data aggregation and sum</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Code-for-data-aggregation-and-sum/m-p/916891#M361158</link>
      <description>&lt;P&gt;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.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please state clearly the statistics and other information you would like from this data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 20 Feb 2024 01:04:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Code-for-data-aggregation-and-sum/m-p/916891#M361158</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-02-20T01:04:06Z</dc:date>
    </item>
    <item>
      <title>Re: Code for data aggregation and sum</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Code-for-data-aggregation-and-sum/m-p/916892#M361159</link>
      <description>&lt;P&gt;Thank you Paige for your hint. I solved the issue using proc summary.&lt;/P&gt;</description>
      <pubDate>Tue, 20 Feb 2024 01:23:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Code-for-data-aggregation-and-sum/m-p/916892#M361159</guid>
      <dc:creator>mayasak</dc:creator>
      <dc:date>2024-02-20T01:23:55Z</dc:date>
    </item>
  </channel>
</rss>

