<?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 across columns based on conditions in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Summing-across-columns-based-on-conditions/m-p/749000#M235306</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data vertical;
	set test;
	array a1(3) type1-type3;
	array a2(3) number_retrieved1-number_retrieved3;
	do i=1 to dim(a1);
	type=a1(i);
	num=a2(i);
		if not missing(type) then output;
	end;
	
	keep id type num;
run;

proc sql number;
	create table want as select id, sum(num) as count, type
		from vertical
		group by id, type
		order by id;
quit;

proc transpose data=want out=want_t(drop=_name_);
	by id;
	id type;
	var count;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This will get you a dataset that is 1 row per ID with the colors as columns going across the top and the count of each color per ID.&lt;/P&gt;</description>
    <pubDate>Fri, 18 Jun 2021 22:05:56 GMT</pubDate>
    <dc:creator>tarheel13</dc:creator>
    <dc:date>2021-06-18T22:05:56Z</dc:date>
    <item>
      <title>Summing across columns based on conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Summing-across-columns-based-on-conditions/m-p/748899#M235267</link>
      <description>&lt;P&gt;Hello!&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a data set that looks as follows:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Say I have a basket with colored balls. I retrieve balls of type x, then record the number retrieved.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I need to first condition on type such that if the type is "Yellow" then I need to sum all the "number_retrievedX" associated with type "Yellow". How would I go about doing this? Thank you so much for your time!&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test; 
input id type1 number_retrieved1 type2 number_retrieved2 type3 number_retrieved3; 
datalines; 
1 Yellow 5 Blue 4 Yellow 7
1 Green 2 Yellow 3 Purple 8
2&amp;nbsp;Blue&amp;nbsp;5&amp;nbsp;Purple&amp;nbsp;12&amp;nbsp;Red&amp;nbsp;10&amp;nbsp;
3&amp;nbsp;Yellow&amp;nbsp;7&amp;nbsp;Yellow&amp;nbsp;3&amp;nbsp;Purple&amp;nbsp;6
3&amp;nbsp;Red&amp;nbsp;4&amp;nbsp;Yellow&amp;nbsp;10&amp;nbsp;Yellow&amp;nbsp;9
;&amp;nbsp;
run;&amp;nbsp;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;TABLE border="1" width="100%"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="12.5%" height="30px"&gt;ID&lt;/TD&gt;
&lt;TD width="12.5%" height="30px"&gt;Type1&lt;/TD&gt;
&lt;TD width="12.5%" height="30px"&gt;Number_retrieved1&lt;/TD&gt;
&lt;TD width="12.5%" height="30px"&gt;Type2&lt;/TD&gt;
&lt;TD width="12.5%" height="30px"&gt;Number_retrieved2&lt;/TD&gt;
&lt;TD width="12.5%" height="30px"&gt;Type3&lt;/TD&gt;
&lt;TD width="12.5%" height="30px"&gt;Number_retrieved3&lt;/TD&gt;
&lt;TD width="12.5%" height="30px"&gt;&lt;FONT color="#993300"&gt;Number_retrieved_total&lt;/FONT&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="12.5%" height="30px"&gt;1&lt;/TD&gt;
&lt;TD width="12.5%" height="30px"&gt;Yellow&lt;/TD&gt;
&lt;TD width="12.5%" height="30px"&gt;5&lt;/TD&gt;
&lt;TD width="12.5%" height="30px"&gt;Blue&lt;/TD&gt;
&lt;TD width="12.5%" height="30px"&gt;4&lt;/TD&gt;
&lt;TD width="12.5%" height="30px"&gt;Yellow&lt;/TD&gt;
&lt;TD width="12.5%" height="30px"&gt;7&lt;/TD&gt;
&lt;TD width="12.5%" height="30px"&gt;&lt;FONT color="#993300"&gt;12&lt;/FONT&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="12.5%" height="30px"&gt;1&lt;/TD&gt;
&lt;TD width="12.5%" height="30px"&gt;Green&lt;/TD&gt;
&lt;TD width="12.5%" height="30px"&gt;2&lt;/TD&gt;
&lt;TD width="12.5%" height="30px"&gt;Yellow&lt;/TD&gt;
&lt;TD width="12.5%" height="30px"&gt;3&lt;/TD&gt;
&lt;TD width="12.5%" height="30px"&gt;Purple&lt;/TD&gt;
&lt;TD width="12.5%" height="30px"&gt;8&lt;/TD&gt;
&lt;TD width="12.5%" height="30px"&gt;&lt;FONT color="#993300"&gt;3&lt;/FONT&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="12.5%" height="30px"&gt;2&lt;/TD&gt;
&lt;TD width="12.5%" height="30px"&gt;Blue&lt;/TD&gt;
&lt;TD width="12.5%" height="30px"&gt;5&lt;/TD&gt;
&lt;TD width="12.5%" height="30px"&gt;Purple&lt;/TD&gt;
&lt;TD width="12.5%" height="30px"&gt;12&lt;/TD&gt;
&lt;TD width="12.5%" height="30px"&gt;Red&lt;/TD&gt;
&lt;TD width="12.5%" height="30px"&gt;10&lt;/TD&gt;
&lt;TD width="12.5%" height="30px"&gt;&lt;FONT color="#993300"&gt;.&lt;/FONT&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="12.5%" height="30px"&gt;3&lt;/TD&gt;
&lt;TD width="12.5%" height="30px"&gt;Yellow&lt;/TD&gt;
&lt;TD width="12.5%" height="30px"&gt;7&lt;/TD&gt;
&lt;TD width="12.5%" height="30px"&gt;Yellow&lt;/TD&gt;
&lt;TD width="12.5%" height="30px"&gt;3&lt;/TD&gt;
&lt;TD width="12.5%" height="30px"&gt;Purple&lt;/TD&gt;
&lt;TD width="12.5%" height="30px"&gt;6&lt;/TD&gt;
&lt;TD width="12.5%" height="30px"&gt;&lt;FONT color="#993300"&gt;10&lt;/FONT&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="12.5%" height="30px"&gt;3&lt;/TD&gt;
&lt;TD width="12.5%" height="30px"&gt;Red&lt;/TD&gt;
&lt;TD width="12.5%" height="30px"&gt;4&lt;/TD&gt;
&lt;TD width="12.5%" height="30px"&gt;Yellow&lt;/TD&gt;
&lt;TD width="12.5%" height="30px"&gt;10&lt;/TD&gt;
&lt;TD width="12.5%" height="30px"&gt;Yellow&lt;/TD&gt;
&lt;TD width="12.5%" height="30px"&gt;9&lt;/TD&gt;
&lt;TD width="12.5%" height="30px"&gt;&lt;FONT color="#993300"&gt;&amp;nbsp;19&lt;/FONT&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;</description>
      <pubDate>Fri, 18 Jun 2021 14:33:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Summing-across-columns-based-on-conditions/m-p/748899#M235267</guid>
      <dc:creator>awardell</dc:creator>
      <dc:date>2021-06-18T14:33:26Z</dc:date>
    </item>
    <item>
      <title>Re: Summing across columns based on conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Summing-across-columns-based-on-conditions/m-p/748901#M235268</link>
      <description>&lt;P&gt;That's great that you provided your data set as SAS data step code.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test; 
input id type1 $ number_retrieved1 type2 $ number_retrieved2 type3 $ number_retrieved3; 
datalines; 
1 Yellow 5 Blue 4 Yellow 7
1 Green 2 Yellow 3 Purple 8
2 Blue 5 Purple 12 Red 10 
3 Yellow 7 Yellow 3 Purple 6
3 Red 4 Yellow 10 Yellow 9
; 
run; 
data want;
    set test;
    array type type:;
    array nn number_retrieved:;
    sum=0;
    do i=1 to dim(type);
        if type(i)='Yellow' then sum=sum+nn(i);
    end;
    drop i;
run;&lt;/CODE&gt;&amp;nbsp;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 18 Jun 2021 14:39:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Summing-across-columns-based-on-conditions/m-p/748901#M235268</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-06-18T14:39:18Z</dc:date>
    </item>
    <item>
      <title>Re: Summing across columns based on conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Summing-across-columns-based-on-conditions/m-p/749000#M235306</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data vertical;
	set test;
	array a1(3) type1-type3;
	array a2(3) number_retrieved1-number_retrieved3;
	do i=1 to dim(a1);
	type=a1(i);
	num=a2(i);
		if not missing(type) then output;
	end;
	
	keep id type num;
run;

proc sql number;
	create table want as select id, sum(num) as count, type
		from vertical
		group by id, type
		order by id;
quit;

proc transpose data=want out=want_t(drop=_name_);
	by id;
	id type;
	var count;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This will get you a dataset that is 1 row per ID with the colors as columns going across the top and the count of each color per ID.&lt;/P&gt;</description>
      <pubDate>Fri, 18 Jun 2021 22:05:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Summing-across-columns-based-on-conditions/m-p/749000#M235306</guid>
      <dc:creator>tarheel13</dc:creator>
      <dc:date>2021-06-18T22:05:56Z</dc:date>
    </item>
  </channel>
</rss>

