I have a table with the following columns:
I would like to transpose this table into the following format:
I want to avoid using PROC SQL union all since the data I am querying is extremely large
Good idea to avoid PROC SQL here. I don't even know if SQL can do this transpose, but I'm sure I don't want to try to figure out how to program it in SQL.
From now on, please provide a portion of your data following these examples and isntructions. Here is fake data converted to the requested format.
data have;
input member_id contract_jan contract_feb contract_mar contract_apr pbp_jan pbp_feb pbp_mar pbp_apr cost_sharing_jan cost_sharing_feb
cost_sharing_mar cost_sharing_apr;
cards;
123 1 2 3 4 5 6 7 8 9 10 11 12
143 13 14 15 16 17 18 19 20 21 22 23 24
431 14 24 34 44 54 64 74 84 94 104 114 12
999 135 145 155 165 175 185 195 205 215 225 235 24
;
data want;
set have;
array c contract:;
array p pbp:;
array cs cost_sharing:;
do i=1 to dim(c);
_contract=c(i);
_pbp=p(i);
_cost_sharing=cs(i);
monthname=scan(vname(c(i)),2,'_');
date=input(cats('01',monthname,'2019'),date9.);
output;
end;
drop i contract: pbp: cost_sharing: monthname;
format date monyy7.;
run;
In addition, it would be wise not to work with months as character strings; a better way to handle this is to turn these month character strings into actual valid SAS date values. I have done this in the code above, stealing a method from @Patrick here.
Good idea to avoid PROC SQL here. I don't even know if SQL can do this transpose, but I'm sure I don't want to try to figure out how to program it in SQL.
From now on, please provide a portion of your data following these examples and isntructions. Here is fake data converted to the requested format.
data have;
input member_id contract_jan contract_feb contract_mar contract_apr pbp_jan pbp_feb pbp_mar pbp_apr cost_sharing_jan cost_sharing_feb
cost_sharing_mar cost_sharing_apr;
cards;
123 1 2 3 4 5 6 7 8 9 10 11 12
143 13 14 15 16 17 18 19 20 21 22 23 24
431 14 24 34 44 54 64 74 84 94 104 114 12
999 135 145 155 165 175 185 195 205 215 225 235 24
;
data want;
set have;
array c contract:;
array p pbp:;
array cs cost_sharing:;
do i=1 to dim(c);
_contract=c(i);
_pbp=p(i);
_cost_sharing=cs(i);
monthname=scan(vname(c(i)),2,'_');
date=input(cats('01',monthname,'2019'),date9.);
output;
end;
drop i contract: pbp: cost_sharing: monthname;
format date monyy7.;
run;
In addition, it would be wise not to work with months as character strings; a better way to handle this is to turn these month character strings into actual valid SAS date values. I have done this in the code above, stealing a method from @Patrick here.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.
Lock in the best rate now before the price increases on April 1.
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.
Ready to level-up your skills? Choose your own adventure.