<?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 How to calculate running total for certain rows in SAS in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-running-total-for-certain-rows-in-SAS/m-p/896003#M354002</link>
    <description>&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="YeWMM.png" style="width: 940px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/88366i67FD437D2C3B4ED7/image-size/large?v=v2&amp;amp;px=999" role="button" title="YeWMM.png" alt="YeWMM.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;A href="https://drive.google.com/file/d/1rxqMvUtVRuWWG7FjXgJUyXx62vEz35db/view?usp=drive_link" target="_blank" rel="nofollow noopener noreferrer"&gt;A small sample of the data can be downloaded from my Google Drive.&lt;/A&gt;&lt;/P&gt;&lt;P&gt;You can use the data to debug my code.&lt;/P&gt;&lt;P&gt;This is how my data looks like. At each annual report date, there is a count of male directors (male_dirs) and a count of female directors (male_dirs).&lt;/P&gt;&lt;P&gt;During a year, members may leave or join the board, denoted by male_dirs_ann and female_dirs_ann ("_ann" is for "announcement" because they are merged from the "announcement" table), where 1 indicates one joined, -1 one left.&lt;/P&gt;&lt;P&gt;I wanted to update the numbers of male and female directors (male_dirs and female_dirs) when any left or joined the board, by generating two new variables, "total_male" and "total_female". On the annual report dates, I just copied over the male_dirs and female_dirs because there is no change on these date. For dates with changes, I took the number from the last date (male_dirs/female_dirs) and added/deducted the current change (male_dirs_ann/female_dirs_ann).&lt;/P&gt;&lt;P&gt;Row 2: total_male = 7 + 1 = 8, which is correct.&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Row 6 is wrong: A male director left, and total_male should have been 6 (i.e., 7-1).&lt;/STRONG&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;Rows 9 and 17 are also wrong, among others.&lt;/P&gt;&lt;P&gt;My code is as follows:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data board_size_change;
set board_size_change;
by companyID date;
retain total_male total_female;

if first.companyID = 1 then do; 
   total_male = 0;
   total_female = 0;
end;

*At annual report date;
if not missing(annualreportdate) then do;
    total_male=male_dirs;
    total_female=female_dirs;
    output;
end;

*At announcement;
if missing(annualreportdate) then do;
    total_male + male_dirs_ann;
    total_female + female_dirs_ann;
    output;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;SPAN&gt;What I missed or is there another way to approach this? Thanks a lot!&lt;/SPAN&gt;&lt;/P&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;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 27 Sep 2023 07:58:58 GMT</pubDate>
    <dc:creator>beihorse</dc:creator>
    <dc:date>2023-09-27T07:58:58Z</dc:date>
    <item>
      <title>How to calculate running total for certain rows in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-running-total-for-certain-rows-in-SAS/m-p/896003#M354002</link>
      <description>&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="YeWMM.png" style="width: 940px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/88366i67FD437D2C3B4ED7/image-size/large?v=v2&amp;amp;px=999" role="button" title="YeWMM.png" alt="YeWMM.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;A href="https://drive.google.com/file/d/1rxqMvUtVRuWWG7FjXgJUyXx62vEz35db/view?usp=drive_link" target="_blank" rel="nofollow noopener noreferrer"&gt;A small sample of the data can be downloaded from my Google Drive.&lt;/A&gt;&lt;/P&gt;&lt;P&gt;You can use the data to debug my code.&lt;/P&gt;&lt;P&gt;This is how my data looks like. At each annual report date, there is a count of male directors (male_dirs) and a count of female directors (male_dirs).&lt;/P&gt;&lt;P&gt;During a year, members may leave or join the board, denoted by male_dirs_ann and female_dirs_ann ("_ann" is for "announcement" because they are merged from the "announcement" table), where 1 indicates one joined, -1 one left.&lt;/P&gt;&lt;P&gt;I wanted to update the numbers of male and female directors (male_dirs and female_dirs) when any left or joined the board, by generating two new variables, "total_male" and "total_female". On the annual report dates, I just copied over the male_dirs and female_dirs because there is no change on these date. For dates with changes, I took the number from the last date (male_dirs/female_dirs) and added/deducted the current change (male_dirs_ann/female_dirs_ann).&lt;/P&gt;&lt;P&gt;Row 2: total_male = 7 + 1 = 8, which is correct.&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Row 6 is wrong: A male director left, and total_male should have been 6 (i.e., 7-1).&lt;/STRONG&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;Rows 9 and 17 are also wrong, among others.&lt;/P&gt;&lt;P&gt;My code is as follows:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data board_size_change;
set board_size_change;
by companyID date;
retain total_male total_female;

if first.companyID = 1 then do; 
   total_male = 0;
   total_female = 0;
end;

*At annual report date;
if not missing(annualreportdate) then do;
    total_male=male_dirs;
    total_female=female_dirs;
    output;
end;

*At announcement;
if missing(annualreportdate) then do;
    total_male + male_dirs_ann;
    total_female + female_dirs_ann;
    output;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;SPAN&gt;What I missed or is there another way to approach this? Thanks a lot!&lt;/SPAN&gt;&lt;/P&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;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 27 Sep 2023 07:58:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-running-total-for-certain-rows-in-SAS/m-p/896003#M354002</guid>
      <dc:creator>beihorse</dc:creator>
      <dc:date>2023-09-27T07:58:58Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate running total for certain rows in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-running-total-for-certain-rows-in-SAS/m-p/896044#M354031</link>
      <description>&lt;P&gt;&lt;SPAN&gt;Avoid keeping subtotals in the same table as unsummed data. Calculate running totals in a view or another table, and merge the two tables when reporting them together.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 27 Sep 2023 13:31:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-running-total-for-certain-rows-in-SAS/m-p/896044#M354031</guid>
      <dc:creator>rudfaden</dc:creator>
      <dc:date>2023-09-27T13:31:48Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate running total for certain rows in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-running-total-for-certain-rows-in-SAS/m-p/896050#M354034</link>
      <description>&lt;P&gt;Your data step begins with:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data board_size_change;
set board_size_change;
by companyID date;
retain total_male total_female;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Since you are copying a dataset (board_size_change) to itself, it's very likely that variables total_male and total_female are NOT new variables.&amp;nbsp; &amp;nbsp;On the one hand this makes the retain statement superfluous (vars in a SET statement are automatically retained until replaced by a new obs).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But more importantly it means that with each new obs, total_male and total_female values are replaced by the incoming obs values. Consequently the statements&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if missing(annualreportdate) then do;
    total_male + male_dirs_ann;
    total_female + female_dirs_ann;
    output;
end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;need not be starting with total_male and total_female values inherited from the prior obs.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Consider changing your SET statement to&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;set board_size_change (drop=total_male total_female);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and keep the RETAIN statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, I did NOT download your data for testing purposes, because it obligates me to create/register/use a google account.&amp;nbsp; I try to avoid providing more data to google (they know enough about me already) for someone else's convenience.&lt;/P&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;
&lt;P&gt;&amp;nbsp;&lt;/P&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>Wed, 27 Sep 2023 14:29:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-running-total-for-certain-rows-in-SAS/m-p/896050#M354034</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2023-09-27T14:29:53Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate running total for certain rows in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-running-total-for-certain-rows-in-SAS/m-p/896097#M354046</link>
      <description>&lt;P&gt;Instructions here: &lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712" target="_blank"&gt;https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712&lt;/A&gt; will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the &amp;lt;/&amp;gt; icon or attached as text to show exactly what you have and that we can test code against.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You don't need to include all of the data, enough to work through the cases you need. You could use the OBS= data set option to copy just 20 or so observations or a more complicated code to subset your existing data before using the macro.&lt;/P&gt;</description>
      <pubDate>Wed, 27 Sep 2023 16:51:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-running-total-for-certain-rows-in-SAS/m-p/896097#M354046</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-09-27T16:51:49Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate running total for certain rows in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-running-total-for-certain-rows-in-SAS/m-p/896137#M354066</link>
      <description>&lt;P&gt;Thank you.&amp;nbsp; &amp;nbsp;I tried to convert the dataset to data set code.&amp;nbsp; The code is as follows:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data work.board_size_change;
  infile datalines dsd truncover;
  input CompanyID:32. date:YYMMDDN8. AnnualReportDate:YYMMDDN8. male_dirs:32. female_dirs:32. male_dirs_ann:32. female_dirs_ann:32.;
  format date YYMMDDN8. AnnualReportDate YYMMDDN8.;
datalines;
3 20051201 20051201 7 0 0 0
3 20061027 . . . 1 0
3 20061201 20061201 8 0 0 0
6 20050701 20050701 8 1 0 0
6 20060701 20060701 7 1 0 0
6 20061207 . . . -1 0
6 20061207 . . . 1 0
6 20070207 . . . 0 -1
6 20070208 . . . 0 1
6 20070316 . . . -1 0
6 20070627 . . . 1 0
6 20070701 20070701 7 1 0 0
6 20080601 20080601 7 1 0 0
6 20090319 . . . 1 0
6 20090601 20090601 8 1 0 0
6 20100601 20100601 8 1 0 0
6 20101202 . . . 0 -1
6 20110701 20110701 8 0 0 0
6 20120112 . . . -1 0
6 20120112 . . . 0 1
;;;;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;However, there was an error when I ran this code.&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;ERROR 48-59: The informat YYMMDDN was not found or could not be loaded.&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 27 Sep 2023 19:04:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-running-total-for-certain-rows-in-SAS/m-p/896137#M354066</guid>
      <dc:creator>beihorse</dc:creator>
      <dc:date>2023-09-27T19:04:00Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate running total for certain rows in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-running-total-for-certain-rows-in-SAS/m-p/896140#M354068</link>
      <description>&lt;P&gt;Thank you!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I added&amp;nbsp;&lt;/P&gt;&lt;PRE class=""&gt;&lt;CODE&gt;drop=total_male total_female&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The problem still exists.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 27 Sep 2023 19:05:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-running-total-for-certain-rows-in-SAS/m-p/896140#M354068</guid>
      <dc:creator>beihorse</dc:creator>
      <dc:date>2023-09-27T19:05:53Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate running total for certain rows in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-running-total-for-certain-rows-in-SAS/m-p/896146#M354073</link>
      <description>&lt;P&gt;Okay, so it seems as if something in the macro caused DSD to appear in the INFILE command when in fact it is not necessary. Also, the informats in the INPUT statement should be YYMMDD8. There is no informat YYMMDDN8. although there is a format YYMMDDN8.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.board_size_change;
  infile datalines truncover;
  input CompanyID:32. date:YYMMDD8. AnnualReportDate:YYMMDD8. male_dirs:32. female_dirs:32. male_dirs_ann:32. female_dirs_ann:32.;
  format date YYMMDDN8. AnnualReportDate YYMMDDN8.;
datalines;
3 20051201 20051201 7 0 0 0
3 20061027 . . . 1 0
3 20061201 20061201 8 0 0 0
6 20050701 20050701 8 1 0 0
6 20060701 20060701 7 1 0 0
6 20061207 . . . -1 0
6 20061207 . . . 1 0
6 20070207 . . . 0 -1
6 20070208 . . . 0 1
6 20070316 . . . -1 0
6 20070627 . . . 1 0
6 20070701 20070701 7 1 0 0
6 20080601 20080601 7 1 0 0
6 20090319 . . . 1 0
6 20090601 20090601 8 1 0 0
6 20100601 20100601 8 1 0 0
6 20101202 . . . 0 -1
6 20110701 20110701 8 0 0 0
6 20120112 . . . -1 0
6 20120112 . . . 0 1
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;after that, your code works where I have made one minor change, where the new data set is named BOARD_SIZE_CHANGE1&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data board_size_change1;
set board_size_change;
by companyID date;
retain total_male total_female;

if first.companyID = 1 then do; 
   total_male = 0;
   total_female = 0;
end;

*At annual report date;
if not missing(annualreportdate) then do;
    total_male=male_dirs;
    total_female=female_dirs;
    output;
end;

*At announcement;
if missing(annualreportdate) then do;
    total_male + male_dirs_ann;
    total_female + female_dirs_ann;
    output;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 27 Sep 2023 19:22:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-running-total-for-certain-rows-in-SAS/m-p/896146#M354073</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-09-27T19:22:48Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate running total for certain rows in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-running-total-for-certain-rows-in-SAS/m-p/896158#M354081</link>
      <description>&lt;P&gt;I think your doing more coding than needed.&amp;nbsp; I believe this produces what you are looking for.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;dm 'clear log;clear out;';
data work.board_size_change;
  infile datalines truncover;
  input CompanyID:32. date:YYMMDD8. AnnualReportDate:YYMMDD8. male_dirs:32. female_dirs:32. male_dirs_ann:32. female_dirs_ann:32.;
  format date YYMMDDN8. AnnualReportDate YYMMDDN8.;
datalines;
3 20051201 20051201 7 0 0 0
3 20061027 . . . 1 0
3 20061201 20061201 8 0 0 0
6 20050701 20050701 8 1 0 0
6 20060701 20060701 7 1 0 0
6 20061207 . . . -1 0
6 20061207 . . . 1 0
6 20070207 . . . 0 -1
6 20070208 . . . 0 1
6 20070316 . . . -1 0
6 20070627 . . . 1 0
6 20070701 20070701 7 1 0 0
6 20080601 20080601 7 1 0 0
6 20090319 . . . 1 0
6 20090601 20090601 8 1 0 0
6 20100601 20100601 8 1 0 0
6 20101202 . . . 0 -1
6 20110701 20110701 8 0 0 0
6 20120112 . . . -1 0
6 20120112 . . . 0 1
run;

data want;
 set board_size_change;
 by companyid ;

 if first.companyid then call missing(total_male,total_female);
 total_male  + male_dirs_ann;
 total_female+ female_dirs_ann;

 if not missing(annualreportdate) then do;
   total_male  = male_dirs;
   total_female= female_dirs;
 end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note this program assumes that total_male and total_female are new variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Editted insertion: It also assumes that the first obs for each companyid has a non-missing annualreportdate.&lt;/P&gt;</description>
      <pubDate>Wed, 27 Sep 2023 22:12:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-running-total-for-certain-rows-in-SAS/m-p/896158#M354081</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2023-09-27T22:12:36Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate running total for certain rows in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-running-total-for-certain-rows-in-SAS/m-p/896164#M354082</link>
      <description>&lt;P&gt;Thanks a lot. This works perfectly!&lt;/P&gt;</description>
      <pubDate>Wed, 27 Sep 2023 20:50:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-running-total-for-certain-rows-in-SAS/m-p/896164#M354082</guid>
      <dc:creator>beihorse</dc:creator>
      <dc:date>2023-09-27T20:50:27Z</dc:date>
    </item>
  </channel>
</rss>

