BookmarkSubscribeRSS Feed
VD
Calcite | Level 5 VD
Calcite | Level 5

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.

5 REPLIES 5
Ksharp
Super User

What is your whole code and sample data ?

Haikuo
Onyx | Level 15

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

ballardw
Super User

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.

Tom
Super User Tom
Super User

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;

VD
Calcite | Level 5 VD
Calcite | Level 5

Thank you all for your replies.

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

VD.


sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 1451 views
  • 7 likes
  • 5 in conversation