Link to home
Start Free TrialLog in
Avatar of labradorchik
labradorchikFlag for United States of America

asked on

How to call macro from one SAS program in another SAS program?

How can I call macro from one SAS program in another SAS program?

This is my SAS code with calling macro, which gets a warning "WARNING: Apparent symbolic reference MYMACRO not resolved."  
I thought this is a correct way to call macro. Anyone see a problem with the way I am calling my macro?

DATA testnbrs;
  INFILE 'phonenum.dat' TRUNCOVER;
    INPUT numbs $ 1-10 rcode $ 14-16;
RUN;

PROC PRINT DATA = testnbrs NOOBS;
  WHERE numbs = "&mymacro";
   TITLE "Testing ALL Phone Numbers";
RUN;

Open in new window



Below is the macro SAS code that I am trying to run:
%macro mymacro(numbs,rcode);
  if verify(trim(&numbs),"0123456789") > 0 then
    do;
      &numbs =" ";
      &rcode="1";
    end;

  else if length(trim(&numbs)) ^=10           then
    do;
      &numbs =" ";
      &rcode="1";
    end;

  else if length(substr(&numbs,1,10)) =10  then
    do;
      if verify(trim(&numbs),"0123456789") = 0 then
        do;
          &rcode="0";
        end;

if substr(&numbs,1,1) in ("0","1")        then
        do;
          &numbs =" ";
          &rcode="1";
        end;

      else if substr(&numbs,4,1) in ("0","1")   then
        do;
          &numbs =" ";
          &rcode="1";
        end;

      else if substr(&numbs,2,1)="9"            then
        do;
          &numbs =" ";
          &rcode="1";
        end;

   isame = 1;
      do i = 5 to 10;
        if substr(&numbs,4,1) not = substr(&numbs,i,1) then 
           isame = 0;
      end;

      if isame = 1 then
        do;
          &numbs =" ";
          &rcode="1";
        end;
    end;
%mend mymacro;

Open in new window


Input file phonenum.dat has the following numbers (for testing purposes):
012345678h
0123456789
9876543210
0987654321
0112345678
8876543210
SOLUTION
Avatar of Ian
Ian
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of labradorchik

ASKER

ShannonEE, thank you very much for your in-depth explanation!!
I can see where my problem was, but I am still getting the same error:

WARNING: Apparent invocation of macro NUMBERSTEST not resolved.

What I did I just run the SAS code that invokes the macro. I noticed that you run both (macro and the code which invokes macro in the same SAS program), but I need to keep them separated so this macro code can be invoked from any other programs as well. These have to be two different SAS programs. I assume that macro "numbersTest" would be a global variable and need to be available at any time I need to invoke it.  
How can I do that?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Slight typing error  (on last line)  "options" missing.

* specify FOLDER where you want to store all the macros */
/* *******    you fill in the dots *******      */
filename dateauto " ..... ";

/* make sure facility is turned on */
options mautosource;

/* access your date macros as well as SAS supplied macros */
options  sasautos=(dateauto sasautos);  

Open in new window

Worked as a charm! :) I used %include statement. Thank you very much for all your comments!!