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

Hello, all.

 

I have a dataset looks like below:

 

data test;
    input Var1-Var11@;
cards;
0 1 2 3 4 5 6 7 8 9 .
9 0 1 2 3 4 5 6 7 8 .
8 9 0 1 2 3 4 5 6 7 .
. . . . . . . . . . .
;
run;

And my purpose is to find out each row & column maximum values and to assign the maximum value at the corresponding rows and columns. (Place the find out maximum values at the placeholder "."  in the "test" dataset.)

 

 

I tried it below:

 

data findmax;
    set test;
    Var11 = max(of Var1-Var10);
    array arr[*] _numeric_;
    do i = 1 to 11;
        if arr[i] = . then arr[i] = max(of arr[i]);
    end;
    drop i;
    put arr[2];
run;

 

 

In my solution, each row maximum works well. But each column maximum value was still missing.

 

But if I assign some exact number to each last column cell, the code works well. Like:

 

data findmax;
    set test;
    Var11 = max(of Var1-Var10);
    array arr[*] _numeric_;
    do i = 1 to 11;
        if arr[i] = . then arr[i] = 99;
    end;
    drop i;
    put arr[2];
run;

A minor difference of my code ( arr[ i ] = max(of arr[i])  vs.  arr[ i ] = 99 ) makes such different output. Is there any reason in this case and how can I achieve the purpose.

 

Thanks, best regards.

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Jagadishkatam
Amethyst | Level 16

Yes it is possible with the below code, hope it helps

 

data test;
    input Var1-Var10 ;
cards;
0 1 2 3 4 5 6 7 8 9
9 0 1 2 3 4 5 6 7 8
8 9 0 1 2 3 4 5 6 7
;
run;

data want;
set test end=eof;
var11=max(of var1-var10);
output;
retain maxvar1-maxvar10;
array vars(10) var1-var10;
array maxvars(10) maxvar1-maxvar10;
do i = 1 to 10;
if vars(i)> maxvars(i) then maxvars(i)=vars(i);
vars(i)=maxvars(i);
end;
var11=max(of maxvar1-maxvar10);
if eof then output;
drop maxvar: i;
run;
Thanks,
Jag

View solution in original post

13 REPLIES 13
KachiM
Rhodochrosite | Level 12

Try:

 

data test;
    input Var1-Var11;
cards;
0 1 2 3 4 5 6 7 8 3 .
9 0 1 2 3 4 5 6 7 8 .
8 9 0 1 2 3 4 5 6 7 .
;
run;


data findmax;
    set test;
    Var11 = max(of Var1-Var10);
run;
Dominus
Obsidian | Level 7
Thank you for your reply.
Yes, as you say. The right dots are replaced in this way. But the bottom dots still need to be filled.
Shmuel
Garnet | Level 18

Pay attention, arr[i] is just one variable, then max(arr[i]) = arr[i] always.

 

May be you meant to code:

data findmax;
    set test;
    Var11 = max(of Var1-Var10);
    array arr[*] _numeric_;
    do i = 1 to 11;
        if arr[i] = . then arr[i] = var11; /* line changed */
    end;
    drop i;
    put arr[2];
run;

and do you realy want to assign the max value instead missing value, which is the lowest value ?

 

 

Dominus
Obsidian | Level 7

Thank you for your reminder. What I want is just like this:

 

0 1 2 3 4 5 6 7 8 9 9
9 0 1 2 3 4 5 6 7 8 9
8 9 0 1 2 3 4 5 6 7 9
9 9 2 3 4 5 6 7 8 9 9

 

KachiM
Rhodochrosite | Level 12

I have changed a value in the first row to have max = 8;

 

Are you interested in getting the maximum of each column including the 11-th column?

Dominus
Obsidian | Level 7
Yes, including the 11-th column. And I forgot a requirement: Only data step allowed.
Jagadishkatam
Amethyst | Level 16

You may try the below approach

 

data test;
    input Var1-Var10 ;
cards;
0 1 2 3 4 5 6 7 8 9
9 0 1 2 3 4 5 6 7 8
8 9 0 1 2 3 4 5 6 7
;
run;

proc means data=test noprint;
var var1-var10;
output out=test2(where=(_stat_='MAX'));
run;

data want;
set test test2;
var11=max(of var1-var10);
drop _freq_ _stat_ _type_;
run;
Thanks,
Jag
Dominus
Obsidian | Level 7
Hello, Jag. Thank you for your kindly help. The code works well. But if there any approach to solve it by Only data step? Thanks again.
Jagadishkatam
Amethyst | Level 16

Yes it is possible with the below code, hope it helps

 

data test;
    input Var1-Var10 ;
cards;
0 1 2 3 4 5 6 7 8 9
9 0 1 2 3 4 5 6 7 8
8 9 0 1 2 3 4 5 6 7
;
run;

data want;
set test end=eof;
var11=max(of var1-var10);
output;
retain maxvar1-maxvar10;
array vars(10) var1-var10;
array maxvars(10) maxvar1-maxvar10;
do i = 1 to 10;
if vars(i)> maxvars(i) then maxvars(i)=vars(i);
vars(i)=maxvars(i);
end;
var11=max(of maxvar1-maxvar10);
if eof then output;
drop maxvar: i;
run;
Thanks,
Jag
Dominus
Obsidian | Level 7
Thank you Jag. Your solution are very helpful. Thanks again.

And BTW, thanks for all participants' help.
novinosrin
Tourmaline | Level 20

Hello @Dominus  Sorry, Too late to the party as we were sleeping in EDT time

 

FWIW

 


data test;
    input Var1-Var10 ;
cards;
0 1 2 3 4 5 6 7 8 9
9 0 1 2 3 4 5 6 7 8
8 9 0 1 2 3 4 5 6 7
;
run;

data want;
do _n_=1 by 1 until(z);
 set test end=z;
 array v var:;
 array t(3,10) _temporary_;
  do _i=1 to 10;
   t(_n_,_i)=v(_i);
   end;
  output;
end;
call missing(of v(*));
do _j=1 to 10;
 do _k=1 to 3;
    v(_j)=max(v(_j), t(_k,_j));
 end;
end;
output;
drop _:;
run;

 

 

novinosrin
Tourmaline | Level 20


data test;
    input Var1-Var10 ;
cards;
0 1 2 3 4 5 6 7 8 9
9 0 1 2 3 4 5 6 7 8
8 9 0 1 2 3 4 5 6 7
;
run;

data want;
do until(z);
 set test end=z;
 array v var:;
 array t(10) _temporary_ ;
 do _n_=1 to dim(v);
 t(_n_)=t(_n_) max v(_n_);
 end;
 output;
end;
call pokelong(peekclong(addrlong(t(1)),80),addrlong(v(1)),80);
output;
run;
Dominus
Obsidian | Level 7
Hello @novinosrin, many thanks for your kindly help. These two solutions let me benefit a lot. And it's a long way for me to learn SAS.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 13 replies
  • 8252 views
  • 5 likes
  • 5 in conversation