<?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 counts and percentage from total in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Calculate-counts-and-percentage-from-total/m-p/483028#M125207</link>
    <description>&lt;P&gt;Use the right tool! PROC FREQ is the simplest way to get counts and percentages.&lt;/P&gt;</description>
    <pubDate>Wed, 01 Aug 2018 10:45:49 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2018-08-01T10:45:49Z</dc:date>
    <item>
      <title>Calculate counts and percentage from total</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-counts-and-percentage-from-total/m-p/482987#M125192</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;&lt;P&gt;I want to calculate counts(count members) and percentage from total &amp;nbsp;for each group.&lt;/P&gt;&lt;P&gt;The calculation is made from raw data table.&lt;/P&gt;&lt;P&gt;I know how to do it with proc sql but as you can see it is very long code.&lt;/P&gt;&lt;P&gt;I wonder if anyone suggest a better way(less code) to get same result.&lt;/P&gt;&lt;P&gt;The result is in a table called &amp;nbsp;Final&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/******Calculate counts and percentage from total*******/
/******Calculate counts and percentage from total*******/
/******Calculate counts and percentage from total*******/
/******Calculate counts and percentage from total*******/
/******Calculate counts and percentage from total*******/
data have;
input ID  score  ;
cards;
1  0
2  0
3  2
4  3
5  0
6  2
7  11
8  11
9  2
10 0
;
run;
PROC SQL;
	create table t1 as
	select 	score,1 as help,
                  count(*) as No_customers 
	from have
	group by score
;
QUIT;
PROC SQL;
	create table t2 as
	select 1 as help,
         sum(  No_customers) as Grand_Total_customers 
	from t1  
;
QUIT;
PROC SQL;
	create table t3 as
	select 	a.score,a.No_customers,
             a.No_customers/b.Grand_Total_customers as PCT_From_Total  format=percent7.1
	from t1 as a
	left join t2 as b
	on a.help=b.help
;
QUIT;
PROC SQL;
	create table t4 as
	select 	. as score  ,
	        sum(No_customers) as No_customers,
			sum(PCT_From_Total) as PCT_From_Total format=percent7.1
	from t3
;
QUIT;
Data Final;
Set t3  t4;
Run;
title;
proc print data=Final noobs;run;

 &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 01 Aug 2018 06:10:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-counts-and-percentage-from-total/m-p/482987#M125192</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2018-08-01T06:10:35Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate counts and percentage from total</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-counts-and-percentage-from-total/m-p/483004#M125200</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159549"&gt;@Ronein&lt;/a&gt;: It's easier when you use an appropriate toolset. For example, try this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc freq noprint data=have ;                                                            
  tables score / out=freq outcum ;                                                       
run ;                                                                                    
                                                                                         
data want (keep=score count percent rename=(count=No_customers percent=PCT_from_Total)) ;
  set freq end = z ;                                                                     
  output ;                                                                               
  if z ;                                                                                 
  count = cum_freq ;                                                                     
  percent = cum_pct ;                                                                    
  score = . ;                                                                            
  output ;                                                                               
  format percent 7.1 ;                                                                   
run ;                                                                                    &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;HTH&lt;/P&gt;&lt;P&gt;Paul D.&lt;/P&gt;</description>
      <pubDate>Wed, 01 Aug 2018 07:22:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-counts-and-percentage-from-total/m-p/483004#M125200</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2018-08-01T07:22:27Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate counts and percentage from total</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-counts-and-percentage-from-total/m-p/483008#M125201</link>
      <description>&lt;P&gt;Or proc tabulate:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format;
   picture pctfmt (round) other='009.9%';
run;

proc tabulate data=work.have;
   class score;
   table score='' all,n='No_customers' pctn='PCT_From_Total'*f=pctfmt. / box='score';
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 01 Aug 2018 07:39:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-counts-and-percentage-from-total/m-p/483008#M125201</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2018-08-01T07:39:29Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate counts and percentage from total</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-counts-and-percentage-from-total/m-p/483019#M125203</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159549"&gt;@Ronein&lt;/a&gt;:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;On second thought, if your score values are limited-range integers (say, from 0 to 100, as assumed below) and you would like to do everything in a single step, you can use a key-indexed table:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (keep = score n p rename=(n=No_customers p=PCT_From_Total)) ;
  array f [0:100] _temporary_ ;                                        
  do until (end) ;                                                     
    set have end = end ;                                               
    f [score] + 1 ;                                                    
  end ;                                                                
  t = sum (of f [*]) ;                                                 
  do score = lbound (f) to hbound (f) ;                                
    n = f [score] ;                                                    
    if nmiss (n) then continue ;                                       
    p = divide (n, t) ;                                                
    tp + p ;                                                           
    output ;                                                           
  end ;                                                                
  score = . ;                                                          
  n = t ;                                                              
  p = tp ;                                                             
  output ;                                                             
  format p percent7.1 ;                                                
run ;                                                                  &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;However, if your score values are arbitrary-type, arbitrary-range, you still can do it in one step, but then the key-indexed table needs to be replaced with a hash table. It makes code somewhat lengthier but also makes no assumptions about data:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (keep = score n p rename=(n=No_customers p=PCT_From_Total)) ;
  dcl hash h () ;                                                      
  h.definekey ("score") ;                                              
  h.definedata ("score", "n") ;                                        
  h.definedone () ;                                                    
  do until (end) ;                                                     
    set have end = end ;                                               
    if h.find() ne 0 then n = 1 ;                                      
    else                  n + 1 ;                                      
    h.replace() ;                                                      
  end ;                                                                
  dcl hiter i ("h") ;                                                  
  do while (i.next() = 0) ;                                            
    t + n ;                                                            
  end ;                                                                
  do while (i.next() = 0) ;                                            
    p = divide (n, t) ;                                                
    output ;                                                           
    tp + p ;                                                           
  end ;                                                                
  score = . ;                                                          
  n = t ;                                                              
  p = tp ;                                                             
  output ;                                                             
  format p percent7.1 ;                                                
run ;                                                                  &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;By the way, I do like the TABULATE solution by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15475"&gt;@andreas_lds&lt;/a&gt;&amp;nbsp;(and like this proc in general) but think that you need an output data set.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Paul D.&lt;/P&gt;</description>
      <pubDate>Wed, 01 Aug 2018 08:29:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-counts-and-percentage-from-total/m-p/483019#M125203</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2018-08-01T08:29:42Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate counts and percentage from total</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-counts-and-percentage-from-total/m-p/483028#M125207</link>
      <description>&lt;P&gt;Use the right tool! PROC FREQ is the simplest way to get counts and percentages.&lt;/P&gt;</description>
      <pubDate>Wed, 01 Aug 2018 10:45:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-counts-and-percentage-from-total/m-p/483028#M125207</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2018-08-01T10:45:49Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate counts and percentage from total</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-counts-and-percentage-from-total/m-p/483085#M125233</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/21262"&gt;@hashman&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;By the way, I do like the TABULATE solution by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15475"&gt;@andreas_lds&lt;/a&gt;&amp;nbsp;(and like this proc in general) but think that you need an output data set.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Paul D.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Proc Tabulate will create output data sets, just add Out=datasetname to the Proc statement. However the structure of the output set doesn't look like what you might expect (at least not until you have used it a bit) with table indicators, All summary indicators, variables having statistics appended to the names like the Autoname option in proc means/summary, a different format of the _type_ variable and such. I do have some use for the tabulate output sets so get to dig into these. Often a data step is needed for some post processing though.&lt;/P&gt;</description>
      <pubDate>Wed, 01 Aug 2018 15:07:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-counts-and-percentage-from-total/m-p/483085#M125233</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-08-01T15:07:10Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate counts and percentage from total</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-counts-and-percentage-from-total/m-p/483090#M125236</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But of course you're right. What I meant is that&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15475"&gt;@andreas_lds&lt;/a&gt;&amp;nbsp;didn't offer that provision. And your points about the TABULATE output data set format ought to be well taken. But even if the proc spitted it out cleanly, some post-processing would be needed, anyway (e.g., to set the score to null for the totals). The TABULATE output data set can still be quite useful under some scenarios, post-processing notwithstanding, since the proc can do quite a bit of rather convoluted aggregation in a few lines of code (and do it right, too).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Best&lt;/P&gt;&lt;P&gt;Paul D.&amp;nbsp; &amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 01 Aug 2018 15:27:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-counts-and-percentage-from-total/m-p/483090#M125236</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2018-08-01T15:27:44Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate counts and percentage from total</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-counts-and-percentage-from-total/m-p/585174#M166829</link>
      <description>And I've doubt ...&lt;BR /&gt;I've two variables TRT1,TRT2 those has values like 45(23),55(56) after concatinating count and percentage and I want to add new variable total which should contains total of count and percentage like 100(79)... Can you please tell me how to create</description>
      <pubDate>Fri, 30 Aug 2019 11:59:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-counts-and-percentage-from-total/m-p/585174#M166829</guid>
      <dc:creator>sasuser123123</dc:creator>
      <dc:date>2019-08-30T11:59:30Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate counts and percentage from total</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-counts-and-percentage-from-total/m-p/585191#M166837</link>
      <description>&lt;P&gt;Please explain in detail.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Show us (a portion of) your input data and the desired output.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 30 Aug 2019 12:57:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-counts-and-percentage-from-total/m-p/585191#M166837</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-08-30T12:57:41Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate counts and percentage from total</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-counts-and-percentage-from-total/m-p/585198#M166840</link>
      <description>I've variables age ,sex ,TRT(contains trt1 and trt2) in a datase, now I want to calculate count and percentage of variables Region with respect to TRT and sex with respect to TRT..so I did that by using proc freq.I got output like...(after concatenate the count and percentage and also transpose the data)&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Region. Trt1. Trt2&lt;BR /&gt;India. 23(28). 22(22)&lt;BR /&gt;Usa. 22(22). 23(28)&lt;BR /&gt;&lt;BR /&gt;So no need the another variable Total after Trt2 that should contain total of trt1 and trt2 along with percentage (like For india Total should be 45(50) and for usa 45(50)..&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Fri, 30 Aug 2019 13:15:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-counts-and-percentage-from-total/m-p/585198#M166840</guid>
      <dc:creator>sasuser123123</dc:creator>
      <dc:date>2019-08-30T13:15:50Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate counts and percentage from total</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-counts-and-percentage-from-total/m-p/585388#M166931</link>
      <description>Hello, sorry for the mistake in above question..&lt;BR /&gt;So now I need the another variable Total after Trt2 that should contain total of trt1 and trt2 along with percentage (like For india Total should be 45(50) and for usa 45(50).</description>
      <pubDate>Sat, 31 Aug 2019 05:01:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-counts-and-percentage-from-total/m-p/585388#M166931</guid>
      <dc:creator>sasuser123123</dc:creator>
      <dc:date>2019-08-31T05:01:52Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate counts and percentage from total</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-counts-and-percentage-from-total/m-p/585436#M166952</link>
      <description>&lt;P&gt;You haven't shown us the input data. Please provide a portion of (not necessarily all of) your data, via these instructions: &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;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Data should be in the form above, not any other form.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;We can't really provide code until we know what the input data looks like.&lt;/P&gt;</description>
      <pubDate>Sat, 31 Aug 2019 20:40:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-counts-and-percentage-from-total/m-p/585436#M166952</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-08-31T20:40:51Z</dc:date>
    </item>
  </channel>
</rss>

