<?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 Re: Value calculations in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Value-calculations/m-p/886031#M350135</link>
    <description>&lt;P&gt;Your sample data leaves a lot to presume.&amp;nbsp; I presume that&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Each hospital either has just one value for bug, or two values for bug, (one of the form xxxES and the other of the form xxx).&lt;/LI&gt;
&lt;LI&gt;Each hospital has a constant value for DRUG.&lt;/LI&gt;
&lt;LI&gt;You want one record per hospital with
&lt;OL&gt;
&lt;LI&gt;aggregate counts (it_n and ns_n)&lt;/LI&gt;
&lt;LI&gt;recalculated ps_n&lt;/LI&gt;
&lt;LI&gt;a bug value without the trailing "ES".&lt;/LI&gt;
&lt;/OL&gt;
&lt;/LI&gt;
&lt;LI&gt;The data are already grouped by hospital.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Exposure;
  input Hospital $ Bug $ Drug $ it_n ps_n ns_n;
datalines;
ABC Ecoli   MC  22 0.5 11
ABC EcoliES MC  13 0.7  9.1
XYZ Kleb    MN 100 0.6 60
XYZ KlebES  MN  33 1   33
RTY Ecoli   FL  50 0.4 20
RTY EcoliES FL  20 0.1  2
run; 

data want (drop=sum_:);
  set exposure;
  by hospital notsorted;
  if first.hospital then call missing(sum_itn,sum_nsn);
  sum_itn+it_n;
  sum_nsn+ns_n;
  if last.hospital;
  it_n=sum_itn;
  ns_n=sum_nsn;
  ps_n=ns_n/it_n;
  if find(cats(bug,'!'),'ES!') then bug=substr(bug,1,length(bug)-2);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 24 Jul 2023 03:28:25 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2023-07-24T03:28:25Z</dc:date>
    <item>
      <title>Value calculations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Value-calculations/m-p/886024#M350129</link>
      <description>&lt;PRE&gt;data Exposure;&lt;BR /&gt;input Hospital $ Bug $ Drug $ it_n ps_n ns_n;&lt;BR /&gt;datalines;&lt;BR /&gt;ABC Ecoli MC 22 0.5 11&lt;BR /&gt;ABC EcoliES MC 13 0.7 9.1&lt;BR /&gt;XYZ Kleb MN 100 0.6 60&lt;BR /&gt;XYZ KlebES MN 33 1 33&lt;BR /&gt;RTY Ecoli FL 50 0.4 20&lt;BR /&gt;RTY EcoliES FL 20 0.1 2&lt;BR /&gt;;;;&lt;BR /&gt;run; &lt;CODE class=""&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I have the above data sample. The data contains the following variables Hospital, drug, bug, it_n (number of tests, ps_n (%susceptibility) ns_n (number of susceptible tests).&lt;/P&gt;&lt;P&gt;Some hospitals as in the dataset have values for 2 bugs instead of one,&amp;nbsp; we need to keep values for Ecoli and Kleb and remove EcoliES and KlebES based on this formula:&amp;nbsp; Total number of tests susceptible to Drug/ Total number of tests, for example, we'll end up with Ecoli only and calculate the values as (11 + 9.1 / 22+ 13) = 60%, so 0.6 for ps_n 20.1&amp;nbsp; for ns_n and 35 for it_n.&lt;/P&gt;&lt;P&gt;Is there a way to do this in SAS?&lt;/P&gt;&lt;P&gt;I appreciate your help.&lt;/P&gt;&lt;P&gt;Thank you,&lt;/P&gt;&lt;P&gt;Razina&lt;/P&gt;</description>
      <pubDate>Sun, 23 Jul 2023 23:54:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Value-calculations/m-p/886024#M350129</guid>
      <dc:creator>mayasak</dc:creator>
      <dc:date>2023-07-23T23:54:38Z</dc:date>
    </item>
    <item>
      <title>Re: Value calculations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Value-calculations/m-p/886029#M350133</link>
      <description>&lt;P&gt;You may want a DOW-Loop way:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  do until(last.drug);
    set exposure;
    by hospital drug notsorted;
    sum_it_n=sum(sum_it_n,it_n);
    sum_ns_n=sum(sum_ns_n,ns_n);
  end;

  do until(first.drug);
    set exposure;
    by hospital drug notsorted;
    it_n=sum_it_n;
    ns_n=sum_ns_n;
    ps_n=ns_n/it_n;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Dow-Loop:&amp;nbsp;&lt;A href="https://support.sas.com/resources/papers/proceedings12/052-2012.pdf" target="_blank"&gt;052-2012: The DOW Loop: A Smarter Approach to Your Existing Code (sas.com)&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 24 Jul 2023 02:10:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Value-calculations/m-p/886029#M350133</guid>
      <dc:creator>whymath</dc:creator>
      <dc:date>2023-07-24T02:10:24Z</dc:date>
    </item>
    <item>
      <title>Re: Value calculations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Value-calculations/m-p/886031#M350135</link>
      <description>&lt;P&gt;Your sample data leaves a lot to presume.&amp;nbsp; I presume that&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Each hospital either has just one value for bug, or two values for bug, (one of the form xxxES and the other of the form xxx).&lt;/LI&gt;
&lt;LI&gt;Each hospital has a constant value for DRUG.&lt;/LI&gt;
&lt;LI&gt;You want one record per hospital with
&lt;OL&gt;
&lt;LI&gt;aggregate counts (it_n and ns_n)&lt;/LI&gt;
&lt;LI&gt;recalculated ps_n&lt;/LI&gt;
&lt;LI&gt;a bug value without the trailing "ES".&lt;/LI&gt;
&lt;/OL&gt;
&lt;/LI&gt;
&lt;LI&gt;The data are already grouped by hospital.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Exposure;
  input Hospital $ Bug $ Drug $ it_n ps_n ns_n;
datalines;
ABC Ecoli   MC  22 0.5 11
ABC EcoliES MC  13 0.7  9.1
XYZ Kleb    MN 100 0.6 60
XYZ KlebES  MN  33 1   33
RTY Ecoli   FL  50 0.4 20
RTY EcoliES FL  20 0.1  2
run; 

data want (drop=sum_:);
  set exposure;
  by hospital notsorted;
  if first.hospital then call missing(sum_itn,sum_nsn);
  sum_itn+it_n;
  sum_nsn+ns_n;
  if last.hospital;
  it_n=sum_itn;
  ns_n=sum_nsn;
  ps_n=ns_n/it_n;
  if find(cats(bug,'!'),'ES!') then bug=substr(bug,1,length(bug)-2);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 24 Jul 2023 03:28:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Value-calculations/m-p/886031#M350135</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2023-07-24T03:28:25Z</dc:date>
    </item>
  </channel>
</rss>

