SAS IML pass reference to variable defined in macro

做~自己de王妃 提交于 2019-12-25 00:37:22

问题


In SAS/IML I try to pass to a user defined module a reference to a variable that is defined in macro. The module changes the variable value. Since call of the function is in the do-loop I cannot use &-sign. However use of symget does not work. Here is my code.

proc iml;
    start funcReference(argOut);

        print "funcReference " argOut;
        argOut = 5;

    finish funcReference;
    store module=funcReference;
quit;

proc IML;
    mydata1 = {1 2 3};
    call symput ('macVar', 'mydata1');

    load module=funcReference;
    run funcReference(symget('macVar'));

    print mydata1;
quit;

The output shows that variable mydata1 have not changed:

argOut 
funcReference mydata1 

mydata1 
1 2 3 

Any ideas?


SOLVED

Thanks a lot!


回答1:


You are sending in a temporary scalar matrix (the result of the SYMGET call). That temporary variable is being updated and promptly vanishes, as explained in the article "Oh, those pesky temporary variables!"

Instead of macro variables (which are text strings), you should use the VALUE and VALSET functions, as described in the article "Indirect assignment: How to create and use matrices named x1, x2,..., xn" You need to send in a real matrix in order for the values to be updated correctly, as follows:

proc IML;
load module=funcReference;
mydata1 = {1 2 3};
call symput('macVar', 'mydata1');

matrixName = symget('macVar');  /* matrix named &mydata1 */
z = value(matrixName);          /* z contains data */
run funcReference(z);           /* update values in z */
call valset(matrixName, z);     /* update data in &mydata1 */

print mydata1;


来源:https://stackoverflow.com/questions/27844612/sas-iml-pass-reference-to-variable-defined-in-macro

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!