【问题标题】:Passing strings to C functions with SAS proc proto使用 SAS proc proto 将字符串传递给 C 函数
【发布时间】:2011-01-20 21:31:22
【问题描述】:

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

proc proto 中的嵌入式 C 编译器在 Visual Studio 中编写算法后似乎没有产生我期望的结果。我认为我已经验证过的一件事是传递给 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;

【问题讨论】:

    标签: c string function sas


    【解决方案1】:

    我会联系 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

    【讨论】:

    • 谢谢,+1。为了可移植性和代码控制,我实际上更喜欢在 SAS 中编译小函数,而不是依赖 DLL。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-09
    • 1970-01-01
    • 2019-09-28
    • 2013-05-21
    • 1970-01-01
    • 1970-01-01
    • 2018-03-26
    相关资源
    最近更新 更多