BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
sas_newbie3
Obsidian | Level 7

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
1 ACCEPTED SOLUTION

Accepted Solutions
galtay
Obsidian | Level 7

Have you tried changing

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

to

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

?

View solution in original post

6 REPLIES 6
galtay
Obsidian | Level 7

Have you tried changing

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

to

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

?

sas_newbie3
Obsidian | Level 7

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

%test(least);

Rather than 

%test(&least);

Is it possible?

galtay
Obsidian | Level 7

dont know.  I'm probably more of a SAS newb than you 🙂  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.

sas_newbie3
Obsidian | Level 7

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. 

 

rogerjdeangelis
Barite | Level 11
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);


RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;
  

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 6 replies
  • 876 views
  • 4 likes
  • 4 in conversation