SAS Data Integration Studio, DataFlux Data Management Studio, SAS/ACCESS, SAS Data Loader for Hadoop and others

Two rows data into one row

Accepted Solution Solved
Reply
Contributor
Posts: 40
Accepted Solution

Two rows data into one row

Hi,

Can someone help me with the below issue,

I've few columns in a tabls like showing below image. I want the second image as my output.

How can I do it using SQL.

The query which I've used to get first table is:

PROC SQL ;

CREATE TABLE MTG.NEW AS

SELECT  DISTINCT CLIENTNAME, VERSION FROM MTG.NEW_CLIENT;

ROWS.PNG


Accepted Solutions
Solution
‎11-11-2014 10:40 PM
Occasional Contributor
Posts: 8

Re: Two rows data into one row

Posted in reply to NareshAbburi

data NEW;

input CLIENTNAME$ VERSION;

cards;

Client1 8.1

Client1 8.2

Client2 8.1

Client2 8.2

Client3 8.1

Client4 8.1

Client5 8.1

;

run;

proc transpose data=NEW out=OUT1;

  var VERSION;

  by CLIENTNAME;

run;

options missing="";

data OUT2;

  length VERSION2 $20.;

  set OUT1;

  VERSION2 = catx("," , of COLSmiley Happy;

run;

options missing=".";

View solution in original post


All Replies
Respected Advisor
Posts: 4,173

Re: Two rows data into one row

Posted in reply to NareshAbburi

Using a SAS data step would be much easier. Does it need to be a SQL? And if yes: why?

Contributor
Posts: 40

Re: Two rows data into one row

Hi,

no need of Sql. But I'm not sure of how to do it using DATA step.

could you please explain me..?

Super User
Posts: 19,862

Re: Two rows data into one row

Posted in reply to NareshAbburi

try something like the following;

data want;

set have;

length new_var $256.;

retain new_var;

by client_id;

if first.client_id then new_var=var;

else new_var=catx(',', new_var, var);

if last.client_id then output;

run;

Trusted Advisor
Posts: 1,228

Re: Two rows data into one row

Posted in reply to NareshAbburi

proc sql;

create table MTG.NEW as

select clientname,case when min(version)=max(version) then put(min(version),8.1) else

catx(",",min(version),max(version)) end as version from have

group by clientname;

quit;

Contributor
Posts: 40

Re: Two rows data into one row

Hi,

Thanks for your reply.

The code is very good for my purpose although it has got some data type issue with PUT function.

Solution
‎11-11-2014 10:40 PM
Occasional Contributor
Posts: 8

Re: Two rows data into one row

Posted in reply to NareshAbburi

data NEW;

input CLIENTNAME$ VERSION;

cards;

Client1 8.1

Client1 8.2

Client2 8.1

Client2 8.2

Client3 8.1

Client4 8.1

Client5 8.1

;

run;

proc transpose data=NEW out=OUT1;

  var VERSION;

  by CLIENTNAME;

run;

options missing="";

data OUT2;

  length VERSION2 $20.;

  set OUT1;

  VERSION2 = catx("," , of COLSmiley Happy;

run;

options missing=".";

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 515 views
  • 9 likes
  • 5 in conversation