Alternative for writing out multiple or conditions

Solved
Frequent Contributor
Posts: 131

Alternative for writing out multiple or conditions

What's the most efficient way to create a binary variable if the variable is defined by a very long list of or conditions (i.e.   'if...or....or....then analysisvariable=1; else analysisvariable=0?

I have the code below but it doesn't create the variable correctly:

array enrichment{*} date01-date99 date100-date201;

do i=1 to dim(enrichment);

if (enrichment{i}>=T1 and enrichment{i}<=T2) then anal_t1_enrich=1;

else anal_t1_enrich=0; end;

.

.

.

Basically the binary variable=1 if any of the date variables in the enrichment array is within a date range specified by T1 and T2 (where T1 and T2 are also variables with MM/DD/YYYY as the response format).

The binary variable=0 if none of the date variables in the enrichment array is within the T1-T2 date range.

Thanks!!!!

Accepted Solutions
Solution
‎03-22-2014 11:38 PM
Super User
Posts: 8,127

Re: Alternative for writing out multiple or conditions

You do not want the ELSE clause for each of the IF conditions in your DO loop as that will just set it back to false again.

Set the value to false (0) before the DO loop and then set it true (1) in the loop.

If the number of elements in the loop is large you could short circuit the DO loop with a UNTIL or WHILE clause.

array enrichment date01 - date201;

anal_t1_enrich=0;

do i=1 to dim(enrichment) until (anal_t1_enrich);

if ( T1 <= enrichment(i) <= T2 ) then anal_t1_enrich=1;

end;

All Replies
Posts: 5,541

Re: Alternative for writing out multiple or conditions

array enrichment{*} date01-date99 date100-date201;

anal_t1_enrich = 0;

do i=1 to dim(enrichment);

anal_t1_enrich = anal_t1_enrich or (enrichment{i}>=T1 and enrichment{i}<=T2);

end;

PG

PG
Frequent Contributor
Posts: 131

Re: Alternative for writing out multiple or conditions

Thank you, PG!  that was helpful!!

Solution
‎03-22-2014 11:38 PM
Super User
Posts: 8,127

Re: Alternative for writing out multiple or conditions

You do not want the ELSE clause for each of the IF conditions in your DO loop as that will just set it back to false again.

Set the value to false (0) before the DO loop and then set it true (1) in the loop.

If the number of elements in the loop is large you could short circuit the DO loop with a UNTIL or WHILE clause.

array enrichment date01 - date201;

anal_t1_enrich=0;

do i=1 to dim(enrichment) until (anal_t1_enrich);

if ( T1 <= enrichment(i) <= T2 ) then anal_t1_enrich=1;

end;

Frequent Contributor
Posts: 131

Re: Alternative for writing out multiple or conditions

Thanks so much, Tom!
Did you mean  until (analysis variable=1);?

Thank you, again!

Super User
Posts: 8,127

Re: Alternative for writing out multiple or conditions

SAS will treat any non-zero value as true.

Frequent Contributor
Posts: 131

Re: Alternative for writing out multiple or conditions

Thanks again, Tom!!

🔒 This topic is solved and locked.

Discussion stats
• 6 replies
• 294 views
• 3 likes
• 3 in conversation