Date: Fri, 14 Sep 2007 17:25:34 -0500
Reply-To: Mary <mlhoward@avalon.net>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: Mary <mlhoward@AVALON.NET>
Subject: Re: Identifying the first instance that various conditions are
satisfied
Content-Type: text/plain; charset="iso-8859-1"
OK, I've done it :-). I had fun trying this in IML this afternoon; thanks for the brain teaser!
Here's my try-
Mary Howard
Research Assistant III
Dept. of Ophthalmology
Univ. of Iowa Hospitals and Clinics
******
data set1;
informat id 2. x 1. y 1. first_x 1. first_y 1. first_xyw 1. first_xya 1. ;
input id x y;
cards;
01 1 0
01 0 1
01 1 1
02 1 0
02 0 1
02 1 1
;
data set1a;
set set1;
first_x=0;
first_y=0;
first_xyw=0;
first_xya=0;
proc sql;
select count(*)
into :set1n
from set1a;
run;
%Put >>>&set1n<<< ;
proc iml symsize=20000 worksize=20000;
start mod1(set1n);
edit set1a;
id=99999;
x=0;
y=0;
count_x=0;
count_y=0;
count_xy=0;
do i=1 to set1n;
first_x=0;
first_y=0;
first_xyw=0;
first_xya=0;
old_id=id;
read point i var 'id' into id;
print id old_id;
print count_x count_y count_xy;
if id[1] ^= old_id[1] then
do;
count_x=0;
count_y=0;
count_xy=0;
end;
print count_x count_y count_xy;
read point i var 'x' into x;
if x=1 then
do;
if count_x = 0 then
first_x=1;
count_x=count_x + 1;
end;
read point i var 'y' into y;
if y=1 then
do;
if count_y=0 then
first_y=1;
count_y=count_y + 1;
end;
if x=1 & y=1 then
do;
if count_xy=0 then
first_xyw=1;
count_xy=count_xy+ 1;
end;
if (count_x=1) & (count_y >=1) then
first_xya=1;
else if (count_y=1) & (count_x >=1) then
first_xya=1;
replace;
end;
finish;
run mod1(&set1n);
quit;
run;
proc print data=set1a;
run;
----- Original Message -----
From: Paul Miller
To: SAS-L@LISTSERV.UGA.EDU
Sent: Friday, September 14, 2007 1:25 PM
Subject: Identifying the first instance that various conditions are satisfied
Hello Everyone,
I'm trying to do some coding that involves identifying the first time
that various conditions are satisfied. Below are "have" and "need"
datasets that represent a simplified version of what I'm trying to do.
data have;
input id $ x y;
cards;
1 1 0
1 0 1
1 1 1
;
run;
data need;
input id $ x y first_x first_y first_xyw first_xya;
cards;
1 1 0 1 0 0 0
1 0 1 0 1 0 1
1 1 1 0 0 1 0
;
run;
For first_x and first_y, I just want to identify the first time that
these variables had a value of 1 in the data. For first_xyw, I want to
identify the first time that x and y had a value of 1 within the same
row. For first_xya, I want to identify the first time that x and y had
taken a value of 1 in any row. So this variable is coded 1 in row 2,
because x = 1 in row 1 and y = 1 in row 2. Thus, it's the first time
that we see that combination across any of the rows.
I can find some inelegant ways of doing this (e.g., for first_xya,
creating count variables to identify the number of times x or y has
occurred, coding xya across rows, and then creating a count of xya so
that I can identify the first instance).
I was hoping to find a more elegant approach though.
Can anyone see a good way of doing this?
Thanks,
Paul
Paul J. Miller, Ph.D.
Research Scientist and Statistician
Ontario HIV Treatment Network
1300 Yonge St., Suite 308
Toronto, Ontario M4T 1X3
Phone: (416) 642-6486 ext 232
Fax: (416) 640-4245