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

Transpose many column values in to one column using sas di

Accepted Solution Solved
Reply
Contributor
Posts: 63
Accepted Solution

Transpose many column values in to one column using sas di

Hello All,

 

I am looking for a solution for below query.

I have 1 row and 12 column source data like below and looking an output as mentioned below.

How can we use in SAS DI studio/ Base sas.

 

Query;

 

Data:

 

 

source.JPG

Output should be like below:

 

target.JPG

 


Accepted Solutions
Solution
‎04-27-2017 01:12 PM
PROC Star
Posts: 7,364

Re: Transpose many column values in to one column using sas di

Here is one way:

 

data have;
  input x $ x1-x3 y $ y1-y3 z $ z1-z3;
  cards;
A 10 20 30 B 40 50 60 C 70 80 90
;
data want (keep=col1 col2);
  set have;
  array _col2(*) _numeric_;
  array _col1(*) $ x y z;
  k=-2;
  do i=1 to dim(_col1);
    k+3;
    col1=_col1(i);
    do j=k to (k+2);
      col2=_col2(j);
      output;
    end;
  end;
run;

Art, CEO, AnalystFinder.com

 

View solution in original post


All Replies
Solution
‎04-27-2017 01:12 PM
PROC Star
Posts: 7,364

Re: Transpose many column values in to one column using sas di

Here is one way:

 

data have;
  input x $ x1-x3 y $ y1-y3 z $ z1-z3;
  cards;
A 10 20 30 B 40 50 60 C 70 80 90
;
data want (keep=col1 col2);
  set have;
  array _col2(*) _numeric_;
  array _col1(*) $ x y z;
  k=-2;
  do i=1 to dim(_col1);
    k+3;
    col1=_col1(i);
    do j=k to (k+2);
      col2=_col2(j);
      output;
    end;
  end;
run;

Art, CEO, AnalystFinder.com

 

Contributor
Posts: 63

Re: Transpose many column values in to one column using sas di

Thanks, @art297, for this solution, 

One more doubt, if I have all columns as character, then do we need to take as char?

Super User
Posts: 10,538

Re: Transpose many column values in to one column using sas di

[ Edited ]

Ritesh_dellvostro wrote:

Thanks, @art297, for this solution, 

One more doubt, if I have all columns as character, then do we need to take as char?


No. If the variables are the same type you will need to explicitly assign them to an array similar to the way the _col1 array is defined.

 

And a minor variation on @art297's response that doesn't require you to count and set K as a limit  (assuming all of the numeric group have the same numbers of elements)

data want;
   set have;
   array a x y z;
   array b x1-x3 y1-y3 z1-z3;
   do i=1 to dim(a);
      Col1= a[i];
      do j= 1 to dim(a);
         index = (i-1)*dim(a) +j;
         col2 = b[index];
         output;
      end;
   end;
   keep col1 col3;
;
run;
Contributor
Posts: 63

Re: Transpose many column values in to one column using sas di

Hello @art297- Can you tell me code if I have all character column.
PROC Star
Posts: 7,364

Re: Transpose many column values in to one column using sas di

If all are character:

 

data have;
  input (x  x1-x3 y  y1-y3 z  z1-z3) ($);
  cards;
A 10 20 30 B 40 50 60 C 70 80 90
;

data want (keep=col1 col2);
  set have;
  array _col2(*) $ x1-x3 y1-y3 z1-z3;
  array _col1(*) $ x y z;
  k=-2;
  do i=1 to dim(_col1);
    k+3;
    col1=_col1(i);
    do j=k to (k+2);
      col2=_col2(j);
      output;
    end;
  end;
run;
Frequent Contributor
Posts: 93

Re: Transpose many column values in to one column using sas di

data have;
length string $ 100;
infile datalines DLM='#';
input string;
datalines;
A 10 20 30 B 20 30 50 C 60 70 80
;
run;

data b(keep=new rename=new=Col1);
length new $8.;
set have;
do i=1 by 1 while(scan(string,i,', ') ^=' ');
new=scan(string,i,', ');
output;
end;
run;

 

DATA B;
SET B;
RETAIN ID;
IF ANYALPHA(COL1) THEN DO;
ID=COL1;
DELETE;
END;
RUN;

Super User
Posts: 9,687

Re: Transpose many column values in to one column using sas di

It was very easy for IML if they are all character variables.

 

data have;
  input (x  x1-x3 y  y1-y3 z  z1-z3) ($);
  cards;
A 10 20 30 B 40 50 60 C 70 80 90
;
run;
proc iml;
use have;
read all var _ALL_ into x;
close;
y=shape(x,0,4);
name=repeat(y[,1],1,3);

col1=colvec(name);
col2=colvec(y[,2:4]);

create want var{col1 col2};
append;
close;
quit;
☑ This topic is solved.

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

Discussion stats
  • 7 replies
  • 214 views
  • 1 like
  • 5 in conversation