The sample code on the Full Code tab illustrates how to calcutate BY-group totals for a variable and then create a variable that shows the percentage that each observation contributes to the total for that BY-group.
Note: Use PROC MEANS to calculate a total for each BY-group and to create a new data set that contains one observation for each BY-group. Then use a one-to-many merge to combine this data set with the original data set and calculate the percentage that a variable in each observation contributes to the BY-group total.
You can perform the same task with PROC SQL. See Sample 2 on the Full Code tab.
These sample files and code examples are provided by SAS Institute Inc. "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.
data sales;
input @1 region $char8. @10 repid 4. @15 amount 10. ;
format amount dollar12.;
datalines;
NORTH 1001 1000000
NORTH 1002 1100000
NORTH 1003 1550000
NORTH 1008 1250000
NORTH 1005 900000
SOUTH 1007 2105000
SOUTH 1010 875000
SOUTH 1012 1655000
EAST 1051 2508000
EAST 1055 1805000
;
run;
proc sort data=sales;
by region;
run;
/* Create REGTOT, a data set that contains one observation for each REGION. */
/* Create a new variable REGTOTAL, that contains the total AMOUNT for each */
/* REGION. */
proc means data=sales noprint nway;
var amount;
by region;
output out=regtot(keep=regtotal region) sum=regtotal;
run;
/* Create PERCENT1 by merging REGTOT with SALES, based on the value of */
/* the BY variable REGION. */
data percent1;
merge sales regtot;
by region;
/* Calculate the percentage each observation contributed
to the total for the appropriate region. */
regpct = (amount / regtotal ) * 100;
format regpct 6.2 amount regtotal dollar10.;
run;
proc print data=percent1;
title1 'PERCENT1';
run;
/* Method 2 : Related Technique using PROC SQL */
proc sql;
create table percent2 as
select *, sum(amount) as regtotal format=dollar10.,
100*(amount/sum(amount)) as regpct format=6.2
from sales
group by region;
quit;
These sample files and code examples are provided by SAS Institute Inc. "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.
PERCENT1 Obs region repid amount regtotal regpct 1 EAST 1051 $2,508,000 $4,313,000 58.15 2 EAST 1055 $1,805,000 $4,313,000 41.85 3 NORTH 1001 $1,000,000 $5,800,000 17.24 4 NORTH 1002 $1,100,000 $5,800,000 18.97 5 NORTH 1003 $1,550,000 $5,800,000 26.72 6 NORTH 1008 $1,250,000 $5,800,000 21.55 7 NORTH 1005 $900,000 $5,800,000 15.52 8 SOUTH 1007 $2,105,000 $4,635,000 45.42 9 SOUTH 1010 $875,000 $4,635,000 18.88 10 SOUTH 1012 $1,655,000 $4,635,000 35.71
Type: | Sample |
Topic: | SAS Reference ==> Procedures ==> SQL SAS Reference ==> DATA Step Data Management ==> Manipulation and Transformation ==> BY-group processing Common Programming Tasks ==> Combining Data Data Management ==> Manipulation and Transformation ==> Combining and Modifying Data Sets SAS Reference ==> Statements ==> File-handling ==> MERGE |
Date Modified: | 2006-01-10 03:02:51 |
Date Created: | 2005-02-17 15:43:22 |
Product Family | Product | Host | SAS Release | |
Starting | Ending | |||
SAS System | Base SAS | All | n/a | n/a |