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;

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 3 replies
  • 598 views
  • 2 likes
  • 4 in conversation