## cumulative percent

Solved
Occasional Contributor
Posts: 7

# cumulative percent

Hello,

I have a dataset in which i'd like to create a variable which represents cumulative percent of observations. Can you please let me know whether this is possible?

For example:

Obs Company Sales Cumulative_Percent

1      Abc          350                   0.33

2      xyz           500                   0.66

3      sdt            450                   1.00

Accepted Solutions
Solution
‎04-04-2015 09:21 PM
Posts: 1,270

## Re: cumulative percent

data want;

set have nobs=_nobs_;

cumulative_percent=_n_/_nobs_;

format cumulative_percent percent6.0;

run;

All Replies
Super User
Posts: 23,778

## Re: cumulative percent

Possible in a variety of ways, one of the simplest is PROC FREQ

proc freq data=have;

table company/out=want outcum;

weight sales;

run;

proc print data=have;

run;

Occasional Contributor
Posts: 7

## Re: cumulative percent

Thanks Reeza. Will this be based on observation (row) or sales? Hoping to leverage your example on observation.

Occasional Contributor
Posts: 7

## Re: cumulative percent

Thanks Reeza. Will this calc the cumulative percent by observation (row)? just want to make sure it's not based on sales. For example, if there are 3 rows, each row will be divided by 3. 1/3, 2/3, 3/3.

SAS Super FREQ
Posts: 9,373

## Re: cumulative percent

Hi, investigate PROC FREQ. It can give you Frequency Counts, Cum Frequency, Percent and Cum Percent. In addition, if you want it (and you are running SAS 9.3 or higher), you can get automatic graphs from PROC FREQ, too. You should be able to test this code, since SASHELP.SHOES is generally available on all installations of SAS.

Cynthia

ods graphics on;

proc freq data=sashelp.shoes;

title '1) taking all defaults';

tables region;

run;

proc freq data=sashelp.shoes;

tables region / plots=all;

run;

proc freq data=sashelp.shoes;

tables region / nocum;

run;

proc freq data=sashelp.shoes;

title '4) using nopercent';

tables region / nopercent;

run;

ods output onewayfreqs=work.outfrq;

proc freq data=sashelp.shoes;

title '5) making dataset -- output same as #1';

tables region ;

run;

proc print data=work.outfrq noobs;

title '6) print only desired variables';

var region frequency cumpercent;

run;

Solution
‎04-04-2015 09:21 PM
Posts: 1,270

## Re: cumulative percent

data want;

set have nobs=_nobs_;

cumulative_percent=_n_/_nobs_;

format cumulative_percent percent6.0;

run;

Occasional Contributor
Posts: 7

## Re: cumulative percent

Amazing!! Thank you so much. I really appreciate the prompt solution.

Super User
Posts: 23,778

## Re: cumulative percent

No, I misread your question. If you only have each company listed once you could remove the WEIGHT statement and it would calculate the row percent for you.

🔒 This topic is solved and locked.

Discussion stats
• 7 replies
• 2470 views
• 4 likes
• 4 in conversation