Anand
Anand

Reputation: 31

Run a macro only if the username matches the list

I am trying to run a macro based on a condition of usernames, where do I make the changes:

for ex: I have the following dataset with usernames:

data users2; 
input name$; 
    cards; 
    ABC
    DEF
    YUT
    GTR
    ; 
run;

I have a macro to call: %callmacro;

proc sql; 
select name into: usernames separated by ',' from users2; 
quit;

so I call the macro

%macro NEW(); 
%if &sysuserid in (&usernames) %then %do;
%callmacro;
%end;
%mend;


%new;

So here I get an error :

ERROR: Required operator not found in expression: ( "&sysuserid" in 
(&usernames))

I would like to run a macro only if the username matches in the list. Else is there any way I can call a WINDOWS AD group from SAS macro and check if the sysuserid exixts in that Windows AD group?

Upvotes: 0

Views: 397

Answers (2)

Llex
Llex

Reputation: 1770

You can't use in statement in %if clause without special option. There are two ways to solve the problem.

1. /minoperator option(you need change delimeter in &usernames from ',' to ' '. ):

Firstly, you need to change delimeter in usernames macro variable and use strip function:

proc sql; 
select strip(name) into: usernames separated by ' ' from users2; 
quit;

Then, your code with option /minoperator.

%macro new() /minoperator; 
   %if &sysuserid in (&usernames) %then %do;
      %callmacro;
   %end;
%mend;

%new;

2. The other solution is to use loop by scan function(no need in changing delimeter):

%macro new();
   %do i = 1 %to %sysfunc(countw(%bquote(&usernames),%str(%,)));
      %if %sysfunc(strip(%str(&sysuserid)))=%sysfunc(strip(%scan(%bquote(&usernames),&i,%str(%,)))) %then %do;
         %callmacro;
      %end;
   %end;
%mend new;


%new();

Don't forget to use strip function, when you compare character variables and select into. My advise is to change it:

proc sql; 
select strip(name) into: usernames separated by ',' from users2; 
quit;

Upvotes: 0

Richard
Richard

Reputation: 27498

You could check the usernames inside the macro

%macro ThisIsConditionallyRestricted(nametable=users2);
  proc sql noprint;
    select name from &nametable where name = "&sysuserid";
  quit;
  %if &SQLOBS = 0 %then %do;
    %put WARNING: You were not prepared!;
    %return;
  %end;
  … 
%mend;

%ThisIsConditionallyRestricted;

Upvotes: 1

Related Questions