<?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: Yearly conditional sum in SAS in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Yearly-conditional-sum-in-SAS/m-p/508302#M136506</link>
    <description>&lt;P&gt;Here's how I would do it:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let first_year=2012;
%let last_year=2014;
proc transpose data=have out=long;
  var col1-col4;
  by year pqr notsorted;
run;
  
proc sort data=long;
  by _name_;
run;

data want;
  do until(last._name_);
    set long;
    by _name_;
	total+pqr*col1;
    array years(&amp;amp;first_year:&amp;amp;last_year) year&amp;amp;first_year-year&amp;amp;last_year;
    years(year)+pqr*col1;
    end;
  output;
  call missing(total,of years(*));
  keep _name_ total year&amp;amp;first_year-year&amp;amp;last_year; 
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 29 Oct 2018 11:17:37 GMT</pubDate>
    <dc:creator>s_lassen</dc:creator>
    <dc:date>2018-10-29T11:17:37Z</dc:date>
    <item>
      <title>Yearly conditional sum in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Yearly-conditional-sum-in-SAS/m-p/508107#M136418</link>
      <description>&lt;P&gt;&lt;SPAN&gt;I have a below table&lt;/SPAN&gt;&lt;/P&gt;&lt;PRE&gt;+------+------+------+------+------+-----+
    |  Yr  | col1 | col2 | col3 | col4 | PQR |
    +------+------+------+------+------+-----+
    | 2012 |    1 |    0 |    1 |    1 |   2 |
    | 2012 |    0 |    1 |    0 |    0 |   4 |
    | 2013 |    1 |    1 |    1 |    1 |   6 |
    | 2014 |    0 |    0 |    0 |    0 |   8 |
    | 2012 |    1 |    0 |    1 |    1 |   7 |
    | 2013 |    0 |    1 |    0 |    0 |   3 |
    | 2014 |    1 |    0 |    1 |    1 |   2 |
    | 2012 |    0 |    1 |    0 |    0 |  10 |
    | 2014 |    0 |    0 |    1 |    0 |  12 |
    | 2014 |    0 |    0 |    0 |    0 |   5 |
    +------+------+------+------+------+-----+&lt;/PRE&gt;&lt;P&gt;&lt;SPAN&gt;The output I want is as below&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;+------+-------+------+------+------+
    |      | Total | 2012 | 2013 | 2014 |
    +------+-------+------+------+------+
    | col1 |    17 |    9 |    6 |    2 |
    | col2 |    23 |   14 |    9 |    0 |
    | col3 |    29 |    9 |    6 |   14 |
    | col4 |    17 |    9 |    6 |    2 |
    +------+-------+------+------+------+&lt;/PRE&gt;&lt;P&gt;&lt;SPAN&gt;For row `col1` in my output table&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The column `Total` is `SUM(PQR)` when `col1` is 1 my input table&lt;BR /&gt;&lt;BR /&gt;The value `17` is `SUM(PQR)` when `col1` is 1 in my input table&lt;BR /&gt;&lt;BR /&gt;The value in col `2012` is `SUM(PQR)` when `col1` is 1 and `Yr=2012` in my input table&lt;BR /&gt;&lt;BR /&gt;The value `9` is `SUM(PQR)` when `col1` is 1 and `Yr=2012` in my input table&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Similarly `6` in column `2013` is `SUM(PQR)` when `col1` is 1 and `Yr` is `2013`&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hope the process to get output table is understood&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help will be appreciated&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 28 Oct 2018 13:05:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Yearly-conditional-sum-in-SAS/m-p/508107#M136418</guid>
      <dc:creator>Rookie_123</dc:creator>
      <dc:date>2018-10-28T13:05:49Z</dc:date>
    </item>
    <item>
      <title>Re: Yearly conditional sum in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Yearly-conditional-sum-in-SAS/m-p/508111#M136421</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;redone below in a datastep:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&amp;nbsp;&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;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 28 Oct 2018 17:24:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Yearly-conditional-sum-in-SAS/m-p/508111#M136421</guid>
      <dc:creator>VDD</dc:creator>
      <dc:date>2018-10-28T17:24:17Z</dc:date>
    </item>
    <item>
      <title>Re: Yearly conditional sum in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Yearly-conditional-sum-in-SAS/m-p/508113#M136422</link>
      <description>&lt;P&gt;Thanks but I wanted it in SAS&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 28 Oct 2018 13:57:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Yearly-conditional-sum-in-SAS/m-p/508113#M136422</guid>
      <dc:creator>Rookie_123</dc:creator>
      <dc:date>2018-10-28T13:57:37Z</dc:date>
    </item>
    <item>
      <title>Re: Yearly conditional sum in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Yearly-conditional-sum-in-SAS/m-p/508115#M136423</link>
      <description>&lt;P&gt;the code I provided is SAS code.&amp;nbsp; Try it&lt;/P&gt;&lt;P&gt;Please explain&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/225671"&gt;@Rookie_123&lt;/a&gt; what do you mean "wanted it in SAS"&lt;/P&gt;</description>
      <pubDate>Sun, 28 Oct 2018 14:05:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Yearly-conditional-sum-in-SAS/m-p/508115#M136423</guid>
      <dc:creator>VDD</dc:creator>
      <dc:date>2018-10-28T14:05:45Z</dc:date>
    </item>
    <item>
      <title>Re: Yearly conditional sum in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Yearly-conditional-sum-in-SAS/m-p/508116#M136424</link>
      <description>&lt;P&gt;I wanted to avoid PROC SQL part.Do it entirely in SAS without using PROC SQL at all&lt;/P&gt;</description>
      <pubDate>Sun, 28 Oct 2018 14:09:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Yearly-conditional-sum-in-SAS/m-p/508116#M136424</guid>
      <dc:creator>Rookie_123</dc:creator>
      <dc:date>2018-10-28T14:09:11Z</dc:date>
    </item>
    <item>
      <title>Re: Yearly conditional sum in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Yearly-conditional-sum-in-SAS/m-p/508117#M136425</link>
      <description>&lt;P&gt;Proc SQL is SAS.&lt;/P&gt;&lt;P&gt;Do you mean you want to do the process in a datastep?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input year col1 col2 col3 col4 pqr;
cards;
2012 1 0 1 1 2
2012 0 1 0 0 4
2013 1 1 1 1 6
2014 0 0 0 0 8
2012 1 0 1 1 7
2013 0 1 0 0 3
2014 1 0 1 1 2
2012 0 1 0 0 10
2014 0 0 1 0 12
2014 0 0 0 0 5
;

proc sort data=have out=havesort;
	by year;
run;

data have2(drop=total1 total2 total3 total4 pqr);
	retain total1 total2 total3 total4 0;
	set havesort;
	by year;
	if col1&amp;gt;0 then total1 + pqr;
	if col2&amp;gt;0 then total2 + pqr;
	if col3&amp;gt;0 then total3 + pqr;
	if col4&amp;gt;0 then total4 + pqr;
	if last.year then
		do;
			col1=total1;
			col2=total2;
			col3= total3;
			col4=total4;
			output;
			total1=0;
			total2 =0;
			total3 =0;
			total4 =0;
		end;
run;

proc transpose data=have2 out=have3;
id year;
run;

data want;
	set have3;
	total = sum(_2012,_2013,_2014);
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;using a datastep&lt;/P&gt;</description>
      <pubDate>Sun, 28 Oct 2018 15:06:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Yearly-conditional-sum-in-SAS/m-p/508117#M136425</guid>
      <dc:creator>VDD</dc:creator>
      <dc:date>2018-10-28T15:06:47Z</dc:date>
    </item>
    <item>
      <title>Re: Yearly conditional sum in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Yearly-conditional-sum-in-SAS/m-p/508141#M136434</link>
      <description>&lt;P&gt;The code may not be the best but could be helpful&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input   Yr$  col1 col2 col3 col4 PQR ;
cards; 
 2012    1    0    1    1   2 
 2012    0    1    0    0   4 
 2013    1    1    1    1   6 
 2014    0    0    0    0   8 
 2012    1    0    1    1   7 
 2013    0    1    0    0   3 
 2014    1    0    1    1   2 
 2012    0    1    0    0  10 
 2014    0    0    1    0  12 
 2014    0    0    0    0   5 
 ;

data have;
set have;
output;
yr='Total';
output;
run;

proc sort data=have;by yr;run;

data sum;
set have;
by yr;
array newcol1(*) col1 col2 col3 col4;
array newcol2(*) col_1 col_2 col_3 col_4;
do i = 1 to dim(newcol1);
if newcol1(i)=1 then newcol2(i)=pqr;
end;
drop col1 col2 col3 col4;
run;

proc sql;
create table sum2 as select yr,sum(col_1) as col1, 
sum(col_2) as col2, 
sum(col_3) as col3, 
sum(col_4) as col4 from sum group by yr;
quit;

proc transpose data=sum2 out=want;
id yr;
var col1 col2 col3 col4 ;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 28 Oct 2018 17:58:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Yearly-conditional-sum-in-SAS/m-p/508141#M136434</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2018-10-28T17:58:25Z</dc:date>
    </item>
    <item>
      <title>Re: Yearly conditional sum in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Yearly-conditional-sum-in-SAS/m-p/508196#M136447</link>
      <description>&lt;P&gt;If what you really want is a report rather than a data set, then it is well worth learning about proc tabulate:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc tabulate data=have;
  class year;    /*classifiers*/
  var col1 col2 col3 col4;  /* "analysis vars (to enable a SUM request)"*/
  weight pqr;
  tables 
    col1 col2 col3 col4                        /*rows */
    ,
    (all='Total' year=' ')*sum=' '*f=comma8.0  /*columns*/
  /rts=10 misstext='0';
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The "weight" statement identifies a variable to be used as a weight in calculating sums, means, variances, when applied to "analysis" variables (COL1 .. COL4 as specified in the VAR statement).&amp;nbsp; It has nothing to do with CLASS variables (year).&amp;nbsp; So the TABLES statement asks for:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;One row for each COL variable&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; (the row specification precedes the comma)&lt;/LI&gt;
&lt;LI&gt;Then (after a comma) specifiy the columns as:
&lt;OL&gt;
&lt;LI&gt;ALL (the entire dataset, i.e. all years), followed by a column for each year&lt;/LI&gt;
&lt;LI&gt;Populate the cell with sums&amp;nbsp; (of the respective &lt;EM&gt;&lt;STRONG&gt;weighted&lt;/STRONG&gt;&lt;/EM&gt;&amp;nbsp;analysis variables)&lt;/LI&gt;
&lt;/OL&gt;
&lt;/LI&gt;
&lt;LI&gt;All the =' ' just applies labels to appear in the table.&lt;BR /&gt;&lt;BR /&gt;Edited addition: You can remove all the &lt;EM&gt;&lt;STRONG&gt;='some text'&lt;/STRONG&gt;&lt;/EM&gt;&amp;nbsp; or &lt;EM&gt;&lt;STRONG&gt;=''&lt;/STRONG&gt;&lt;/EM&gt; to demonstrate that all they do is place (or remove) text&amp;nbsp;in the printed table.&amp;nbsp; In the case of text removal (i.e. &lt;EM&gt;&lt;STRONG&gt;=''&lt;/STRONG&gt;&lt;/EM&gt;) a cell can actually be removed.&lt;/LI&gt;
&lt;LI&gt;The "f=comma8.0" specifies the format to be applied within each cell&lt;/LI&gt;
&lt;LI&gt;you can look up the rest&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;At first, proc tabulate may not seem intuitive, but once you grasp the syntax, it's a powerful tool.&lt;/P&gt;</description>
      <pubDate>Mon, 29 Oct 2018 04:57:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Yearly-conditional-sum-in-SAS/m-p/508196#M136447</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2018-10-29T04:57:30Z</dc:date>
    </item>
    <item>
      <title>Re: Yearly conditional sum in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Yearly-conditional-sum-in-SAS/m-p/508205#M136450</link>
      <description>&lt;P&gt;Never ever enjoyed reading the tabulate doc , your notes rather made me comprehend, enjoy and pay attention. Very neat and sound. Gracias!&lt;/P&gt;</description>
      <pubDate>Mon, 29 Oct 2018 01:23:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Yearly-conditional-sum-in-SAS/m-p/508205#M136450</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-10-29T01:23:29Z</dc:date>
    </item>
    <item>
      <title>Re: Yearly conditional sum in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Yearly-conditional-sum-in-SAS/m-p/508302#M136506</link>
      <description>&lt;P&gt;Here's how I would do it:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let first_year=2012;
%let last_year=2014;
proc transpose data=have out=long;
  var col1-col4;
  by year pqr notsorted;
run;
  
proc sort data=long;
  by _name_;
run;

data want;
  do until(last._name_);
    set long;
    by _name_;
	total+pqr*col1;
    array years(&amp;amp;first_year:&amp;amp;last_year) year&amp;amp;first_year-year&amp;amp;last_year;
    years(year)+pqr*col1;
    end;
  output;
  call missing(total,of years(*));
  keep _name_ total year&amp;amp;first_year-year&amp;amp;last_year; 
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 29 Oct 2018 11:17:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Yearly-conditional-sum-in-SAS/m-p/508302#M136506</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2018-10-29T11:17:37Z</dc:date>
    </item>
    <item>
      <title>Re: Yearly conditional sum in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Yearly-conditional-sum-in-SAS/m-p/508312#M136512</link>
      <description>&lt;P&gt;Since you already have the zeros and ones, using PROC MEANS/PROC SUMMARY will compute the conditional sums easily using a WEIGHT statement. For example, using COL1:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=have;
    class year;
    var pqr/weight=col1;
    output out=sums sum=sum_pqr;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;No need to write your own code to do conditional sums.&lt;/P&gt;</description>
      <pubDate>Mon, 29 Oct 2018 12:17:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Yearly-conditional-sum-in-SAS/m-p/508312#M136512</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2018-10-29T12:17:01Z</dc:date>
    </item>
    <item>
      <title>Re: Yearly conditional sum in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Yearly-conditional-sum-in-SAS/m-p/508435#M136557</link>
      <description>&lt;P&gt;Are you &lt;STRONG&gt;implying&lt;/STRONG&gt; that similar rules are involved with col2 col3 and col4?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's another approach&lt;/P&gt;
&lt;PRE&gt;data have;
   input yr col1-col4 pqr;
datalines;
     2012     1     0     1     1    2 
     2012     0     1     0     0    4 
     2013     1     1     1     1    6 
     2014     0     0     0     0    8 
     2012     1     0     1     1    7 
     2013     0     1     0     0    3 
     2014     1     0     1     1    2 
     2012     0     1     0     0   10 
     2014     0     0     1     0   12 
     2014     0     0     0     0    5 
run;

data trans;
   set have;
   array c col1-col4;
   length name $ 10;
   do i=1 to dim(c);
      weightvar = c[i];
      name = vname(c[i]);
      output;
   end;
   keep yr pqr weightvar name;
run;

proc tabulate data=trans;
   class yr name;
   var pqr;
   weight weightvar;
   table name*pqr=''*sum=''*f=best5.,
         (all='Total' yr='')
         /row=float
   ;
run;
         &lt;/PRE&gt;
&lt;P&gt;Your variables col1 through col4 make me suspect that perhaps proc transpose was used in creating the data. Perhaps that prior step wasn't needed for this output in that case.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 29 Oct 2018 17:54:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Yearly-conditional-sum-in-SAS/m-p/508435#M136557</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-10-29T17:54:51Z</dc:date>
    </item>
    <item>
      <title>Re: Yearly conditional sum in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Yearly-conditional-sum-in-SAS/m-p/509470#M136970</link>
      <description>&lt;P&gt;Everyone has helped in their own way to achieve what I want.Thanks for your help&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 01 Nov 2018 12:04:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Yearly-conditional-sum-in-SAS/m-p/509470#M136970</guid>
      <dc:creator>Rookie_123</dc:creator>
      <dc:date>2018-11-01T12:04:10Z</dc:date>
    </item>
  </channel>
</rss>

