- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
data want;
set have nobs=_nobs_;
cumulative_percent=_n_/_nobs_;
format cumulative_percent percent6.0;
run;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thanks Reeza. Will this be based on observation (row) or sales? Hoping to leverage your example on observation.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
data want;
set have nobs=_nobs_;
cumulative_percent=_n_/_nobs_;
format cumulative_percent percent6.0;
run;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Amazing!! Thank you so much. I really appreciate the prompt solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.