BookmarkSubscribeRSS Feed
Chikku
Calcite | Level 5

Hi All,

 

Please find below the the input dataset containing 1 observation with 16 var.

which i want to convert as the output dataset having 4 obs with 4 var (4x4 format).

could you please let me know the logic that i can use to do it programatically.

 

Input dataset                              
res1 res2 res3 res4 res5 res6 res7 res8 res9 res10 res11 res12 res13 res14 res15 res16
1 0.866025 0.57735 0.75 0.866025 1 0.333333 0.57735 0.57735 0.333333 1 0.866025 0.75 0.57735 0.866025 1

 

output dataset to be generated:-

prod1 prod2 prod3 prod4
1 0.866025 0.57735 0.75
0.866025404 1 0.333333 0.57735
0.577350269 0.333333 1 0.866025
0.75 0.57735 0.866025 1

 

regards,

karthik.

7 REPLIES 7
RW9
Diamond | Level 26 RW9
Diamond | Level 26

This should work:

data have;
 input res1	res2	res3	res4	res5	res6	res7	res8	res9	res10	res11	res12	res13	res14	res15	res16;
datalines;
1	0.866025	0.57735	0.75	0.866025	1	0.333333	0.57735	0.57735	0.333333	1	0.866025	0.75	0.57735	0.866025	1
;
run;

data want (keep=prod:);
  set have;
  array res{16};
  array prod{4} 8;
  do i=0 to 3;
    do j=1 to 4;
      prod{j}=res{(i*4)+j};
    end;
    output;
  end;
run;
Chikku
Calcite | Level 5

cou;d not see the complete code,can you resend.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Its all there, in the grey box middle of the post?

ballardw
Super User
Repostin likely won't help. Internet explorer has problems rendering code from this forum that includes braces. Often a line or more of code will be missing after {
FreelanceReinh
Jade | Level 19

A slight modification of RW9's code would use a single DO loop rather than two nested DO loops:

do i=1 to 16;
  prod{mod(i-1,4)+1}=res{i};
  if ~mod(i,4) then output;
end;
data_null__
Jade | Level 19

It looks like a correlation matrix to me.  You can alternatively put RES1-RES16 into a two dimensional array.  I added some of the compenents of a TYPE=CORR data set _TYPE_ _NAME_.  

 

data have;
   input res1-res16;
   datalines;
1  0.866025 0.57735  0.75  
0.866025 1  0.333333 0.57735
0.57735  0.333333 1  0.866025 
0.75  4.57735  0.866025 1
;;;;
   run;

data _2d(type=corr);
   retain _type_ 'CORR';
   length _name_ $32;
   set have;
   array res[4,4] res:;
   array prod[4];
   keep prod: _type_ _name_;;
   do i=1 to hbound(res,1);
      _name_ = vname(prod[i]);
      do j=1 to hbound(res,2);
         prod[j]=res[i,j];
         end;
      output;
      end;
   run;

Capture.PNG

 

Ksharp
Super User
It is IML thing.
1)

data have;
   input res1-res16;
   datalines;
1  0.866025 0.57735  0.75  
0.866025 1  0.333333 0.57735
0.57735  0.333333 1  0.866025 
0.75  4.57735  0.866025 1
;;;;
   run;
proc iml;
use have;
read all var _num_ into x;
close;
y=shape(x,4);
create want from y;
append from y;
close;
quit;








2)

data want;
 set have;
 array x{*} _numeric_;
 do i=1 to dim(x) by 4;
  pred1=x{i};pred2=x{i+1};pred3=x{i+2};pred4=x{i+3};
  output;
 end;
 keep pred:;
run;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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
  • 1245 views
  • 3 likes
  • 6 in conversation