year | id | censor |
2001 | 1 | 1 |
2002 | 1 | 1 |
2003 | 1 | 1 |
2004 | 1 | 1 |
2005 | 1 | 1 |
2006 | 1 | 1 |
2002 | 2 | 0 |
2003 | 2 | 0 |
2004 | 2 | 0 |
2005 | 2 | 0 |
2006 | 2 | 0 |
2002 | 3 | 0 |
2003 | 3 | 0 |
2004 | 3 | 0 |
2005 | 3 | 0 |
2002 | 4 | 1 |
2003 | 4 | 1 |
2004 | 4 | 1 |
From this table, to make a table below,
year | id | censor | y01 | y02 | y03 | y04 | y05 | y06 | outcome |
2001 | 1 | 1 | 1 | 0 | 0 | 0 | 0 | 0 | 0 |
2002 | 1 | 1 | 0 | 1 | 0 | 0 | 0 | 0 | 0 |
2003 | 1 | 1 | 0 | 0 | 1 | 0 | 0 | 0 | 0 |
2004 | 1 | 1 | 0 | 0 | 0 | 1 | 0 | 0 | 0 |
2005 | 1 | 1 | 0 | 0 | 0 | 0 | 1 | 0 | 0 |
2006 | 1 | 1 | 0 | 0 | 0 | 0 | 0 | 1 | 0 |
2002 | 2 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 |
2003 | 2 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 |
2004 | 2 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 |
2005 | 2 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 |
2006 | 2 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 1 |
2002 | 3 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 |
2003 | 3 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 |
2004 | 3 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 |
2005 | 3 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 1 |
2002 | 4 | 1 | 0 | 1 | 0 | 0 | 0 | 0 | 0 |
2003 | 4 | 1 | 0 | 0 | 1 | 0 | 0 | 0 | 0 |
2004 | 4 | 1 | 0 | 0 | 0 | 1 | 0 | 0 | 0 |
I tried to use array function, but it didn't work out well.
Could you please help me to make a code for this?
Thanks so much.
I would guess:
data have;
input year id censor;
datalines;
2001 1 1
2002 1 1
2003 1 1
2004 1 1
2005 1 1
2006 1 1
2002 2 0
2003 2 0
2004 2 0
2005 2 0
2006 2 0
2002 3 0
2003 3 0
2004 3 0
2005 3 0
2002 4 1
2003 4 1
2004 4 1
;
data want;
set have; by id;
array y y01-y06;
do i = 1 to dim(y);
y{i} = i = mod(year, 100);
end;
outcome = last.id and not censor;
drop i;
run;
proc print data=want noobs; run;
I would guess:
data have;
input year id censor;
datalines;
2001 1 1
2002 1 1
2003 1 1
2004 1 1
2005 1 1
2006 1 1
2002 2 0
2003 2 0
2004 2 0
2005 2 0
2006 2 0
2002 3 0
2003 3 0
2004 3 0
2005 3 0
2002 4 1
2003 4 1
2004 4 1
;
data want;
set have; by id;
array y y01-y06;
do i = 1 to dim(y);
y{i} = i = mod(year, 100);
end;
outcome = last.id and not censor;
drop i;
run;
proc print data=want noobs; run;
Same idea as PG, but easier than having to use mod
data want;
set have;
by id;
array y(2001:2006) y01-y06;
do i = lbound(y) to hbound(y);
y{i} = i = year;
end;
drop i;
run;
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.