-1

I have a control to verify.
I want to write a macro variable who includes an SQL proc in case the condition is verified.
Exemple:

%Macro Control1(List) / minoperator mindelimiter=' ';

%IF &Control1 in &List. %Then %do;

proc sql;

create table work.&Control1 as

Select id, count(id) as number

From data.&Produit.

group by id
having calculated number > 1
;
quit;

%end;

%mend;

%let list = &control1. &control2 &Control4 ;

%Control1(&List);

If we do the same process to control3 the proc sql doesn't run because control3 doesn't belong to the list.

I might have some mistakes in the syntax

/*CONTROL1 and PRODUIT ARE ALREADY DECLARED */

Mlk
  • 91
  • 8

1 Answers1

1

Your main problem is that you need to let SAS know that you want to use in as an operator in your macro logic.

To enable use of IN as an operator in macro language then you need set the MINOPERATOR option. To insure it always works for your macro independent of how the system option is set add it to the %macro statement.

%macro Control1(List) / minoperator mindelimiter=' ';

But you also have a number of other issues.

When defining a parameter to the macro you just want to type the name you want to parameter to have. Your example has an & before the name which would be invalid syntax and that is used to reference the value of macro variable and you cannot use the value to macro variable to name your parameter. So when defining the macro just list the name

%macro control1(list) .....

Note than when you CALL the macro is fine to reference a macro variable when passing in the parameter value to the macro, as you have done in your example call.

The %if statement has multiple problems. The first appears to just be a typo with an extra e added to the end of the list macro variable's name. The second is a logical mistake. The value the macro variable CONTROL1 is never going to be the same as that value with double quote characters around it. Also the () around the list of values for the in operator are not needed, but they won't cause any actual problems.

%if &control1 in &list %then %do;

Your SELECT statement is missing a comma between the two variables you are selecting.

select id,count(id) as number

The condition in your HAVING clause is not valid syntax. Are you trying to test if the count is larger than 1? Plus if you want to reference a variable you have calculated you need to use the CALCULATED keyword.

having calculated number > 1

Finally it is potentially confusing to reference two macro variables, CONTROL1 and PROUDUIT, in your macro that are not defined by the code (or even documented in a comment). They are not input parameters. You have not declared them as local macro variables. Or done anything to insure that they will exist.

Tom
  • 47,574
  • 2
  • 16
  • 29