I have a data format like below:
id time1 time2 time3
1 1
2
3
2 1
2
3
The time was on diagonal for each people, but I want to convert it to a long format with one time variable and different times under it:
ID Time
1 1
1 2
1 3
2 1
2 2
3 3
Does anyone know how to do it? Thank you!
data have;
input id time1 time2 time3 ;
cards;
1 1 . .
. . 2 .
. . . 3
2 1 . .
. . 2 .
. . . 3
;
data want;
set have(rename=(id=_id));
retain id .;
if not missing(_id) then id=_id;
time=coalesce(of time1-time3);
keep id time;
run;
I would try:
data want; set have; time = max(time1,time2,time3); drop time1-time3; run;
Do your second and third rows really have blank ID values?
data have;
input id time1 time2 time3 ;
cards;
1 1 . .
. . 2 .
. . . 3
2 1 . .
. . 2 .
. . . 3
;
data want;
set have(rename=(id=_id));
retain id .;
if not missing(_id) then id=_id;
time=coalesce(of time1-time3);
keep id time;
run;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and save with the early bird rate—just $795!
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.