Help using Base SAS procedures

cumulative percent

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 5
Accepted Solution

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
Trusted Advisor
Posts: 1,228

Re: cumulative percent

Posted in reply to qsilver23

data want;

set have nobs=_nobs_;

cumulative_percent=_n_/_nobs_;

format cumulative_percent percent6.0;

run;

View solution in original post


All Replies
Super User
Posts: 19,862

Re: cumulative percent

Posted in reply to qsilver23

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: 5

Re: cumulative percent

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

Occasional Contributor
Posts: 5

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: 8,868

Re: cumulative percent

Posted in reply to qsilver23

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;

  title '2) adding plots';

  tables region / plots=all;

run;

 

proc freq data=sashelp.shoes;

  title '3) adding nocum';

  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
Trusted Advisor
Posts: 1,228

Re: cumulative percent

Posted in reply to qsilver23

data want;

set have nobs=_nobs_;

cumulative_percent=_n_/_nobs_;

format cumulative_percent percent6.0;

run;

Occasional Contributor
Posts: 5

Re: cumulative percent

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

Super User
Posts: 19,862

Re: cumulative percent

Posted in reply to qsilver23

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.

Need further help from the community? Please ask a new question.

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