简体   繁体   中英

SAS: input specific value for a parameter for macro program

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 !

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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM