BookmarkSubscribeRSS Feed
WilliamB
Obsidian | Level 7

Hello, I would like with a code, create percentages by categories and Name.
You can help me?
Thank you very much.

 

 

 

Table    Want   
         
NameProduitNumber  NameProduitNumberPercent
TitiA2  TitiA29,09%
TitiB5  TitiB522,73%
TitiC7  TitiC731,82%
TitiD8  TitiD836,36%
RemA6  RemA633,33%
RemB4  RemB422,22%
RemC6  RemC633,33%
RemD2  RemD211,11%
ErzA0  ErzA00,00%
ErzB9  ErzB952,94%
ErzC5  ErzC529,41%
ErzD3  ErzD317,65%
3 REPLIES 3
PeterClemmensen
Tourmaline | Level 20

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;

 

Kurt_Bremser
Super User

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;
gamotte
Rhodochrosite | Level 12

And another way with proc freq.

 

proc freq data=have;
    by Name notsorted;
    table produit / out=want;
    weight Number;
run;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

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!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1066 views
  • 2 likes
  • 4 in conversation