DATA Step, Macro, Functions and more

call routine vs function

Accepted Solution Solved
Reply
Super Contributor
Posts: 271
Accepted Solution

call routine vs function

Under what situations CALL ROUTINE is used over SAS functions such as CALL SCAN vs. SCAN?


Accepted Solutions
Solution
‎03-21-2016 10:50 AM
Super User
Posts: 5,509

Re: call routine vs function

Posted in reply to SAS_inquisitive

My general rule of thumb is to use the function when possible.  The CALL routines can do more, but the functions can do the most commonly required tasks.  So switch to the CALL routine when you need something a little more complex than what the function can do.  By using the functions when possible, you can use a simpler program at the cost of having slightly less functionality available.

View solution in original post


All Replies
Super User
Super User
Posts: 7,964

Re: call routine vs function

Posted in reply to SAS_inquisitive

From the documentation:

http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a001131396.htm

 

To try and put it another way, call <function> does something, but doesn't necessarily assign a result.  Lok at the examples in the documentation:

http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a002255934.htm

You will see that call scan is executed, but is not assigned to anything e.g.:

abc=scan(...);

call scan(...);

 

The first example assigns the result to abc, the second just executes the function, no return.

Solution
‎03-21-2016 10:50 AM
Super User
Posts: 5,509

Re: call routine vs function

Posted in reply to SAS_inquisitive

My general rule of thumb is to use the function when possible.  The CALL routines can do more, but the functions can do the most commonly required tasks.  So switch to the CALL routine when you need something a little more complex than what the function can do.  By using the functions when possible, you can use a simpler program at the cost of having slightly less functionality available.

Super Contributor
Posts: 308

Re: call routine vs function

Posted in reply to SAS_inquisitive

Hello,

 

I pretty much use call missing function when I have to set to missing both character and numeric variables.

 

For example when I use hash and have previously loaded variables using set, I find useful call missing rather then missing.

 

Consider the following example:

 

data want;
if 0 then set have hash1;

if _N_=1 then
    do;
        declare hash a(dataset: "hash1");
        a.definekey('key');
        a.definedata('num1', 'num2', 'num3', 'char1', 'char2', 'char3');
        a.definedone();
    end;

set have;

if a.find() ne 0 then call missing(num1, num2, num3, char1, char2, char3);


run;

 

Super Contributor
Posts: 271

Re: call routine vs function

Thank you, all.

Super Contributor
Posts: 271

Re: call routine vs function

Posted in reply to SAS_inquisitive

I was looking at this example in SAS doc. So CALL SCAN is able to create variables from its arguments (position, length) without specifying those variables explicitly?

 

data artists;
   input string $60.;
   drop string;
   do i=1 to 99;
      call scan(string, i, position, length);
      if not position then leave;
      Name=substrn(string, position, length);
      output;
   end;
   datalines;
Picasso Toulouse-Lautrec Turner "Van Gogh" Velazquez
;

proc print data=artists;
run;

 

 

Super User
Super User
Posts: 7,050

Re: call routine vs function

Posted in reply to SAS_inquisitive

SAS always creates new variables when you reference a name that it has not seen before.  That has nothing to do with CALL SCAN().

Super Contributor
Posts: 271

Re: call routine vs function

I looked closely at the arugments of the CALL SCAN. It makes sense now.  

Thanks !

Respected Advisor
Posts: 3,799

Re: call routine vs function

[ Edited ]
Posted in reply to SAS_inquisitive

This example may further your understanding.  It looks for a string that is all uppercase and 2 bytes long.  Using call scan I find the position and length of each uppercase string.  if length eq 2.  substring the target from p for l.  Using SCAN function you would have to measure the result returned from SCAN.  Same but different.

 

data _2upcase;
   input string $50.;
   do c=1 by 1 until(l eq 2 or p eq 0);
      call scan(strip(string),c,p,l,,'ldsp');
      end;
   length _2upcase $2;
   _2upcase = substrn(string,p,l);
   cards;
nndkd11UUndkdLLL
kdnakaliueoina
nnnlllLLLlllLLlll
thisISa2DIgit
this IS silly
this.IS.silly
;;;;
   run;

I reckon this could be replaced with a one line RegEX but that's another topic.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 8 replies
  • 488 views
  • 5 likes
  • 6 in conversation