BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
DeepakSwain
Pyrite | Level 9

Hi there, 

I need your kind help to transpose my table having  2 groups of data elements related to dates and weight. 

data have;
id= 101;
date_1= '20200101' ;
weight_1=51;
date_2='20200102';
weight_2=52;
date_3='20200103';
weight_3=53;
date_4='20200104';
weight_4=54;
output;
id= 102;
date_1= '20200101' ;
weight_1=61;
date_2='20200102';
weight_2=62;
date_3='20200103';
weight_3=63;
date_4=' ';
weight_4=.;
output;
id= 103;
date_1= '20200101' ;
weight_1=71;
date_2='20200102';
weight_2=72;
date_3=' ';
weight_3=. ;
date_4='20200104 ';
weight_4=74 ;
output;
run;




data want;
input id  date $ weight ;
datalines;
101 20200101 51
101 20200102 52
101 20200103 53
101 20200104 54
102 20200101 61
102 20200102 62
102 20200103 63
103 20200101 71
103 20200102 72
103 20200104 74
;
run;








Thank you in advance for your kind reply.

Swain
1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Why do you have "dates" as character values???

One way:

data want;
   set have;
   by id;
   array d (*) date_1-date_4;
   array w (*) weight_1-weight_4;
   do i=1 to dim(d);
      date=d[i];
      weight=w[i];
      output;
   end;
   keep id date weight;
run;

Character dates are pretty hard to work with for any manipulation and they make lousy graph axis values.

View solution in original post

3 REPLIES 3
ballardw
Super User

Why do you have "dates" as character values???

One way:

data want;
   set have;
   by id;
   array d (*) date_1-date_4;
   array w (*) weight_1-weight_4;
   do i=1 to dim(d);
      date=d[i];
      weight=w[i];
      output;
   end;
   keep id date weight;
run;

Character dates are pretty hard to work with for any manipulation and they make lousy graph axis values.

DeepakSwain
Pyrite | Level 9
Thanks a lot for your quick reply. I agree with you regarding character form of date. To improve the efficiency, I will convert it to date format.
Swain
ballardw
Super User

@DeepakSwain wrote:
Thanks a lot for your quick reply. I agree with you regarding character form of date. To improve the efficiency, I will convert it to date format.

We can actually do that when the shape is changed.

data want;
   set have;
   by id;
   array d (*) date_1-date_4;
   array w (*) weight_1-weight_4;
   do i=1 to dim(d);
      date= input(d[i],yymmdd8.);
      weight=w[i];
      output;
   end;
format date yymmdd10. ;/* or format of your choosing*/ keep id date weight; run;

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 949 views
  • 1 like
  • 2 in conversation