代码之家  ›  专栏  ›  技术社区  ›  cacti5

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

  •  1
  • cacti5  · 技术社区  · 6 年前

    让示例宏变量为

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

    如何获取这个宏变量的数组长度,其中包括引号和逗号?例如,我想返回长度3。

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

    我最好使用一个已建立的SAS函数或一行代码来完成,而不是创建新的处理函数。以下是我迄今为止所做的尝试:

    • countw("&temp.",",") :尝试将其转换为字符串时,引号会产生错误。

    注:由宏变量“TEMP”生成的行。4.
    “A”、“B”、“C”

    注49-169:带引号字符串后的标识符含义可能在未来的SAS中发生变化 释放在带引号的字符串和后续字符串之间插入空格 建议使用标识符。

    错误388-185:应为算术运算符。

    • countw(&temp.,",") count(&temp.) :函数调用的典型错误有太多参数
    • count((&temp.)) dim((&temp.))
    • 变体使用 %superq 在上面
    3 回复  |  直到 6 年前
        1
  •  4
  •   Tom    6 年前

    在宏变量值上使用宏引号,以便逗号不会给调用 countw() 作用使用 q 可能还有 m 的可选第三个参数 countw() 函数使其知道不计算引号内的分隔符。

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

    如果要计算数据步骤中的计数,则可以使用 symget() 函数检索宏变量的值。

    count = countw(symget('temp'),',','mq');
    
        2
  •  0
  •   Reeza    6 年前

    这对我很有用:

    %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
    
        3
  •  0
  •   Don Hopkins    6 年前

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

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