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

Hello,

I want to create table, that showing the percentage of each region in the total sales.

My data: 

Pawel__0-1610573574283.png

I wrote something like this:

proc sql;
create table want8 as
Select Genre, NA_Sales/sum(EU_Sales,NA_Sales, JP_Sales, Other_Sales) as NA_Sales format=percent8.2, EU_Sales/sum(EU_Sales,NA_Sales, JP_Sales, Other_Sales)
as EU_Sales format=percent8.2, JP_Sales/sum(EU_Sales,NA_Sales, JP_Sales, Other_Sales) as JP_Sales format=percent8.2
from games order by Genre;
quit;
run;
proc print data=want8;
run;

But I get this result:

Pawel__1-1610573718029.png

I wonder why it doesn't group correctly.

 

1 ACCEPTED SOLUTION

Accepted Solutions
Angel_Larrion
SAS Employee

instead of

 

 

sum(EU_Sales,NA_Sales, JP_Sales, Other_Sales)

 

 

you can put

 

 

sum(EU_Sales+NA_Sales+ JP_Sales+ Other_Sales)

 

 

 

 

proc sql;
create table want8 as
Select Genre, sum(NA_Sales)/sum(EU_Sales+NA_Sales+ JP_Sales+ Other_Sales) as NA_Sales format=percent8.2, sum(EU_Sales)/sum(EU_Sales+NA_Sales+ JP_Sales+ Other_Sales)
as EU_Sales format=percent8.2, sum(JP_Sales)/sum(EU_Sales+NA_Sales+ JP_Sales+ Other_Sales) as JP_Sales format=percent8.2
from games group by Genre order by Genre;
quit;
run;
proc print data=want8;
run;

that should work.

View solution in original post

7 REPLIES 7
Angel_Larrion
SAS Employee

With the code you showed us you are calculating the percentage of each region per game (i.e. each row in the output table is a game ).

 

What do you want to obtain? (i.e. What does a row represent in the desired output table?)

 

Pawel_
Fluorite | Level 6
I want to calculate percentage of each region per genre.
Angel_Larrion
SAS Employee

then you have to group by genre.

 

proc sql;
create table want8 as
Select Genre, sum(NA_Sales)/sum(EU_Sales,NA_Sales, JP_Sales, Other_Sales) as NA_Sales format=percent8.2, sum(EU_Sales)/sum(EU_Sales,NA_Sales, JP_Sales, Other_Sales)
as EU_Sales format=percent8.2, sum(JP_Sales)/sum(EU_Sales,NA_Sales, JP_Sales, Other_Sales) as JP_Sales format=percent8.2
from games group by Genre order by Genre;
quit;
run;
proc print data=want8;
run;

  

this code should work

Pawel_
Fluorite | Level 6

Unfortunately the result is the same.

Pawel__0-1610577048363.png

 

 

Angel_Larrion
SAS Employee

you also need to take the sum of the desired region per genre instead of just the sum of the game, that's why I put sum(region_sale) instead of region_sale.

After making this change the result is the same?

Pawel_
Fluorite | Level 6

Sorry, I didn't notice this change. However, now the result is this :

Pawel__0-1610577726510.png

 

Angel_Larrion
SAS Employee

instead of

 

 

sum(EU_Sales,NA_Sales, JP_Sales, Other_Sales)

 

 

you can put

 

 

sum(EU_Sales+NA_Sales+ JP_Sales+ Other_Sales)

 

 

 

 

proc sql;
create table want8 as
Select Genre, sum(NA_Sales)/sum(EU_Sales+NA_Sales+ JP_Sales+ Other_Sales) as NA_Sales format=percent8.2, sum(EU_Sales)/sum(EU_Sales+NA_Sales+ JP_Sales+ Other_Sales)
as EU_Sales format=percent8.2, sum(JP_Sales)/sum(EU_Sales+NA_Sales+ JP_Sales+ Other_Sales) as JP_Sales format=percent8.2
from games group by Genre order by Genre;
quit;
run;
proc print data=want8;
run;

that should work.

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
  • 7 replies
  • 1940 views
  • 2 likes
  • 2 in conversation