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'm new to the forum.

I want to create a macro, that for the following data 

Pawel__0-1609857256873.png

 

will display all games for a specific year and specific platform and sum up sales.

My idea was this, but unfortunately it doesn't work.

%MACRO sorting(Year, Platform);
proc sql;
create table want4 as
select Name,Platform, Year, sum(NA_Sales, JP_Sales, Other_Sales) as suma
from games
group by &Year, &Platform
order by suma desc;
quit;
run;
%mend;
%sorting(2010,"PS3")

Is it possible for the result to be a table?

I suppose I'm passing the arguments incorrectly.

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Shmuel
Garnet | Level 18

You are asking to extract and sum "games for a specific year and specific platform and sum up sales".

To extract specific info use where. Your macro should look like:

%MACRO sorting(Year, Platform);
proc sql;
create table want4 as
select Name,Platform, Year, 
       sum(NA_Sales, JP_Sales, Other_Sales) as suma
from games (where=(Year=&year and Platform=&palatform))
group by Year, Platform /* probably not needed as one group selected */
order by calculated suma;  /* desc ?? you did not select this variable */
quit;
run;
%mend;
%sorting(2010,"PS3")

 

View solution in original post

7 REPLIES 7
PaigeMiller
Diamond | Level 26

Why do you need a macro at all?

 

PROC SUMMARY or PROC REPORT or PROC TABULATE or PROC SQL will do this without macros.

--
Paige Miller
Pawel_
Fluorite | Level 6
I know, but I have to use macro.
Tom
Super User Tom
Super User

@Pawel_ wrote:
I know, but I have to use macro.

????

PaigeMiller
Diamond | Level 26

Default macro, gets the answers while using a macro (pointlessly, but a macro is used)

 

%MACRO sorting;
proc sql;
create table want4 as
select Name,Platform, Year, sum(NA_Sales, JP_Sales, Other_Sales) as suma
from games
group by Year, Platform
order by suma desc;
quit;
run;
%mend;
%sorting

Didn't someone explain what the macro is supposed to do? Macros make code dynamic, what part of the above should be dynamic?

--
Paige Miller
Tom
Super User Tom
Super User

Turn on the MPRINT option before calling your macro so that you can see what SAS code it is generating.

Currently it is generating this code, which does not make any sense.

create table want4 as
select Name,Platform, Year, sum(NA_Sales, JP_Sales, Other_Sales) as suma
from games
group by 2010, "PS3"
order by suma desc;

If you want to create the new variable SUMA then just use a data step (you could use PROC SQL also but why?).

data want4 ;
  set games;
  suma = sum(NA_Sales, JP_Sales, Other_Sales) ;
run;
mkeintz
PROC Star

@Pawel_ wrote (emphasis mine)::

...

will display all games for a specific year and specific platform and sum up sales.

 


If by "specific" you mean a single user-specified year and platform, then you would need a "where" clause, not a "group by".

 

And I don't know what you mean by "Is it possible for the result to be a table?"

 

And finally, I reply with the most common observation on this forum.  Allow me to be recursive:

 

A problem description that just says "it doesn't work" doesn't work.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Shmuel
Garnet | Level 18

You are asking to extract and sum "games for a specific year and specific platform and sum up sales".

To extract specific info use where. Your macro should look like:

%MACRO sorting(Year, Platform);
proc sql;
create table want4 as
select Name,Platform, Year, 
       sum(NA_Sales, JP_Sales, Other_Sales) as suma
from games (where=(Year=&year and Platform=&palatform))
group by Year, Platform /* probably not needed as one group selected */
order by calculated suma;  /* desc ?? you did not select this variable */
quit;
run;
%mend;
%sorting(2010,"PS3")

 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 762 views
  • 1 like
  • 5 in conversation