BookmarkSubscribeRSS Feed
ZRick
Obsidian | Level 7

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;

17 REPLIES 17
Linlin
Lapis Lazuli | Level 10

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

ZRick
Obsidian | Level 7

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.

Linlin
Lapis Lazuli | Level 10

to get color coding:

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

ZRick
Obsidian | Level 7

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

I have many variables share the same condition.

Haikuo
Onyx | Level 15

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

Ksharp
Super User

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

Ksharp

Haikuo
Onyx | Level 15

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

ZRick
Obsidian | Level 7

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

Ksharp
Super User

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

ZRick
Obsidian | Level 7

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;

Linlin
Lapis Lazuli | Level 10

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;

ZRick
Obsidian | Level 7

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?

Linlin
Lapis Lazuli | Level 10

_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.  

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
  • 17 replies
  • 6925 views
  • 6 likes
  • 5 in conversation