0

I would like to create macro program for the following purpose:

I have a data set (abcsales) that have following variables: ID sales date month country, etc.

I would like to have statistics of sales variable to be shown by inputting the month and country in my macro program:

I have done the following:

%MACRO create_sales_month_country(data,month,country);

    proc univariate data=&data;
    class &month &country;
    var sales;
    run;

%MEND;

but by doing that, it will return sales by all countries with different months.

What I would like to achieve is input a specific month and a specific country then return me the statistic results of that specific country at a specific month.

I know that I could use keywords variables in my macro program for example:

%MACRO create_sales_month_country(data,month="April",country="Mexico");

but I need to change each time the value of the parameter to get another country's statistics.

I'm wondering if there is a "where statement" that I could use in the "proc univariate" that will allow me to sort the dataset with the specific month and country and then output the statistics that I would like to have for the desired country with the desired month.

I have tried the following:

%MACRO create_sales_month_country(data,month,country);

    proc univariate data=&data;
    class &month &country;
    var sales;
    where month = "&month" and country = "&country"
    run;

%MEND;

But the above code doesn't work at all.

Could you please help me to find a solution with the code which could achieve what I would like to do ?

Thank you !

2
  • Your second set of code is mostly correct, you're missing the semicolon on the WHERE statement though and you don't show what the error is. When writing macro code, first test it without a macro and then convert it to a macro. Commented Nov 26, 2020 at 18:40
  • Please post the log from calling the second code so we understand what's happening. Use the SAS debugging options options mprint symbolgen; then call the macro %create_sales_month_country(myData, 1, Canada); and post the full log to help us help you debug any issues. Commented Nov 26, 2020 at 18:41

1 Answer 1

2

First plan what code you want the macro to generate. Do you want to only select one MONTH and COUNTRY combination? So code like:

proc univariate data=my_data;
  where month = "January" and country = "United States" ;
  class month country;
  var sales;
run;

Then decide what you want to require the user to pass in. Do you want them to pass the string January or the string "January"? For the first one your macro will look like:

%macro create_sales_month_country(data,month,country);
proc univariate data=&data;
  where month = "&month" and country = "&country" ;
  class month country;
  var sales;
run;
%mend ;

And the call will look like

%create_sales_month_country(my_data,January,United States)

Note you are allowed to include the parameter names in the macro call even when the macro is defined so that you don't have to.

%create_sales_month_country(data=my_data,month=January,country=United States)

Note that will work when MONTH and COUNTRY are character strings, like in my non-macro example code. If MONTH and COUNTRY are numeric variables then do not add the quotes.

The alternative is to force the caller to pass valid SAS code for the value they want. This is useful if you want to use the IN operator to let them select multiple months or countries.

%macro create_sales_month_country(data,month,country);
proc univariate data=&data;
  where month in (&month) and country in (&country) ;
  class month country;
  var sales;
run;
%mend ;

So now the call for the example includes the quotes. Like this:

%create_sales_month_country(data=my_data,month="January",country='United States')

And if you wanted two months of data for the United States then use:

%create_sales_month_country(data=my_data,month="June" "July",country='United States')
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Tom for your detailed explanation !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.