运行 仅当用户名与列表匹配时才使用宏
Run a macro only if the username matches the list
我正在尝试 运行 基于用户名条件的宏,我应该在哪里进行更改:
例如:
我有以下带有用户名的数据集:
data users2;
input name$;
cards;
ABC
DEF
YUT
GTR
;
run;
我有一个要调用的宏:%callmacro;
proc sql;
select name into: usernames separated by ',' from users2;
quit;
所以我调用宏
%macro NEW();
%if &sysuserid in (&usernames) %then %do;
%callmacro;
%end;
%mend;
%new;
所以这里我得到一个错误:
ERROR: Required operator not found in expression: ( "&sysuserid" in
(&usernames))
我想 运行 仅当列表中的用户名匹配时才使用宏。否则有什么方法可以从 SAS 宏调用 WINDOWS AD 组并检查该 Windows AD 组中是否存在 sysuserid?
如果没有特殊选项,您不能在 %if
子句中使用 in
语句。有两种方法可以解决问题。
1. /minoperator
选项(您需要从 ','
更改 &usernames
中的分隔符至 ' '
. ):
首先,您需要更改 usernames
宏变量中的分隔符并使用 strip
函数:
proc sql;
select strip(name) into: usernames separated by ' ' from users2;
quit;
然后,您的代码带有选项 /minoperator
。
%macro new() /minoperator;
%if &sysuserid in (&usernames) %then %do;
%callmacro;
%end;
%mend;
%new;
2。另一种解决方案是使用 loop by scan
function(无需更改分隔符):
%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;
您可以检查宏中的用户名
%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;
我正在尝试 运行 基于用户名条件的宏,我应该在哪里进行更改:
例如: 我有以下带有用户名的数据集:
data users2;
input name$;
cards;
ABC
DEF
YUT
GTR
;
run;
我有一个要调用的宏:%callmacro;
proc sql;
select name into: usernames separated by ',' from users2;
quit;
所以我调用宏
%macro NEW();
%if &sysuserid in (&usernames) %then %do;
%callmacro;
%end;
%mend;
%new;
所以这里我得到一个错误:
ERROR: Required operator not found in expression: ( "&sysuserid" in
(&usernames))
我想 运行 仅当列表中的用户名匹配时才使用宏。否则有什么方法可以从 SAS 宏调用 WINDOWS AD 组并检查该 Windows AD 组中是否存在 sysuserid?
如果没有特殊选项,您不能在 %if
子句中使用 in
语句。有两种方法可以解决问题。
1. /minoperator
选项(您需要从 ','
更改 &usernames
中的分隔符至 ' '
. ):
首先,您需要更改 usernames
宏变量中的分隔符并使用 strip
函数:
proc sql;
select strip(name) into: usernames separated by ' ' from users2;
quit;
然后,您的代码带有选项 /minoperator
。
%macro new() /minoperator;
%if &sysuserid in (&usernames) %then %do;
%callmacro;
%end;
%mend;
%new;
2。另一种解决方案是使用 loop by scan
function(无需更改分隔符):
%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 andselect into
. My advise is to change it:proc sql; select strip(name) into: usernames separated by ',' from users2; quit;
您可以检查宏中的用户名
%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;