DATA Step, Macro, Functions and more

Calculate percentage of total

Reply
Frequent Contributor
Frequent Contributor
Posts: 78

Calculate percentage of total

[ Edited ]

I have the below dataset

 

data A;
Input CHANNEL $ AMOUNT ;
cards;
ABC 110
ABC 220
ABC 770
BBC 810
BBC 190
;
run;

 

May I know how to generate the below output with the above data? Thanks.

 

Channel  Amount  Percent
ABC        110         0.1
ABC         220        0.2
ABC         770        0.7
BBC         110        0.11
BBC          890       0.89
Super User
Super User
Posts: 9,211

Re: SAS QUERY

Proc means/summary/freq.  Simple search on the SAS docs gives examples:

http://support.sas.com/kb/25/237.html

Occasional Contributor
Posts: 6

Re: Calculate percentage of total

Hi scb,

 

As RW9 already stated in his post see the example link he provided.

But hereby a little bit of code to get you started:

 

Data A;
Input Channel $ Amount ;
cards;
ABC 110
ABC 220
ABC 770
BBC 810
BBC 190
;
run;

Proc SQL;                                                       
  Create Table B AS                                     
    Select *,(Amount/SUM(Amount)) AS Percent format=6.2      
  	From A                                             
	Group BY Channel;
Quit;

 

Super User
Posts: 10,618

Re: Calculate percentage of total

data A;
Input CHANNEL $ AMOUNT ;
cards;
ABC 110
ABC 220
ABC 770
BBC 810
BBC 190
;
run;

proc sql;
select *,AMOUNT/sum(AMOUNT)  format=percent8.2
 from a as x
   group by CHANNEL;
quit;
Contributor
Posts: 37

Re: Calculate percentage of total

data A;
Input CHANNEL $ AMOUNT ;
cards;
ABC 110
ABC 220
ABC 770
BBC 810
BBC 190
;
run;

DATA A2;
SET A;
CHAR_AMOUNT=AMOUNT;
RUN;

PROC FORMAT;
PICTURE PCTPIC low-high='000.0%';
RUN;


TITLE;
PROC TABULATE DATA=WORK.A2 OUT=TEST ;
CLASS CHANNEL CHAR_AMOUNT;
KEYLABEL PCTSUM="% within Channel";
VAR amount;
TABLE CHANNEL*CHAR_AMOUNT, (amount*(SUM*F=6. PCTSUM<AMOUNT*CHAR_AMOUNT>*F=PCTPIC.)) / RTS=20;
RUN;
Ask a Question
Discussion stats
  • 4 replies
  • 116 views
  • 0 likes
  • 5 in conversation