BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
jazhang
Fluorite | Level 6

I would like to replicate following Matlab code with SAS IML:


%%% Trace function %%%
function val = mytrace(A,B)
if (nargin == 1)
   val = sum(diag(A));
elseif (nargin == 2)
   val = sum(sum(A.*B));
end
return
%%% end of mytrace.m %%%

 

I am wondering if there is any similar function in SAS which would return the number of declared inputs for the function just as the nargin(FUN) in matlab?

 

Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
IanWakeling
Barite | Level 11

I don't think there is a direct analog of 'nargin' in IML, however you could achieve the same effect by giving the input matrix B a blank default value, and then testing the type of matrix B, it will be undefined(U) if the call to the function has not provided a matrix B, or it will be numeric (N) if a value for B is given.  The syntax would be along the following lines:

  start mytrace(A, B=);
    if type(B)='U' then return(trace(A)); else etc..;
  finish;

View solution in original post

2 REPLIES 2
IanWakeling
Barite | Level 11

I don't think there is a direct analog of 'nargin' in IML, however you could achieve the same effect by giving the input matrix B a blank default value, and then testing the type of matrix B, it will be undefined(U) if the call to the function has not provided a matrix B, or it will be numeric (N) if a value for B is given.  The syntax would be along the following lines:

  start mytrace(A, B=);
    if type(B)='U' then return(trace(A)); else etc..;
  finish;
Rick_SAS
SAS Super FREQ

I agree with Ian.  You can use default parameter values. If necessary, you can use the ISSKIPPED function in a module to determine if an argument was provided or if the default value is being used.  You might enjoy reading the article "Everything you wanted to know about writing SAS/IML modules," which links to additional articles about default parameter values.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

Multiple Linear Regression in SAS

Learn how to run multiple linear regression models with and without interactions, presented by SAS user Alex Chaplin.

Find more tutorials on the SAS Users YouTube channel.

From The DO Loop
Want more? Visit our blog for more articles like these.
Discussion stats
  • 2 replies
  • 989 views
  • 5 likes
  • 3 in conversation