包含引号和逗号的 SAS 宏变量的长度

Length of a SAS macro variable that contains quotes and commas

设样本宏变量为

%let temp="A","B","C";

如何获取这个宏变量的数组长度,包括引号和逗号?例如,我想要 return 长度 3.

%let length_of_temp=%sysfunc(SOME_FUNC(&temp.));
%put length=length_of_temp;

LOG: length=3

我最好使用一个已建立的 SAS 函数或代码行来完成它,而不是创建一个新的处理函数。到目前为止,这是我尝试过的:

NOTE: Line generated by the macro variable "TEMP". 4
""A","B","C"

NOTE 49-169: The meaning of an identifier after a quoted string might change in a future SAS release. Inserting white space between a quoted string and the succeeding identifier is recommended.

ERROR 388-185: Expecting an arithmetic operator.

这对我有用:

%let temp="A","B","C";

%let count = %sysfunc(countw("&temp", ","));
%put Number of elements = &count.;

结果:

8002  %put Number of elements = &count.;
Number of elements = 3

此处的 %quote 函数可能有助于屏蔽引号:

%let count = %sysfunc(countw(%quote(&temp), ","));
%put Number of elements = &count.;

在您的宏变量值上使用宏引号,这样逗号就不会在调用 countw() 函数时造成麻烦。使用 q 和可能的 m 可选的第三个参数到 countw() 函数,让它知道不要计算引号内的分隔符。

%let temp="A1,a2","B","C";
%let count = %sysfunc(countw(%superq(temp),%str(,),mq));

如果您想在数据步骤中计算计数,那么您可以使用 symget() 函数来检索宏变量的值,而不是宏引用。

count = countw(symget('temp'),',','mq');