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
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 |
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 |
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;
Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.
Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.
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.