BookmarkSubscribeRSS Feed
smend
Fluorite | Level 6

I have multi dimensional arrays (lets say K{3,5}) that I would like to rearrange or change the order based on the minimum values of the multi dimensional arrays (L{3,5}).  

 

Example: 

data have;
input k1_1 $ k1_2 $ k1_3 $ k1_4 $ K1_5 $
k2_1 $ k2_2 $ K2_3 $ k2_4 $ k2_5 $
k3_1 $ k3_2 $ k3_3 $ k3_4 $ k3_5 $
l1_1-l1_5 l2_1-l2_5 l3_1-l3_5;
datalines;
a b c d e f g h i j k l m n o 10 5 8 7 9 8 1 2 3 4 8 8 3 20 20
run;

 

The minimum of L arrays are:

 
data minimum;
set have;
array l{3,5} l1_1-l1_5 l2_1-l2_5 l3_1-l3_5;
mink1=.;
mink2=.;
mink3=.;
do i=1  to 5;
if l{1,i}>=0 then do; mink1=min(mink1, l{1,i});end;
if l{2,i}>=0 then do; mink2=min(mink2, l{2,i});end;
if l{3,i}>=0 then do; mink3=min(mink3, l{3,i});end;
end;
run;
 
mink1=5;
mink2=1;
mink3=3;
 
Therefore, I would like to change the orders of the k arrays based on the minimum l.  
 
 
data want;
input     r1_1 $ r1_2 $ r1_3 $ r1_4 $ r1_5 $
  r2_1 $ r2_2 $ r2_3 $ r2_4 $ r2_5 $
  r3_1 $ r3_2 $ r3_3 $ r3_4 $ r3_5 $
  s1_1-s1_5 s2_1-s2_5 s3_1-s3_5;
datalines;
f g h i j k l m n o a b c d e 8 1 2 3 4 8 8 3 20 20 10 5 8 7 9
run;
1 REPLY 1
FreelanceReinh
Jade | Level 19

Hello @smend,

 

Try this:

/* Set array dimensions */

%let d1=3;
%let d2=5;

/* Compute minima and prepare a RENAME statement */

data minima(keep=i m);
set have;
length renlist $8000;
array t[&d2] _temporary_;
array l[&d1,&d2] l:;
do i=1 to &d1;
  do j=1 to &d2;
    t[j]=l[i,j];
  end;
  m=min(of t[*]);
  renlist=catx(' ',renlist,cats('k',i,'_1-k',i,"_&d2=r",i,'_1-r',i,"_&d2 l",
                                    i,'_1-l',i,"_&d2=s",i,'_1-s',i,"_&d2"));
  output;
end;
call symputx('renlist',renlist);
run;

proc sort data=minima;
by m;
run;

/* Create new dataset with rearranged array values */

data want(keep=r: s:);
set have;
set have(rename=(&renlist));
array k[&d1,&d2] k:;
array l[&d1,&d2] l:;
array r[&d1,&d2] r:;
array s[&d1,&d2] s:;
do a=1 to &d1;
  set minima;
  do j=1 to &d2;
    r[a,j]=k[i,j];
    s[a,j]=l[i,j];
  end;
end;
run;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 1 reply
  • 688 views
  • 0 likes
  • 2 in conversation