Thursday, March 27, 2014

How to leverage user-defined SAS functions

Target:
Use function for input-output programming:
1.  input(varString $, varNum)  lengthOfReturnedString $;
2.  input(varNum, varNum,...) lengthOfReturnedNum ;
3.  input(varString $ , varString $, ...)  lengthOfReturnedString $;

Solution:
1.
proc fcmp outlib=sasuser.funcs.test;
function inOut(number, str $)  $ 500;
if number > 5 then
strOut = cats(str, "the input greater than 5");
else
strOut = cats(str, "the input less than 5");
return(strOut);
endsub;

options cmplib=sasuser.funcs;
data _null_;
number = 50;
str = "Here is the result: ";
strOut = inOut(number, str);
put strOut= ;
run;

2.

proc fcmp outlib=sasuser.funcs.trial;
function sum_two_input_values(in1, in2);
n = in1 + in2;
return(n);
endsub;

data _null_;
in1 = 2;
in2 = 3;
nm = sum_two_input_values(in1, in2);
put nm=;
run;


3.

/* function 2: string = conStr(string1, string2) */
proc fcmp outlib=sasuser.funcs.trial;
* input is character, output is character with length;
function conStr(str1 $ , str2 $) $ 20;
length str1 $ 10 str2 $ 10 outStr $ 20;
outStr = cats(str1, str2);
return(outStr);
endsub;
run;
options cmplib=sasuser.funcs;
data _null_;
in1 = 'd2';
in2 = 'k3';
kk = conStr(in1, in2);
put kk=;
run;

More information:
http://support.sas.com/resources/papers/proceedings11/083-2011.pdf

No comments:

Post a Comment