Hello, I would like with a code, create percentages by categories and Name.
You can help me?
Thank you very much.
Table | Want | |||||||
Name | Produit | Number | Name | Produit | Number | Percent | ||
Titi | A | 2 | Titi | A | 2 | 9,09% | ||
Titi | B | 5 | Titi | B | 5 | 22,73% | ||
Titi | C | 7 | Titi | C | 7 | 31,82% | ||
Titi | D | 8 | Titi | D | 8 | 36,36% | ||
Rem | A | 6 | Rem | A | 6 | 33,33% | ||
Rem | B | 4 | Rem | B | 4 | 22,22% | ||
Rem | C | 6 | Rem | C | 6 | 33,33% | ||
Rem | D | 2 | Rem | D | 2 | 11,11% | ||
Erz | A | 0 | Erz | A | 0 | 0,00% | ||
Erz | B | 9 | Erz | B | 9 | 52,94% | ||
Erz | C | 5 | Erz | C | 5 | 29,41% | ||
Erz | D | 3 | Erz | D | 3 | 17,65% |
Here is one way
data have;
input Name $ Produit $ Number ;
datalines;
Titi A 2
Titi B 5
Titi C 7
Titi D 8
Rem A 6
Rem B 4
Rem C 6
Rem D 2
Erz A 0
Erz B 9
Erz C 5
Erz D 3
;
data want(drop=tot);
do until (last.Name);
set have;
by Name notsorted;
tot+Number;
end;
do until (last.Name);
set have;
by Name notsorted;
Percent=number/tot;
output;
end;
tot=0;
format Percent percent8.2;
run;
Another way, using SAS SQL:
proc sql;
create table want as
select
name,
produit,
number,
number / sum(number) format=percent7.2 as percent
from have
group by name;
quit;
And another way with proc freq.
proc freq data=have;
by Name notsorted;
table produit / out=want;
weight Number;
run;
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
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.
Ready to level-up your skills? Choose your own adventure.