DATA Step, Macro, Functions and more

multiple variables use the same condition, how to write them in the same if statement?

Reply
Frequent Contributor
Posts: 133

multiple variables use the same condition, how to write them in the same if statement?

Two variables accrual_1, accrual_2 share the same condition, but I don't know how to write them into the same if statement.

data tmp;

input accrual_1 accrual_2;

cards;

1 2

12 4

3 5

;

data tmp2 tmp3;

set tmp;

if (accrual_1 or accrual_2) not in (1, 2, 3, 4, 5, 6) then output tmp3;

else output tmp2;

run;

Super Contributor
Posts: 1,636

Re: multiple variables use the same condition, how to write them in the if statement?

data tmp;

input accrual_1 accrual_2;

cards;

1 2

12 4

3 5

;

data tmp2 tmp3;

set tmp;

if accrual_1 not in (1, 2, 3, 4, 5, 6) or accrual_2 not in (1, 2, 3, 4, 5, 6) then output tmp3;

else output tmp2;

run;

/*or */

data tmp2 tmp3;

set tmp;

if accrual_1 in (1, 2, 3, 4, 5, 6) and accrual_2 in (1, 2, 3, 4, 5, 6) then output tmp2;

else output tmp3;

run;

Linlin

Frequent Contributor
Posts: 133

multiple variables use the same condition, how to write them in the same if statement?

sidebar question:

how did you get the color coding in the post?

I tried to do that in base sas and copy / paste, it does not work for me.

Super Contributor
Posts: 1,636

multiple variables use the same condition, how to write them in the same if statement?

to get color coding:

copy from SAS editor ,paste to word, copy from word, paste to here.

Frequent Contributor
Posts: 133

multiple variables use the same condition, how to write them in the same if statement?

Is there a way to group variables together and just write the condition once?

I have many variables share the same condition.

Respected Advisor
Posts: 3,124

multiple variables use the same condition, how to write them in the same if statement?

For your specific condition, you could try array():

data tmp3  tmp2 ;

set tmp;

n=0;

array acc(*) acc:;

   do _n_=1 to dim(acc);

      if acc(_n_) not in (1,2,3,4,5,6) then do;

           output tmp3;

n+1;

return;

end;

   end;

   if n=0 then output tmp2;

   drop n;

   run;

Regards,

Haikuo

Super User
Posts: 9,691

multiple variables use the same condition, how to write them in the same if statement?

I think you should add a  LEAVE statement in the do loop.

Ksharp

Respected Advisor
Posts: 3,124

multiple variables use the same condition, how to write them in the same if statement?

Thanks, Ksharp. I thought about it. I have a 'return' inside the do-loop, will that lead to 'leave' action in the context of this code?

Haikuo

Super User
Posts: 9,691

Re: multiple variables use the same condition, how to write them in the same if statement?

Opps. missing that. My bad.

Frequent Contributor
Posts: 133

multiple variables use the same condition, how to write them in the same if statement?

can you help explain "return" and "leave" inside the Do loop? appreciate it!

Super User
Posts: 9,691

multiple variables use the same condition, how to write them in the same if statement?

RETURN will return the top of data step immediately, and will not execute the following code.

LEAVE will leave the current loop and will not execute the following code in the loop, but will also execute the code after loop.

Ksharp

Frequent Contributor
Posts: 133

multiple variables use the same condition, how to write them in the same if statement?

I don't quite understand your "n"

before the loop,  n=0;

during the loop,  n+1;

and  after the loop: if n=0 then output tmp2;

Super Contributor
Posts: 1,636

multiple variables use the same condition, how to write them in the same if statement?

using array maybe helpful:

data tmp;

input accrual_1 accrual_2 accrual_3 accrual_4;

cards;

1 2 1 2

12 4 12 4

3 5 3 5

;

data tmp2 tmp3;

set tmp;

array acc(*) accrual_1 accrual_2 accrual_3 accrual_4;

count=0;

do _n_=1 to dim(acc);

if acc(_n_) in (1,2,3,4,5,6) then count+1;

end;

/* count is the number of variables that have same condition */

if count=4 then output tmp2;

else output tmp3;

run;

Frequent Contributor
Posts: 133

multiple variables use the same condition, how to write them in the same if statement?

I think _n_ is the count of the variable, right?

Because it is set from 1 to dim(acc).

I think dim(acc) means the column of the table, not the row,

Am I right?

Super Contributor
Posts: 1,636

Re: multiple variables use the same condition, how to write them in the same if statement?

_n_ is the index-variable of the do loop.  it is the same as "i" in "do i=1 to 20". the advantage to use "_n_ " is that "_n_" will be dropped automatically from the output datasets. if you replace "_n_" with "i" in my example then "i" will be in datasets tmp2 and tmp3 as a variable.

dim(acc) is the number of elements in array "acc". in my example dim(acc)=4 because they are four elements (accrual_1 accrual_2 accrual_3 accrual_4) in the array.  

Ask a Question
Discussion stats
  • 17 replies
  • 1096 views
  • 6 likes
  • 5 in conversation