user6242444
user6242444

Reputation: 11

sas format not always created by macro fails syntax check

I have a sas macro which if certain conditions are met creates a user defined format which is used later in the macro. However this user format is not always created. So when sas validates the syntax when the macro is called it errors as the user defined format is not known when the condition is not met. The statement to use the user defined format is wrapped in an if condition which has not been met but the macro still errors.

Any advice to overcome this problem greatly received.

Upvotes: 1

Views: 67

Answers (2)

Tom
Tom

Reputation: 51566

What do you mean by IF condition? SAS will check the syntax of a DATA step before it starts executing. So cannot prevent the reference to format in the data step by using an IF or similar execution time code. So this code will generate an error even though the condition in the IF statement can never be true.

data bad;
  if 0=1 then format x $1XYZ.;
run;

If you use a macro %IF statement so that the reference to format is never generated in the SAS code that the macro creates then you should not have any error. So if you had a similar data step being generated by a macro and used a %IF to prevent the macro from generating the invalid format name then the code will run without error.

data good;
%if (0=1) %then %do;
  format x $1XYZ.;
%end;
run;

Most likely you just want to use a macro variable to hold the format name and set it empty if the format is not created.

data good;
   format x &format_name ;
run;

Upvotes: 1

Joe
Joe

Reputation: 63424

One good way to deal with this is to create a dummy format that doesn't actually do anything, before the conditional creation. That way, you have something to prevent the error.

%macro fizz_buzz(format=0);
*Format that does nothing;
proc format;
  value FIZZBUZZF
  other=[best.]
  ;
quit;

*Conditionally created same format;
%if &format=1 %then %do;
  proc format;
    value FIZZBUZZF
      3,6,9,12='FIZZ'
      5,10='BUZZ'
      15='FIZZBUZZ'
      other=[2.]
    ;
  quit;
%end;

data _null_;
  do _i = 1 to 15;
    put _i fizzbuzzf.;
  end;
run;
%mend fizz_buzz;

%fizz_buzz(format=0);

Upvotes: 1

Related Questions