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

发布于 2024-08-23 15:00:35 字数 1588 浏览 3 评论 0原文

我一直在尝试使用一些 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;

测试函数

/* 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;

I've been poking my nose in extending SAS string handling with some C functions such as a longest common substring algorithm. The proc FCMP functions get easily pretty inefficient.

The embedded C compiler in proc proto doesn't seem to produce the results I expect after writing the algorithm in Visual Studio. One thing I think I have verified is that the strings passed to a C function seem to be space padded to a length of approximately 100 characters.

Before I go on to write more code to deduce the place where the string should end, I'd like to know if anyone knows of alternative approaches or can in general share ideas about writing C functions for SAS?

Here's some code as an example

/* 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;

Testing the funcs with

/* 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;

gives

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

EDIT: We'll, it turns out that you can hack yourself around this by simply trimming the string in the wrappers, for example:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

零度° 2024-08-30 15:00:35

我会联系 SAS 技术支持 ([email protected]) 获取有关 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

I would contact SAS Technical Support ([email protected]) for help with PROC PROTO and how SAS passes strings into C routines.

There are other ways to access routines written in C. One is to use CALL MODULE, the MODULEN function or the MODULEC function. These routines have the ability to call functions that are stored in a .dll (or .so on Unix). A link to the Windows CALL MODULE documentation is here:

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

A link to the UNIX CALL MODULE documentation is here:

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

Another option is to license SAS/TOOLKIT. SAS/TOOLKIT allows you to write functions, PROCs, formats, informats, and engines in C, and other languages, that can be used from SAS. Here is a page that has information about SAS/TOOLKIT:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文