SAS函数findc的空字符参数

huangapple go评论72阅读模式
英文:

SAS function findc with null character argument

问题

非常偶然地,我写了一个findc()函数并提交了程序。

data test;
  x=findc(,'abcde');
run;

我查看了结果,一切都正常。当我浏览代码时,我注意到findc()函数缺少了第一个字符参数。我立刻感到惊讶,这样的代码居然能够工作。

我查看了帮助文档:

FINDC函数允许字符参数为空。空参数被视为长度为零的字符字符串。数值参数不能为null。

这个功能是设计用于什么的?容错性或者还有其他用途吗?感谢任何提示。
PS:我发现findw()也具有相同的行为,但find()不具备。

英文:

Very incidentally, I wrote a findc() function and I submitted the program.

data test;
  x=findc(,'abcde');
run;

I looked at the result and nothing is unnormal. As I glanced over the code, I noticed the findc() function missed the first character argument. I was immediately amazed that such code would work.
I checked the help documentation:

> The FINDC function allows character arguments to be null. Null arguments are treated as character strings that have a length of zero. Numeric arguments cannot be null.

What is this feature designed for? Fault tolerance or something more? Thanks for any hint.
PS: I find findw() has the same behavior but find() not.

答案1

得分: 0

我怀疑允许参数根本不出现只是允许传递给它的字符串长度为零的一个副产品。

通常在SAS中,字符串是固定长度的。因此,不存在空字符串,只有用空格填充的字符串。如果你在一个只包含空格的字符串上使用TRIM()函数,结果将是一个包含一个空格的字符串。

但当引入TRIMN()和其他函数,如FINDC()和FINDW()时,它们开始允许函数的参数为空字符串(如果你想将结果存储到变量中,它仍然是固定长度的)。但他们没有修改现有函数如INDEX()或FIND()的行为。

对于FINDC()函数,在使用TRIMN()函数或strip修饰符时,你可能希望具有这种功能。

示例用例可能是在字符串中查找第一个空格,同时忽略用于填充固定长度变量的空格。

space = findc(trimn(string), ' ');
英文:

I suspect that allowing the argument to be not present at all is just an artifact of allowing the strings passed to it to be of zero length.

Normally in SAS strings are fixed length. So there was no such thing as an empty string, just one that was filled with spaces. If you use the TRIM() function on a string that only has spaces the result is a string with one space.

But when they introduced the TRIMN() and other functions like FINDC() and FINDW() they started allowing arguments to functions to be empty strings (if you want to store the result into a variable it will still be fixed length). But they did not modify the behavior of the existing functions like INDEX() or FIND().

For the FINDC() function you might want this functionality when using the TRIMN() function or the strip modifier.

Example use case might be to locate the first space in a string while ignoring the spaces used to pad the fixed length variable.

space = findc(trimn(string),' ');

huangapple
  • 本文由 发表于 2023年1月9日 17:41:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75055418.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定