DATA Step, Macro, Functions and more

if with multiple conditions across observations

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 84
Accepted Solution

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;

View solution in original post


All Replies
Super User
Posts: 22,874

Re: if with multiple conditions

Super User
Posts: 13,084

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:

 

IDEventResultToxicity
1ANC3200None
1HGB7.7None
1Retic0.3839None
1Platelet384None
2ANC1500Yes
2HGB9.3Yes
2Retic0.1118Yes
2Platelet232Yes
3ANC2000Yes
3HGB10.4Yes
3Retic0.2987Yes
3Platelet418Yes

 

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: 320

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.

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

Discussion stats
  • 5 replies
  • 3835 views
  • 0 likes
  • 5 in conversation