c - 使用 SAS proc proto 将字符串传递给 C 函数

标签 c string function sas

我一直在尝试用一些 C 函数(例如最长公共(public)子串算法)扩展 SAS 字符串处理。 proc FCMP 函数很容易变得非常低效。

在 Visual Studio 中编写算法后,proc proto 中的嵌入式 C 编译器似乎没有产生我期望的结果。我认为我已经验证的一件事是传递给 C 函数的字符串似乎被空格填充到大约 100 个字符的长度。

在我继续编写更多代码来推断字符串应该结束的位置之前,我想知道是否有人知道替代方法或者可以大体上分享有关为 SAS 编写 C 函数的想法?

这里有一些代码作为例子

/* C functions*/
proc proto package=sasuser.funcs.sfuncs;
    /* A string length function */
    int cslen(const char *s);
    externc cslen;
    int cslen(const char *s)
    {
        int i=0;
        while (s[i++]!=0){}
        return i-1;
    }
    externcend;
    /* A char function */
    int cschar(const char *s,const int pos);
    externc cschar;
    int cschar(const char *s,const int pos)
    {
        return s[pos];
    }
    externcend;
run;
option cmplib=sasuser.funcs;
/* SAS wrappers */
proc fcmp outlib=sasuser.funcs.sfuncs;
    function slen(s $);
        val=cslen(s);
        return(val);
    endsub;
    function schar(s $,pos);
        val=cschar(s,pos);
        return(val);
    endsub;
quit;

测试函数
/* Tests */
data _null_;
    length str $6.;
    str="foobar";
    len=slen(str);
    firstchar=schar(str,0);
    lastchar=schar(str,5);
    shouldbenull=schar(str,6);
    put _all_;
run;

给予

str=foobar len=91 firstchar=102 lastchar=114 shouldbenull=32 _ERROR_=0 _N_=1

编辑:我们会,事实证明您可以通过简单地修剪包装器中的字符串来解决这个问题,例如:

proc fcmp outlib=sasuser.funcs.sfuncs;
    function slen(s $);
        val=cslen(trim(s));
        return(val);
    endsub;
quit;

最佳答案

我会联系 SAS 技术支持 (support@sas.com) 以获取有关 PROC PROTO 以及 SAS 如何将字符串传递到 C 例程中的帮助。

还有其他方法可以访问用 C 编写的例程。一种是使用 CALL MODULE、MODULEN 函数或 MODULEC 函数。这些例程能够调用存储在 .dll(或 Unix 上的 .so)中的函数。 Windows CALL MODULE 文档的链接位于此处:

http://support.sas.com/documentation/cdl/en/hostwin/61924/HTML/default/win-func-module.htm

此处是 UNIX CALL MODULE 文档的链接:

http://support.sas.com/documentation/cdl/en/hostunx/61879/HTML/default/unx-func-module.htm

另一种选择是许可 SAS/TOOLKIT。 SAS/TOOLKIT 允许您使用 C 语言和其他语言编写可从 SAS 使用的函数、PROC、格式、信息格式和引擎。这是包含有关 SAS/TOOLKIT 的信息的页面:

http://www.sas.com/products/toolkit/index.html

关于c - 使用 SAS proc proto 将字符串传递给 C 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2332289/

相关文章:

c - 快速提问

java - 根据字符数向字符串添加新行

c - 为什么在 c 中打印字符串数组不提供相同的输出?

vb.net - 大括号中的数字有什么作用,例如 "{0}"是什么意思?

function - Common Lisp - 使用一个函数作为另一个函数的输入

c++ - 对字节字符串取模

c - 如何在 C 中的 bool 表达式中计算不同长度的数组?

c++ - 带有类模板 typedef 参数的函数模板

c - Gtk,模态对话框

android - 我如何使用带微调器的开关?