Reputation: 1
I would like to dynamically compute max of n columns of a SAS table.
what I have:
data have;
input id Month d_201701 d_201702 d_201703 d_201704 d_201705 d_201706 d_201707 d_201708 d_201709 d_201710;
datalines;
1 201701 0 1 0 1 1 0 0 0 1 1
2 201702 . 0 2 1 2 2 0 0 1 2
3 201703 . . 1 3 3 4 0 0 1 3
4 201704 . . . 1 2 3 4 1 1 4
5 201704 . . . 15 30 5 5 5 5 5
;
run;
What should do:
data want; set have;
if Month = 201701 then do;
r_201701=max(d_201701,d_201702,d_201703); .... r_201708=max(d_201708,d_201709,d_201710);
end;
....
if Month = 201704 then do;
r_201704=max(d_201704,d_201705,d_201706); .... r_201708=max(d_201708,d_201709,d_201710);
end;
I don't want to take this simplified approach - not only because it's too time consuming and many typos could occur, but also because in my file there are more than 4 months and more than 10 columns (around 100 columns).
I also want the number of columns to be dynamically set (eg. 24 instead of 3 as mentioned above).
Thx for help,
Upvotes: 0
Views: 47
Reputation: 21294
This is fully dynamic. You don't need to know how many dates you have, how many years, or what the months are. It does assume you have continuous dates though and an entry for every date.
data have;
input id Month d_201701 d_201702 d_201703 d_201704 d_201705 d_201706 d_201707 d_201708 d_201709 d_201710;
datalines;
1 201701 0 1 0 1 1 0 0 0 1 1
2 201702 . 0 2 1 2 2 0 0 1 2
3 201703 . . 1 3 3 4 0 0 1 3
4 201704 . . . 1 2 3 4 1 1 4
5 201704 . . . 15 30 5 5 5 5 5
;
run;
proc transpose data=have out=long;
by id;
var d_:;
run;
data long_w_date;
set long;
date = input(compress(compress(_name_,, 'kd'), '_'), yymmn6.);
format date date9.;
run;
proc expand data=long_w_date out=want1;
by id;
id date;
convert col1 = col1_move3 / transformout=( reverse movmax 3 );
run;
Upvotes: 1