Search code examples
sassas-macro

Trying to create a macro with If-Then conditions in SAS


Here is the code I'm using to creat a format.....

    libname myfmt "&FBRMrootPath./Formats";
%macro Create_Macro(DSN,Label,Start,fmtname,type);
options mprint mlogic symbolgen;
%If &type='n' %then %do;
  proc sort data=&DSN out=Out; by &Label;run;
  Data ctrl;
     set Out(rename=(&Label=label &Start=start )) end=last;
     retain fmtname &fmtname type &type;

  %If last %then %do;
      hlo='O';
      label='*ERROR';
      output;
  %End;
%End;

%Else  %do;
     proc sort data=&DSN out=Out; by &Start;run;
    Data ctrl;
     set Out(rename=(&Start=label &Label=start )) end=last;
     retain fmtname &fmtname type &type;
     output;
    %If last %then %do;
      hlo='O';
      label='*ERROR';
      output;
      %End;
%End;

proc format library=myfmt cntlin=ctrl;

%Mend Create_Macro;

%Create_Macro(SSIN.prd,prd_nm,prd_id,'prd_test','n');
/*%Create_Macro(SSIN.prd,prd_id,prd_nm,'prd_testc','c');*/

I'm getting following errors...Code looks good but I donno why I'm getting errors... Any help???


Solution

  • Not entirely sure what you are doing, but the error message is probably because you are mixing macro code with data step code. Trying change to this:

    if last then do;
       hlo='O';
       label='*ERROR';
       output;
    end;
    

    In other words, get rid of the ampersands (which indicate macro variable references). And also be sure to add a run; statement at the end of each data step and after the PROC FORMAT call.