| 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;
Navigate AI governance with SAS tools for compliance and ethical AI practices. Try SAS Ai Navigator for free for the first six months.
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.
Ready to level-up your skills? Choose your own adventure.