Pass a parameter in macro

Accepted Solution Solved
Reply
Contributor
Posts: 26
Accepted Solution

Pass a parameter in macro

[ Edited ]

I have a macro paragram.

%macro test(param);
    proc print data=customer;
    where customer_group="&param";
    %if &param=A members %then %do;
        title "the most abudant  group is %upcase(&param)";
    %end;
    %else %do;
        title "the least abudant group is %upcase(&param)";
    %end;
    var name gender age customer_group;
    
run;
%mend test;
options mprint mlogic;

%let least=Internet/Catalog Customers; %test(&least);

But I get an error:

MPRINT(TEST):   where customer_group="Internet/Catalog Customers";
ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand
       is required. The condition was: &param=A members

Accepted Solutions
Solution
‎02-26-2017 04:29 PM
Occasional Contributor
Posts: 17

Re: Pass a parameter in macro

Have you tried changing

%if &param=A members %then %do;

to

%if "&param"=A members %then %do;

?

View solution in original post


All Replies
Solution
‎02-26-2017 04:29 PM
Occasional Contributor
Posts: 17

Re: Pass a parameter in macro

Have you tried changing

%if &param=A members %then %do;

to

%if "&param"=A members %then %do;

?

Contributor
Posts: 26

Re: Pass a parameter in macro

Thanks. It works. I just also wonder if I pass the the variable directly. Say

%test(least);

Rather than 

%test(&least);

Is it possible?

Occasional Contributor
Posts: 17

Re: Pass a parameter in macro

dont know.  I'm probably more of a SAS newb than you Smiley Happy  I just did a little pattern recognition on your code.  In some places the variable had quotes around it and in others it didn't.  seemed reasonable to assume that was where the problem was.

Contributor
Posts: 26

Re: Pass a parameter in macro

[ Edited ]

The reason I asked is I want to reference the parameter into the tite statement.

title "the least abudant group is %upcase(&param)"

I want to replace the word "least" in the title statement with the param as they are same words. 

 

Valued Guide
Posts: 505

Re: Pass a parameter in macro

Pass a parameter in macro

When this is resolved Internet/Catalog Customers
your program tries to do a division,

I almost always double quote my macro comparison arguments.


HAVE
====

Up to 40 obs WORK.CUSTOMER total obs=5

Obs     NAME      SEX    CUSTOMER_GROUP

 1     Alfred      M     Store Customers
 2     Alice       F     Store Customers
 3     Barbara     F     Internet/Catalog Customers
 4     Carol       F     Store Customers
 5     Henry       M     Store Customers


WANT
====

the least abudant group is INTERNET/CATALOG CUSTOMERS

Obs     NAME      SEX    AGE          CUSTOMER_GROUP

 3     Barbara     F      13    Internet/Catalog Customers

BUT I GET THE ERRO


MPRINT(TEST):   where customer_group="Internet/Catalog Customers";
ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand
       is required. The condition was: &param=A members

WITH THIS CODE
==============

%macro test(param);
    proc print data=customer;
    where customer_group="&param";
    %if &param=A members %then %do;
        title "the most abudant  group is %upcase(&param)";
    %end;
    %else %do;
        title "the least abudant group is %upcase(&param)";
    %end;
    var name gender age customer_group;

run;
%mend test;
options mprint mlogic;
%let least=Internet/Catalog Customers;
%test(&least);


WORKING CODE
============

Change

   %if &param =A members %then %do;

to

  %if "&param" ="A members" %then %do;

Also she need a run;quit;

FULL SOLUTION
=============

data customer( keep=name sex age customer_group);
  set sashelp.class(obs=5);
  if name='Barbara' then customer_group ='Internet/Catalog Customers';
  else customer_group='Store Customers';
run;quit;

%macro test(param);

    proc print data=customer;
    where customer_group="&param";
    %if "&param" ="A members" %then %do;
        title "the most abudant  group is %upcase(&param)";
    %end;
    %else %do;
        title "the least abudant group is %upcase(&param)";
    %end;
    var name sex age customer_group;
    run;quit;
run;
%mend test;


options mprint mlogic;
%let least=Internet/Catalog Customers;
%test(&least);


Esteemed Advisor
Esteemed Advisor
Posts: 7,221

Re: Pass a parameter in macro

I am not sure what the value is in this whole bit of code.  The only part which actually changes is the word most or least, so why not evaluate than and drop all the rest of it?  E.g.

data _null_;
  call symputx('word',ifc("&param."="A members","most","least"));
run;

title "The &word. abundant group is %upcase(&param.)";

proc print data=customer;
  where customer_group="&param.";
  var name gender age customer_group;
run;
  
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 199 views
  • 4 likes
  • 4 in conversation