<?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: Calculate totals and sub-totals in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Calculate-totals-and-sub-totals/m-p/643498#M192051</link>
    <description>&lt;P&gt;1) on your last input line there is a dot instead of a comma after the level.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2) Use next code to create separate variables to define categories and sub categories:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA test;
INFILE DATALINES dlm=',' truncover;
INPUT level :6. name :$30. count :6.;
length cat $10;
catx = scan(name,-1,' ');
array ct cat1-cat5;
do i=1 to countw(catx,'.');
  ct(i) = scan(catx,i,'.');
end;
DATALINES;
1, Top-category 1, .
2, Sub-category 1.1, 10
2, Sub-category 1.2, 20
2, Sub-category 1.3, .
3, Sub-sub category 1.3.1, 5
3, Sub-sub category 1.3.2, 5
1, Top-category 2, .
2, Sub-category 2.1, 10
2, Sub-category 2.2, .
2, Sub-category 2.3, 5
;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;next step run proc summary to calculate totals and subtotals:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=test;
 class cat1-cat3;
 var count;&lt;BR /&gt; output out=summary sum=;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 28 Apr 2020 10:10:48 GMT</pubDate>
    <dc:creator>Shmuel</dc:creator>
    <dc:date>2020-04-28T10:10:48Z</dc:date>
    <item>
      <title>Calculate totals and sub-totals</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-totals-and-sub-totals/m-p/643492#M192046</link>
      <description>&lt;P&gt;Given the following sample data&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;DATA test;
INPUT level :6. name :$100. count :6.;
INFILE DATALINES DSD;
DATALINES;
1, Top-category 1, .
2, Sub-category 1.1, 10
2, Sub-category 1.2, 20
2, Sub-category 1.3, .
3, Sub-sub category 1.3.1, 5
3, Sub-sub category 1.3.2, 5
1, Top-category 2, .
2, Sub-category 2.1, 10
2, Sub-category 2.2, .
2, Sub-category 2.3, 5
;
RUN;&lt;/PRE&gt;&lt;P&gt;is there a way to calculate the sums of all sub-categories iteratively?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Examples:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;Category 1 should be 30 (10+20+5+5)&lt;/LI&gt;&lt;LI&gt;Sub-category 1.3 should be 10 (5+5)&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;The count-value is only given for the "lowest" categories. If the the value of the "lowest" category is missing "." this should actually be 0 (e.g. sub-category 2.2). This is why top-category 2 should be 15.&lt;/P&gt;</description>
      <pubDate>Tue, 28 Apr 2020 10:12:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-totals-and-sub-totals/m-p/643492#M192046</guid>
      <dc:creator>dstuder</dc:creator>
      <dc:date>2020-04-28T10:12:16Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate totals and sub-totals</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-totals-and-sub-totals/m-p/643493#M192047</link>
      <description>&lt;P&gt;Why should top-category 2 have a sum of 10, when 2.1 = 10 and 2.3 = 5? Why would you discard the value of 2.3?&lt;/P&gt;</description>
      <pubDate>Tue, 28 Apr 2020 09:29:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-totals-and-sub-totals/m-p/643493#M192047</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-04-28T09:29:10Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate totals and sub-totals</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-totals-and-sub-totals/m-p/643498#M192051</link>
      <description>&lt;P&gt;1) on your last input line there is a dot instead of a comma after the level.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2) Use next code to create separate variables to define categories and sub categories:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA test;
INFILE DATALINES dlm=',' truncover;
INPUT level :6. name :$30. count :6.;
length cat $10;
catx = scan(name,-1,' ');
array ct cat1-cat5;
do i=1 to countw(catx,'.');
  ct(i) = scan(catx,i,'.');
end;
DATALINES;
1, Top-category 1, .
2, Sub-category 1.1, 10
2, Sub-category 1.2, 20
2, Sub-category 1.3, .
3, Sub-sub category 1.3.1, 5
3, Sub-sub category 1.3.2, 5
1, Top-category 2, .
2, Sub-category 2.1, 10
2, Sub-category 2.2, .
2, Sub-category 2.3, 5
;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;next step run proc summary to calculate totals and subtotals:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=test;
 class cat1-cat3;
 var count;&lt;BR /&gt; output out=summary sum=;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 28 Apr 2020 10:10:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-totals-and-sub-totals/m-p/643498#M192051</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2020-04-28T10:10:48Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate totals and sub-totals</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-totals-and-sub-totals/m-p/643509#M192060</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/88384"&gt;@Shmuel&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for your solution, which works fine! Unfortunately, I was just giving these numbers in the variable `name` to make my question more understandable here. In reality, my data are like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA test;
INPUT level :6. name :$100. count :6.;
INFILE DATALINES DSD;
DATALINES;
01, Top, .
0101, Sub, 10
0102, Sub, 20
0103, Sub, .
01031, Sub-sub, 5
01032, Sub-sub, 5
02, Top, .
0201, Sub, 10
0202, Sub, 20
0203, Sub, .
02031, Sub-sub, 10
02032, Sub-sub, .
020321, Sub-sub-sub, 6,
020322, Sub-sub-sub, 4
;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;So, 0101 should be used instead of 1.1, 02031 instead of 2.3.1,&amp;nbsp; 020311 instead of 2.3.1.1 etc. Do you think you could adapt this for me?&lt;/P&gt;</description>
      <pubDate>Tue, 28 Apr 2020 11:00:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-totals-and-sub-totals/m-p/643509#M192060</guid>
      <dc:creator>dstuder</dc:creator>
      <dc:date>2020-04-28T11:00:53Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate totals and sub-totals</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-totals-and-sub-totals/m-p/643536#M192080</link>
      <description>&lt;P&gt;So your categories are defined in the LEVEL variable, then&lt;/P&gt;
&lt;P&gt;change next lines to separate the parent level from the sub-levels:&lt;/P&gt;
&lt;P&gt;instead:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;do i=1 to countw(catx,'.');
  ct(i) = scan(catx,i,'.');
end;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;use:&lt;/P&gt;
&lt;P&gt;1) read level as $6 informat instead a numeric informat&lt;/P&gt;
&lt;P&gt;then&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;array ct cat1-cat3;
do i=1 to length(level)-2 by 2;
   ct(i) = substr(level,i,2);
end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 28 Apr 2020 11:28:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-totals-and-sub-totals/m-p/643536#M192080</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2020-04-28T11:28:07Z</dc:date>
    </item>
  </channel>
</rss>

