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

时间:2010-02-25 07:12:03

标签: c string function sas

我一直在用一些C函数扩展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;

使用

测试funcs
/* 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;

1 个答案:

答案 0 :(得分:3)

我会联系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语言编写函数,PROC,格式,信息和引擎,以及可以从SAS使用的其他语言。这是一个包含有关SAS / TOOLKIT的信息的页面:

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

相关问题