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;
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
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.
to get color coding:
copy from SAS editor ,paste to word, copy from word, paste to here.
Is there a way to group variables together and just write the condition once?
I have many variables share the same condition.
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
I think you should add a LEAVE statement in the do loop.
Ksharp
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
Opps. missing that. My bad.
can you help explain "return" and "leave" inside the Do loop? appreciate it!
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
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;
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;
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?
_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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.