BookmarkSubscribeRSS Feed
thanikondharish
Calcite | Level 5

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.

 

 

7 REPLIES 7
singhsahab
Lapis Lazuli | Level 10

@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
Calcite | Level 5
I understood the code .
If we have a number of tests, what can we do.
singhsahab
Lapis Lazuli | Level 10
Better to use PROC TRANSPOSE or ARRAY.
PaigeMiller
Diamond | Level 26

@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.

--
Paige Miller
Kurt_Bremser
Super User
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;
ballardw
Super User

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: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!

Submit your idea!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 7 replies
  • 3613 views
  • 2 likes
  • 5 in conversation