Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

07-12-2017 04:06 AM

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!

Accepted Solutions

Solution

07-18-2017
10:37 PM

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to jazhang

07-12-2017 04:29 AM

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;
```

All Replies

Solution

07-18-2017
10:37 PM

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to jazhang

07-12-2017 04:29 AM

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;
```

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to jazhang

07-12-2017 05:41 AM

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.