DATA Step, Macro, Functions and more

data from wide to long

Accepted Solution Solved
Reply
Contributor HG
Contributor
Posts: 23
Accepted Solution

data from wide to long

I have a dataset with one record per ID. I want to make a dataset with multiple observations per ID (from wide to long).

My original dataset is like,

data have;

input id $ c1-c5 t1-t5;

cards;

1 21 23 34 45 44 22 33 44 55 66

2 21 23 34 45 44 22 33 44 55 66

;

run;

My final dataset is like,

ID    C     T

1     21    22

1     23    33

1     34    44

1     45    55

1     44    66

2     21    22

2     23    33

2     34    44

2     45    55

2     44    66

Is there a way to do the job using ARRAY?

Thanks,

HG


Accepted Solutions
Solution
‎01-19-2012 10:47 PM
Super Contributor
Posts: 1,636

Re: data from wide to long

how about:

data have;

input id $ c1-c5 t1-t5;

cards;

1 21 23 34 45 44 22 33 44 55 66

2 21 23 34 45 44 22 33 44 55 66

;

run;

data want(keep=id c t);

set have;

array _c(*)c:;

array _t(*) t:;

do i=1 to 5;

  c=_c(i);

  t=_t(i);

output;

end;

run;

proc print;run;

Linlin

View solution in original post


All Replies
Solution
‎01-19-2012 10:47 PM
Super Contributor
Posts: 1,636

Re: data from wide to long

how about:

data have;

input id $ c1-c5 t1-t5;

cards;

1 21 23 34 45 44 22 33 44 55 66

2 21 23 34 45 44 22 33 44 55 66

;

run;

data want(keep=id c t);

set have;

array _c(*)c:;

array _t(*) t:;

do i=1 to 5;

  c=_c(i);

  t=_t(i);

output;

end;

run;

proc print;run;

Linlin

Contributor HG
Contributor
Posts: 23

data from wide to long

It's perfect!

Thanks,

HG

☑ This topic is SOLVED.

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

Discussion stats
  • 2 replies
  • 166 views
  • 0 likes
  • 2 in conversation