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

Hello, 

 

I have 12 months observations and I do need to replace maximum value with other value. 

dht115_0-1645123827759.png

Example:

 

Mat-1:

I do need to replace dmd12 value as follow. 

I do need to identify maximum value to identify dmd12 has maximum value. 

 

18027017231031040008

 

Sum is  40089

subtract with maximum number: 40089 - 40008 = 81

divide by 11: 81/11 = 7.3636

int: int(7.3636) = 7

 

Replace 40008 to 7

 

Mat-2:

I do need to replace dmd8 value as follow. 

I do need to identify maximum value to identify dmd8 has maximum value. 

1051090810085007

Sum is  1053

subtract with maximum number: 1053 - 1008 = 45

divide by 11: 45/11 = 4.0909

int: int(4.0909) = 4

 

Replace 1008 to 4

 

data have;
input item $ dmd1-dmd12;
datalines;
mat1 18 0 27 0 17 2 3 10 3 1 0 40008 
mat2 1 0 5 10 9 0 8 1008 5 0 0 7 
;
run;

/*need to replace maximum dmd value with following logic*/
/*replace_value = int((sum(of DemandMon1-DemandMon12)-maximum dmd)/11);*/

 

1 ACCEPTED SOLUTION

Accepted Solutions
ErikLund_Jensen
Rhodochrosite | Level 12

Hi @dht115 

I wonder if this is a real-life problem, but it is definitely a good case for playing with arrays in SAS. The following code should do the trick:

data have;
	input item$ dmd1-dmd12;
cards;
mat1 18 0 27 0 17 2 3 10 3 1 0 40008
mat2 1 0 5 10 9 0 8 1008 5 0 0 7
;
run;

data want (drop=maxval maxpos);
	set have;
	array d dmd1-dmd12;
	maxval = max(of d{*});
	maxpos = whichn(maxval, of d{*});
	d{maxpos} = int((sum(of d{*}) - maxval) / 11);
run;

 

 

View solution in original post

2 REPLIES 2
ErikLund_Jensen
Rhodochrosite | Level 12

Hi @dht115 

I wonder if this is a real-life problem, but it is definitely a good case for playing with arrays in SAS. The following code should do the trick:

data have;
	input item$ dmd1-dmd12;
cards;
mat1 18 0 27 0 17 2 3 10 3 1 0 40008
mat2 1 0 5 10 9 0 8 1008 5 0 0 7
;
run;

data want (drop=maxval maxpos);
	set have;
	array d dmd1-dmd12;
	maxval = max(of d{*});
	maxpos = whichn(maxval, of d{*});
	d{maxpos} = int((sum(of d{*}) - maxval) / 11);
run;

 

 

ChanceTGardener
SAS Employee

If you have SAS/IML 15.1 or higher licensed, you could do it this way in PROC IML: 

 

data have;
input item $ dmd1-dmd12;
datalines;
mat1 18 0 27 0 17 2 3 10 3 1 0 40008 
mat2 1 0 5 10 9 0 8 1008 5 0 0 7 
;

proc iml;
 use have; /* read data set into IML */
  read all var _NUM_ into X[colname=dmd];
  read all var _CHAR_ into item[colname=char];
 close have;
 
 /* locate maximum value in each row and permanently replace it with desired logic */
 X[loc(X=X[,<>])] = int((X[,+] - X[,<>]) / 11); *print X[c=dmd r=item];

 /* create output data set */
 colNames = char || dmd; 
 create want from item X [colname=colNames];  
  append from item X; 
 close;
quit;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 2 replies
  • 1077 views
  • 4 likes
  • 3 in conversation