BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Riteshdell
Quartz | Level 8

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

 

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

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

7 REPLIES 7
art297
Opal | Level 21

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

 

Riteshdell
Quartz | Level 8

Thanks, @art297, for this solution, 

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

ballardw
Super User

@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;
Riteshdell
Quartz | Level 8
Hello @art297- Can you tell me code if I have all character column.
art297
Opal | Level 21

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;
thomp7050
Pyrite | Level 9

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;

Ksharp
Super User

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;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

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