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

How can I get the table below:

 

id month1 month2 month3 month4 math1  math2 math3 math4
1 1 2 3 4 10 15 24  .
2 1 2 3 4  . 15  . 12
3 1 2 3 4 15 18 17 13

 

 

My code below give the following table

 

id month1 month2 month3 month4 math1  math2 math3 math4
1 1 2 3  . 10 15 24  .
2 2 4  .  .  . 15  . 12
3 1 2 3 4 15 18 17 13

 

data tic;
input id country$ month math;
datalines;
1  1 10 
1  2 15 
1  3 24 
2  2 15 
2  4 12 
3  1 15 
3  2 16 
3  3 17 
3  4 15 
;
run;
proc sort data=tic;
by id;
run;

 data tot(drop=month math);
 retain month1-month4 math1-math4;
array tat{4} month1-month4;
array kat{4} math1-math4;
set tic;
by id;
if first.id then do;
i=1;
do j=1 to 4;
tat{j}=.;
kat{j}=.;
end;
end;
tat(i)=month;
kat(month)=math;
if last.id then output;
i+1;
run;

 What I want is to be able to produce the first table not the second table. The problem is with being able to sequentially number each column to tally with the column number per each id. any help is highly appreciated.

 

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
ChrisNZ
Tourmaline | Level 20

Like this?

 


data TOT(drop=MONTH MATH);
  set TIC;
  retain MONTH1 1 MONTH2 2 MONTH3 3 MONTH4 4 MATH1-MATH4;
  array KAT{4} MATH1-MATH4;
  by ID;
  if first.ID then call missing(of KAT[*]);
  KAT[MONTH]=MATH;
  if last.ID then output;
run;

 

ID MONTH1 MONTH2 MONTH3 MONTH4 MATH1 MATH2 MATH3 MATH4
1 1 2 3 4 10 15 24 .
2 1 2 3 4 . 15 . 12
3 1 2 3 4 15 16 17 15

 

 

View solution in original post

4 REPLIES 4
ChrisNZ
Tourmaline | Level 20

Like this?

 


data TOT(drop=MONTH MATH);
  set TIC;
  retain MONTH1 1 MONTH2 2 MONTH3 3 MONTH4 4 MATH1-MATH4;
  array KAT{4} MATH1-MATH4;
  by ID;
  if first.ID then call missing(of KAT[*]);
  KAT[MONTH]=MATH;
  if last.ID then output;
run;

 

ID MONTH1 MONTH2 MONTH3 MONTH4 MATH1 MATH2 MATH3 MATH4
1 1 2 3 4 10 15 24 .
2 1 2 3 4 . 15 . 12
3 1 2 3 4 15 16 17 15

 

 

Reeza
Super User
If you don’t know the number of months/scores ahead of time use PROC TRANSPOSE instead and merge the results. This approach is fully dynamic and an earlier post had the code for this methodology. 
Teamtim
Fluorite | Level 6
Thanks for your response. Retain month1-month4(1:4) math1-math4;
Shmuel
Garnet | Level 18

It seems that month1-month4 have constant values, then replace last step into:

data tot(drop=month math);
   retain month1 1 
           month2 2
           month3 3
           month4 4
           math1-math4;

   array kat{4} math1-math4;
set tic;
by id;
    if first.id then do;
      do j=1 to 4;
           kat{j}=.;
      end;
   end;
   kat(month)=math;
   if last.id then output;
   DROP j month; /* ??? */
run;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

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
  • 4 replies
  • 1449 views
  • 1 like
  • 4 in conversation