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

I have a data set that I need to calculate percentages on.

 

Sample Table:

Group   -------------- Quantity -------------- CumulativeQuantity

A                                 2                                          10

A                                 3                                          10

A                                5                                           10

etc. 2000+ observations

 

Want something like this (add a percent variable):

Group   -------------- Quantity -------------- CumulativeQuantity-------- Percent

A                                 2                                          10                         20%

A                                 3                                          10                         30%

A                                5                                           10                          50%

etc./and so on for 2000+ observations

 

Can't seem to get the code correct to output percent.

 

Thank you for your help!

 

1 ACCEPTED SOLUTION

Accepted Solutions
smantha
Lapis Lazuli | Level 10
Data ds(drop=dummy);
input class1 $ val dummy;
Cards;
A 2 10
A 3 10
A 5 10
;

proc means data=ds nway noprint;
class  class1;
var val;
output out=ds_out(drop=_: ) sum(val)=/autoname;
run;

proc sql;
create table x as
select a.*,b.val_sum, a.val/b.val_sum as val_freq format=percent10.2
from ds a,
ds_out b
where a.class1=b.class1;
quit;

View solution in original post

1 REPLY 1
smantha
Lapis Lazuli | Level 10
Data ds(drop=dummy);
input class1 $ val dummy;
Cards;
A 2 10
A 3 10
A 5 10
;

proc means data=ds nway noprint;
class  class1;
var val;
output out=ds_out(drop=_: ) sum(val)=/autoname;
run;

proc sql;
create table x as
select a.*,b.val_sum, a.val/b.val_sum as val_freq format=percent10.2
from ds a,
ds_out b
where a.class1=b.class1;
quit;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 1 reply
  • 984 views
  • 0 likes
  • 2 in conversation