Alternative for writing out multiple or conditions

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 131
Accepted Solution

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=0Smiley Wink?

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
Super User
Posts: 7,060

Re: Alternative for writing out multiple or conditions

Posted in reply to Maisha_Huq

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;


View solution in original post


All Replies
Respected Advisor
Posts: 4,927

Re: Alternative for writing out multiple or conditions

Posted in reply to Maisha_Huq

Change your code to:

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
Super User
Posts: 7,060

Re: Alternative for writing out multiple or conditions

Posted in reply to Maisha_Huq

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
Super User
Posts: 7,060

Re: Alternative for writing out multiple or conditions

Posted in reply to Maisha_Huq

SAS will treat any non-zero value as true.

Frequent Contributor
Posts: 131

Re: Alternative for writing out multiple or conditions

Posted in reply to Maisha_Huq

Thanks again, Tom!!

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

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