DATA Step, Macro, Functions and more

Conditional running of array

Reply
Frequent Contributor
Frequent Contributor
Posts: 94

Conditional running of array

I want to run an array only if a condition is satisfied. The code I am using looks like this:

if index(upcase(Combination), 'OPT') ne  0 then do;

          array Opt[&mMaxCount_opt] $;

          do i = 1 to &mMaxCount_opt;

               Opt=i;

         end;

end;

I get an error as the code after the if loop seems to be running even when the condition is not satisfied (in which case the value of the macro variable &mMaxCount_opt is . ).

Any solution?

Thanks.

Super User
Posts: 10,028

Re: Conditional running of array

What is your whole code and sample data ?

Respected Advisor
Posts: 3,156

Re: Conditional running of array

Along with others (such as drop, keep, rename, label, retain, length, format, informat, attrib, , by, where etc.), Array() statement is non-executable. Which means it is compile-time ONLY, which further means it can NOT be applied CONDITIONALLY. (With the exception of Macro language, but that is a different story and no contradictory to this rule).

Going back to your code, the array() statement will be effective regardless of your condition applied. In this case, you need to use Macro language to branch out SAS statements conditionally. And I suspect this would be the reason whey Ksharp needs your context to come up his Macro language code.

HTH,

Haikuo

Super User
Posts: 11,343

Re: Conditional running of array

You should include error messages with the question to provide more information.

I suspect that you are getting an error from the statement:
array Opt[&mMaxCount_opt] $;

probably about problem array declaration.

IF the array OPT was already assigned before the condition and the value of &mMaxCount_opt resolves to a numeric then you dont' need or want the array Opt[&mMaxCount_opt] $; statment.

If you want to process all element of the array OPT you don't even need the macro variable:

do i = 1 to dim(Opt); would work.

Super User
Super User
Posts: 7,050

Re: Conditional running of array

How did you create the macro variable mMaxCount_opt ?

If your data step is being generated by a macro then you can use macro logic such as:

%if (&mMaxCount_opt > 0) %then %do;

  array Opt[&mMaxCount_opt] $;

  if index(upcase(Combination), 'OPT') then do i = 1 to dim(Opt);

    Opt=i;

  end;

%end;

Frequent Contributor
Frequent Contributor
Posts: 94

Re: Conditional running of array

Thank you all for your replies.

I will use macro language to run the array conditionally, as suggested.

VD.


Ask a Question
Discussion stats
  • 5 replies
  • 378 views
  • 7 likes
  • 5 in conversation