BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
qsilver23
Calcite | Level 5

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

1 ACCEPTED SOLUTION

Accepted Solutions
stat_sas
Ammonite | Level 13

data want;

set have nobs=_nobs_;

cumulative_percent=_n_/_nobs_;

format cumulative_percent percent6.0;

run;

View solution in original post

7 REPLIES 7
Reeza
Super User

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;

qsilver23
Calcite | Level 5

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

qsilver23
Calcite | Level 5

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.

Cynthia_sas
SAS Super FREQ

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;

stat_sas
Ammonite | Level 13

data want;

set have nobs=_nobs_;

cumulative_percent=_n_/_nobs_;

format cumulative_percent percent6.0;

run;

qsilver23
Calcite | Level 5

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

Reeza
Super User

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.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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