## if with multiple conditions across observations

Solved
Frequent Contributor
Posts: 84

# if with multiple conditions across observations

[ Edited ]

Hi all,

I have an example data below. What I want is:

I have a new column named Toxicity. If HGB > 8.0 and retic > 0.08 occurs at the same time, then Toxicity = "None", otherwise Toxicity = "Yes"

I don't know how to do with multiple if conditions but different event names...any idea?

```data Lab;
input ID  Event \$ Result;
datalines;

1 ANC 3200
1 HGB 7.7
1 Retic 0.3839
1 Platelet 384
2 ANC 1500
2 HGB 9.3
2 Retic 0.1118
2 Platelet 232
3 ANC 2000
3 HGB 10.4
3 Retic 0.2987
3 Platelet 418
;
run;```

in my example, the Toxicity with "None" should be ID = 2 and ID = 3.

Thanks,

C

Accepted Solutions
Solution
‎03-21-2018 10:56 AM
Contributor
Posts: 29

## Re: if with multiple conditions

Proc transpose for HGB and Retic to determinate the status of Toxicity. Then merge back with lab data by id. Sample codes are below

``````proc transpose data=lab out=Toxicity;
by id;
where event in('HGB' 'Retic');
var result;
id event;
run;

data Toxicity;
length  Toxicity \$10;
set Toxicity;
if HGB > 8 and Retic > 0.08 then  Toxicity = "YES";
else  Toxicity = "None";
keep id Toxicity;
run;

data want;
merge lab Toxicity;
by id;
run;``````

All Replies
Super User
Posts: 24,010

Super User
Posts: 13,941

## Re: if with multiple conditions

Please show what you expect the output to look like.

Since your data set is in terms of Event And Result then it appears that you want to set toxicity for ID based on two different Events.

Are the order of HGB and Retic for Event within ID always the same with Event=HGB always followed by Event=Retic?

Frequent Contributor
Posts: 84

## Re: if with multiple conditions

my expected output should be:

 ID Event Result Toxicity 1 ANC 3200 None 1 HGB 7.7 None 1 Retic 0.3839 None 1 Platelet 384 None 2 ANC 1500 Yes 2 HGB 9.3 Yes 2 Retic 0.1118 Yes 2 Platelet 232 Yes 3 ANC 2000 Yes 3 HGB 10.4 Yes 3 Retic 0.2987 Yes 3 Platelet 418 Yes

Yes, my conditions should be with two different events (HGB and Retic) and the order is not always HGB then Retic, it maybe orderless.

Thanks,

C

Super Contributor
Posts: 359

## Re: if with multiple conditions

Hello,

``````proc sort data=Lab;
by ID;
run;

data want;
set Lab;
by ID;
retain chk;

format Toxicity \$4.;

keep ID Toxicity;

if first.ID then chk=0;

if (Event="HGB" and result>8) or (Event="Retic" and result>0.08) then chk=chk+1;

if last.ID then do;
if chk=2 then Toxicity="None";
else Toxicity="Yes";

output;
end;
run;
``````
Solution
‎03-21-2018 10:56 AM
Contributor
Posts: 29

## Re: if with multiple conditions

Proc transpose for HGB and Retic to determinate the status of Toxicity. Then merge back with lab data by id. Sample codes are below

``````proc transpose data=lab out=Toxicity;
by id;
where event in('HGB' 'Retic');
var result;
id event;
run;

data Toxicity;
length  Toxicity \$10;
set Toxicity;
if HGB > 8 and Retic > 0.08 then  Toxicity = "YES";
else  Toxicity = "None";
keep id Toxicity;
run;

data want;
merge lab Toxicity;
by id;
run;``````
☑ This topic is solved.