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;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 1 reply
  • 355 views
  • 0 likes
  • 2 in conversation