<?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: Summing variable and then counting back down in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Summing-variable-and-then-counting-back-down/m-p/246996#M56274</link>
    <description>&lt;P&gt;I am not sure I understand the problem. Here is my interpretation:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;At a given date (t), all you know is&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;TotFem(t) &amp;lt;= Females(t) &amp;lt;= SumAdults(t) - TotMales(t)&lt;/LI&gt;
&lt;LI&gt;So, if EggsLaid(t) is the number of eggs laid between t-1 and t, Fecundity(t) (EggsLaid(t)/ Females(t-1)) is bounded between EggsLaid(t) / (SumAdults(t-1) - TotMales(t-1)) and EggsLaid(t) / TotFem(t).&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is that right?&lt;/P&gt;</description>
    <pubDate>Fri, 29 Jan 2016 22:01:43 GMT</pubDate>
    <dc:creator>PGStats</dc:creator>
    <dc:date>2016-01-29T22:01:43Z</dc:date>
    <item>
      <title>Summing variable and then counting back down</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Summing-variable-and-then-counting-back-down/m-p/246950#M56271</link>
      <description>&lt;P&gt;Hii,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have some insect data where I have counts insects (Sum Adults), but I don't know if they're males or females until they die and are dissected (DeadFem and DeadMale). &amp;nbsp;When they are all dead, then I summed up how many there total (TotFem). &amp;nbsp;I want to know how many females there are on a given date to do a fecundity calculations (#females/#eggs laid).&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Date &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;SumAdults &amp;nbsp; DeadFem DeadMale TotFem&lt;/P&gt;&lt;P&gt;18 Apr &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 17 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&lt;/P&gt;&lt;P&gt;22 Apr &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 14 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1 &amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;25 Apr &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;12 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 3&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;and so on.&amp;nbsp;The last entry in TotFem tells me how many total females I had, so then I want a variable that starts at that value and subtracts the value of DeadFem for each date.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 29 Jan 2016 18:31:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Summing-variable-and-then-counting-back-down/m-p/246950#M56271</guid>
      <dc:creator>kebowers</dc:creator>
      <dc:date>2016-01-29T18:31:45Z</dc:date>
    </item>
    <item>
      <title>Re: Summing variable and then counting back down</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Summing-variable-and-then-counting-back-down/m-p/246958#M56272</link>
      <description>&lt;P&gt;This may give you something to work with. I strongly recommend having an actual SAS date value including a year.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input day month $        SumAdults   DeadFem DeadMale TotFem;
datalines;
18 Apr               17                     0            0              0
22 Apr               14                     1             2             1   
25 Apr                12                    2             0             3
;
run;
/* it would be preferable to have an actual complete SAS date value to sort by*/
proc sort data=have; by descending day;run;

data want;
   set have;
   retain alive 0;
   output;
   alive = alive + deadfem;
run;

proc sort data=want; by day;run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 29 Jan 2016 19:05:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Summing-variable-and-then-counting-back-down/m-p/246958#M56272</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-01-29T19:05:30Z</dc:date>
    </item>
    <item>
      <title>Re: Summing variable and then counting back down</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Summing-variable-and-then-counting-back-down/m-p/246963#M56273</link>
      <description>&lt;P&gt;It's best to give an example of your desired output.&amp;nbsp; I think all you need is a running total of the dead male, then you can do any calculation you want.&amp;nbsp; Let me know if this helps:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data have;&lt;BR /&gt;input Date$&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SumAdults&amp;nbsp;&amp;nbsp; DeadFem DeadMale TotFem;&lt;BR /&gt;cards;&lt;BR /&gt;18Apr&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 17&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0&lt;BR /&gt;22Apr&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 14&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 2&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&lt;BR /&gt;25Apr&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 12&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 2&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 3&lt;BR /&gt;;run;&lt;BR /&gt;&lt;BR /&gt;data want;&lt;BR /&gt;set have;&lt;BR /&gt;TotMal+deadmale;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Fri, 29 Jan 2016 19:34:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Summing-variable-and-then-counting-back-down/m-p/246963#M56273</guid>
      <dc:creator>Steelers_In_DC</dc:creator>
      <dc:date>2016-01-29T19:34:51Z</dc:date>
    </item>
    <item>
      <title>Re: Summing variable and then counting back down</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Summing-variable-and-then-counting-back-down/m-p/246996#M56274</link>
      <description>&lt;P&gt;I am not sure I understand the problem. Here is my interpretation:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;At a given date (t), all you know is&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;TotFem(t) &amp;lt;= Females(t) &amp;lt;= SumAdults(t) - TotMales(t)&lt;/LI&gt;
&lt;LI&gt;So, if EggsLaid(t) is the number of eggs laid between t-1 and t, Fecundity(t) (EggsLaid(t)/ Females(t-1)) is bounded between EggsLaid(t) / (SumAdults(t-1) - TotMales(t-1)) and EggsLaid(t) / TotFem(t).&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is that right?&lt;/P&gt;</description>
      <pubDate>Fri, 29 Jan 2016 22:01:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Summing-variable-and-then-counting-back-down/m-p/246996#M56274</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2016-01-29T22:01:43Z</dc:date>
    </item>
    <item>
      <title>Re: Summing variable and then counting back down</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Summing-variable-and-then-counting-back-down/m-p/250349#M56584</link>
      <description>&lt;P&gt;I am not sure that solves the problem. &amp;nbsp;Maybe my real question is how to I inialize a new variable with something other than 0. I want it to be the final value of the totfem variable.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;I had created all these counting variables to keep track of how many male and female insects died each day as well as the total number of insects. What I need is the &lt;STRONG&gt;last value&lt;/STRONG&gt; of totfem to be the &lt;STRONG&gt;initial value&lt;/STRONG&gt; of a new variable (livefem). &amp;nbsp;From there I think I can write a do loop to subtract the daily value of sumfem.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data sasuser.sumhome;
set sasuser.sorthome;
retain  sumadult sumfem summale sumegg cnt days sumlost;
by Date2;

days = Date2-Date1;

if first.Date2 then
do;
sumlost =0;
sumadult = 0;
sumfem = 0;
summale = 0;
sumegg = 0;


cnt = 0;
end;

sumlost= sumlost+lost;
sumadult = sumadult+alive;
sumfem = sumfem+deadfem;
summale = summale+deadmale;
sumegg= sumegg+eggs;


cnt= cnt+1;

if last.Date2 then output;

run;&lt;BR /&gt;data sasuser.hometot;&lt;BR /&gt;set sasuser.sumhome;&lt;BR /&gt;retain sumadult sumfem summale sumegg cnt days totfem ;&lt;BR /&gt;by Date2;&lt;BR /&gt;drop Cage Lost Alive Eggs;&lt;BR /&gt;&lt;BR /&gt;totfem+sumfem;&lt;BR /&gt;run;&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 16 Feb 2016 15:21:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Summing-variable-and-then-counting-back-down/m-p/250349#M56584</guid>
      <dc:creator>kebowers</dc:creator>
      <dc:date>2016-02-16T15:21:07Z</dc:date>
    </item>
    <item>
      <title>Re: Summing variable and then counting back down</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Summing-variable-and-then-counting-back-down/m-p/250643#M56607</link>
      <description>&lt;P&gt;does this get you close?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data howmany ;
   infile datalines dsd delimiter=','; 
input day DATE10. sumAdults deadFem deadMale totFem ;
format day date10. ;
datalines;
18 Apr 16,17,0,0,0
22 Apr 16,14,1,2,1
25 Apr 16,12,2,0,3
;
run;

title1 'raw input'; 
proc print data = howmany ;
run;

title1 'rolling total of dead female bugs' ;
proc sql ;
create table rolling as
select d.day,SUM(r.totfem) as rollingtotfem
from
howmany d
join
howmany r
on d.day &amp;gt;= r.day
group by d.day
;
quit;
run;

title1 'apply rolling total back to raw data' ;
proc sql ;
select d.day,d.sumAdults ,d.deadFem ,d.deadMale ,d.totFem , r.rollingtotfem
from howmany d
join rolling r
on d.day = r.day;
quit ;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 17 Feb 2016 15:13:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Summing-variable-and-then-counting-back-down/m-p/250643#M56607</guid>
      <dc:creator>johnsville</dc:creator>
      <dc:date>2016-02-17T15:13:17Z</dc:date>
    </item>
  </channel>
</rss>

