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
data want;
set have nobs=_nobs_;
cumulative_percent=_n_/_nobs_;
format cumulative_percent percent6.0;
run;
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;
Thanks Reeza. Will this be based on observation (row) or sales? Hoping to leverage your example on observation.
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.
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;
data want;
set have nobs=_nobs_;
cumulative_percent=_n_/_nobs_;
format cumulative_percent percent6.0;
run;
Amazing!! Thank you so much. I really appreciate the prompt solution.
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.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.