<?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 Sum up - by category wise in a column in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Sum-up-by-category-wise-in-a-column/m-p/479422#M123752</link>
    <description>&lt;P&gt;Hi Community,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Just consider the dataset as an example.&amp;nbsp;&lt;/P&gt;&lt;P&gt;w - wood; p- plastic ; s - steel&amp;nbsp; ; Items 1 2 3 4 5....(wooditem1 w1; plasticitem5 p5; steelitem3 s3 and so on)....&lt;/P&gt;&lt;P&gt;Here these 3 stuffs have a value. I want to sum up the value by category.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data xxx;
input aa$ bb$ cc$ dd$ c1 c2 c3 c4;
cards;
w1 p2 s4 w3 10 45 14 5
s1 s3 w2 p4 20 14 15 84
p2 w4 w5 w4 12 18 45 7
s1 w3 p5 s2 23 56 87 44
run;
proc print data = xxx;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;This is how my output should be as a result&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;W&lt;/TD&gt;&lt;TD&gt;S&lt;/TD&gt;&lt;TD&gt;P&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;15&lt;/TD&gt;&lt;TD&gt;14&lt;/TD&gt;&lt;TD&gt;45&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;34&lt;/TD&gt;&lt;TD&gt;15&lt;/TD&gt;&lt;TD&gt;84&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;12&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;70&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;…&lt;/TD&gt;&lt;TD&gt;…&lt;/TD&gt;&lt;TD&gt;…&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;Could any one suggest some code by SAS or Proc SQL to resolve it.&lt;/P&gt;&lt;P&gt;Thanks in advance!&lt;/P&gt;</description>
    <pubDate>Thu, 19 Jul 2018 09:32:05 GMT</pubDate>
    <dc:creator>Sathish_jammy</dc:creator>
    <dc:date>2018-07-19T09:32:05Z</dc:date>
    <item>
      <title>Sum up - by category wise in a column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sum-up-by-category-wise-in-a-column/m-p/479422#M123752</link>
      <description>&lt;P&gt;Hi Community,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Just consider the dataset as an example.&amp;nbsp;&lt;/P&gt;&lt;P&gt;w - wood; p- plastic ; s - steel&amp;nbsp; ; Items 1 2 3 4 5....(wooditem1 w1; plasticitem5 p5; steelitem3 s3 and so on)....&lt;/P&gt;&lt;P&gt;Here these 3 stuffs have a value. I want to sum up the value by category.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data xxx;
input aa$ bb$ cc$ dd$ c1 c2 c3 c4;
cards;
w1 p2 s4 w3 10 45 14 5
s1 s3 w2 p4 20 14 15 84
p2 w4 w5 w4 12 18 45 7
s1 w3 p5 s2 23 56 87 44
run;
proc print data = xxx;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;This is how my output should be as a result&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;W&lt;/TD&gt;&lt;TD&gt;S&lt;/TD&gt;&lt;TD&gt;P&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;15&lt;/TD&gt;&lt;TD&gt;14&lt;/TD&gt;&lt;TD&gt;45&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;34&lt;/TD&gt;&lt;TD&gt;15&lt;/TD&gt;&lt;TD&gt;84&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;12&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;70&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;…&lt;/TD&gt;&lt;TD&gt;…&lt;/TD&gt;&lt;TD&gt;…&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;Could any one suggest some code by SAS or Proc SQL to resolve it.&lt;/P&gt;&lt;P&gt;Thanks in advance!&lt;/P&gt;</description>
      <pubDate>Thu, 19 Jul 2018 09:32:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sum-up-by-category-wise-in-a-column/m-p/479422#M123752</guid>
      <dc:creator>Sathish_jammy</dc:creator>
      <dc:date>2018-07-19T09:32:05Z</dc:date>
    </item>
    <item>
      <title>Re: Sum up - by category wise in a column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sum-up-by-category-wise-in-a-column/m-p/479479#M123785</link>
      <description>&lt;P&gt;Try below code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let name_list =aa bb cc dd;

%macro loop();

	data xxx2;
		set xxx;

		w = 0;
		s = 0;
		p = 0;

		%let i=1;
		%do %while (%scan(&amp;amp;name_list, &amp;amp;i) ne );

   			%let next_name = %scan(&amp;amp;name_list, &amp;amp;i);
			if substr(&amp;amp;next_name.,1,1) = 'w' then w = w + c&amp;amp;i.;
			else if substr(&amp;amp;next_name.,1,1) = 's' then s = s + c&amp;amp;i.;
			else if substr(&amp;amp;next_name.,1,1) = 'p' then p = p + c&amp;amp;i.;
   			%let i = %eval(&amp;amp;i + 1);

		%end;

	run;

%mend;

%loop();&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If the variables your using to identify each material are easy to loop through (i.e. mat1,mat2,mat3.......) then the above can be simplified.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Andy&lt;/P&gt;</description>
      <pubDate>Thu, 19 Jul 2018 13:14:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sum-up-by-category-wise-in-a-column/m-p/479479#M123785</guid>
      <dc:creator>Andy_D</dc:creator>
      <dc:date>2018-07-19T13:14:53Z</dc:date>
    </item>
    <item>
      <title>Re: Sum up - by category wise in a column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sum-up-by-category-wise-in-a-column/m-p/479482#M123788</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data xxx;
input aa$ bb$ cc$ dd$ c1 c2 c3 c4;
cards;
w1 p2 s4 w3 10 45 14 5
s1 s3 w2 p4 20 14 15 84
p2 w4 w5 w4 12 18 45 7
s1 w3 p5 s2 23 56 87 44
;
run;

data want;
 set xxx;
 w=0;p=0;s=0;
 array x{*} $ aa bb cc dd;
 array y{*} c1-c4;
 do i=1 to dim(x);
   if x{i}=: 'w' then w+y{i};
    else if x{i}=: 'p' then p+y{i};
     else if x{i}=: 's' then s+y{i};
 end;
 keep w p s;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 19 Jul 2018 13:26:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sum-up-by-category-wise-in-a-column/m-p/479482#M123788</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2018-07-19T13:26:24Z</dc:date>
    </item>
    <item>
      <title>Re: Sum up - by category wise in a column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sum-up-by-category-wise-in-a-column/m-p/480114#M124046</link>
      <description>&lt;P&gt;Thank you!&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/221923"&gt;@Andy_D&lt;/a&gt;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&lt;/P&gt;&lt;P&gt;Your code works well.&lt;/P&gt;</description>
      <pubDate>Sat, 21 Jul 2018 04:53:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sum-up-by-category-wise-in-a-column/m-p/480114#M124046</guid>
      <dc:creator>Sathish_jammy</dc:creator>
      <dc:date>2018-07-21T04:53:44Z</dc:date>
    </item>
    <item>
      <title>Re: Sum up - by category wise in a column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sum-up-by-category-wise-in-a-column/m-p/480169#M124073</link>
      <description>&lt;P&gt;Note that in the rows 2-3 of your proposed output the values of W and S are swapped. Assuming that it's a typo, you've already been offered a solution. One problem with it is that the output variables W, S, P are hard coded. It's fine when you know that is all you have. But in the real world with the data structured like yours - when the information about output metadata is embedded into the data, the aggregate variable names cannot be determined without a preliminary pass through the input: Doing otherwise would mean making assumption about the data.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;One way of avoiding this rather precarious practice is to analyze the input data set first by reading it in full, find out what the distinct prefixes in the values of the variables aa--dd are and then hard code them into a conditional structure, such as if-then-else. Another, more dynamic, approach is to let SAS do the job as shown below. This way, you don't care what the prefixes are: They are discovered in the first pass, and in the ensuing aggregating pass, the compiler knows the output variable names and types automatically.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;                                                    
  input aa$ bb$ cc$ dd$ c1 c2 c3 c4 ;                          
  cards ;                                                      
w1 p2 s4 w3 10 45 14  5                                        
s1 s3 w2 p4 20 14 15 84                                        
p2 w4 w5 w4 12 18 45  7                                        
s1 w3 p5 s2 23 56 87 44                                        
run ;                                                          
/* 1st pass: Find output vnames for 2nd pass */                
data _null_ ;                                                  
  do until (z) ;                                               
    set have end = z ;                                         
    array ch _char_ ;                                          
    length vv $ 32767 ;                                        
    do over ch ;                                               
      v = compress (ch, , "d") ;                               
      if not findw (vv, cats (v)) then vv = catx (" ", vv, v) ;
    end ;                                                      
  end ;                                                        
  call symputx ("vv", vv) ;                                    
run ;                                                          
/* 2nd pass: Aggregate into var list &amp;amp;vv */                    
data want (keep = &amp;amp;vv) ;                                       
  set have ;                                                   
  array ch _char_ ;                                            
  array nn _numeric_ ;                                         
  array vv (j) &amp;amp;vv ;                                           
  do over vv ;   &lt;BR /&gt;    vv = 0 ;                                              
    do over ch ;                                               
      if ch =: vname (vv) then vv = sum (vv, nn) ;          
    end ;                                                      
  end ;                                                        
run ;                                                          &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The correct output will look as follows:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt; W   P   S
----------
15  45  14
15  84  34
70  12   0
56  87  67&lt;/PRE&gt;&lt;P&gt;Paul D.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 21 Jul 2018 18:09:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sum-up-by-category-wise-in-a-column/m-p/480169#M124073</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2018-07-21T18:09:04Z</dc:date>
    </item>
  </channel>
</rss>

