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

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 1114 views
  • 2 likes
  • 4 in conversation