DATA Step, Macro, Functions and more

Enter values sequentially across column

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 16
Accepted Solution

Enter values sequentially across column

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


Accepted Solutions
Solution
3 weeks ago
PROC Star
Posts: 1,760

Re: Enter values sequentially across column

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


All Replies
Solution
3 weeks ago
PROC Star
Posts: 1,760

Re: Enter values sequentially across column

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

 

 

Super User
Posts: 19,878

Re: Enter values sequentially across column

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. 
Occasional Contributor
Posts: 16

Re: Enter values sequentially across column

Thanks for your response. Retain month1-month4(1:4) math1-math4;
Trusted Advisor
Posts: 1,586

Re: Enter values sequentially across column

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;
☑ This topic is solved.

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

Discussion stats
  • 4 replies
  • 118 views
  • 1 like
  • 4 in conversation