id type score
101 testA 89
101 testB 89
102 testA 89
102 testB 89
I have one dataset like above how to transpose the data in proc sql ?
Output should be like see below example:
id testA testB
101 89 89
101 89 89
We should do in proc sql only.
@thanikondharish . it's possible with the help of case and Join statement.
proc sql ;
create table want as
select a.id,testA,testB from
(select id,
case when type eq 'testA' then score end as testA
from have where calculated testA ne .) as a,
(select id,
case when type eq 'testB' then score end as testB
from have where calculated testb ne .) as b where a.id eq b.id;
quit;
@thanikondharish wrote:
I understood the code .
If we have a number of tests, what can we do.
Maxim 14. Use PROC TRANSPOSE or PROC REPORT, and be done with it.
@Kurt_Bremser says:
But mind that this will be a maintenance nightmare if the contents of variable type change, or need some extra macro coding to make it adaptive.
That's why there must be a VERY GOOD reason to not do it with PROC TRANSPOSE, which is much simpler to code and maintains itself:
and
Also note that wide datasets are always harder to code against, and if this is supposed to create a report, PROC REPORT does this on the fly:
also
Maxim 14. Use PROC TRANSPOSE or PROC REPORT, and be done with it.
All excellent advice.
This ought to end any discussion of doing a transpose using SQL. I hope the original poster is paying attention.
data have;
input id $ type $ score;
datalines;
101 testA 89
101 testB 90
102 testA 91
102 testB 92
;
proc sql;
create table want as
select
id,
max(case
when type = "testA"
then score
else .
end) as testA,
max(case
when type = "testB"
then score
else .
end) as testB
from have
group by id
;
quit;
But mind that this will be a maintenance nightmare if the contents of variable type change, or need some extra macro coding to make it adaptive.
That's why there must be a VERY GOOD reason to not do it with PROC TRANSPOSE, which is much simpler to code and maintains itself:
proc transpose data=have out=want (drop=_name_);
by id;
id type;
var score;
run;
Also note that wide datasets are always harder to code against, and if this is supposed to create a report, PROC REPORT does this on the fly:
proc report data=have;
column id score,type;
define id / group;
define score / "" analysis;
define type / "" across;
run;
Just reading the title of this post "transpose data in proc sql" gave me a headache.
Given the verbose nature of having to name everything explicitly in SQL and the row nature of normal SQL behavior the question is sort of an extreme version of using a spoon to hammer nails. It may work but you will put a lot of effort into getting it to work in any specific case. Minor changes like adding a single value to one variable will add to the work.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.