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.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

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