BookmarkSubscribeRSS Feed
scad
Calcite | Level 5

Hi All,

 

I Need help

Use an array to find the minimum value excluding 0
 

This is my code

 

Data exam;

input x1 x2 x3 x4 x5 x6;

cards;

0 0 0 0 0 0

0 0 0 0 0 0

0 1 2 4 6 7

0 0 2 1 4 1

 

data exam1;

set exam;

array xall{6} x1 x2 x3 x4 x5 x6;

array temp[6] (0 0 0 0 0 0);

Do i = 1 to dim(xall);

  IF i = 1 and xall{i}=. then temp{i} = 9999 ;

    else if i=1 and xall{i}= 0 then temp{i} = 8888 ;

    else if i=1 and xall{i} >0 then temp{i} = xall[i] ;

 IF i >1 and (temp{i-1}=. and xall{i}=.) then temp{i} = 9999 ;

   else if  i >1 and (temp{i-1}=0 and xall{i}=0) then temp{i} = 8888 ;

   else if i >1  and (temp{i-1}>0 and xall{i}>0) then temp{i} = min(temp{i-1},xall{i});

end;

run;

 

I think the correct answer is
x1 x2 x3 x4 x5 x6 temp1 temp2 temp3 temp4 temp5 temp6

0   0   0   0   0   0  8888   8888    8888   8888   8888   8888

0   0   0   0   0   0  8888   8888    8888   8888   8888   8888

0   1   2   4   6   7  8888     1          1          1         1         1

0   0   2   1   4   1  8888   8888      1          1         1         1

 

 

 

 

 

 

 

 

 

 

 

 

6 REPLIES 6
novinosrin
Tourmaline | Level 20
Data exam;

input x1 x2 x3 x4 x5 x6;

cards;

0 0 0 0 0 0

0 0 0 0 0 0

0 1 2 4 6 7

0 0 2 1 4 1
;

data want;
 set exam;
 array t x1-x6;
 array j(6);
 do over t;
  if t then j(_i_)=t;
 end;
 if n(of j(*)) then min=min(of j(*));
 else min=0;
 drop j:;
run;
novinosrin
Tourmaline | Level 20
Data exam;

input x1 x2 x3 x4 x5 x6;

cards;

0 0 0 0 0 0

0 0 0 0 0 0

0 1 2 4 6 7

0 0 2 1 4 1
;
data want;
 set exam;
 array t x1-x6;
 do over t;
  if t then min=min(t,min);
 end;
 if min=. then min=0;
run;
hhinohar
Quartz | Level 8
Data exam;
	input x1 x2 x3 x4 x5 x6;
	cards;
0 0 0 0 0 0
0 0 0 0 0 0
0 1 2 4 6 7
0 0 2 1 4 1
;
run;

data exam1;
	set exam;
	array xall{6} x1 x2 x3 x4 x5 x6;
	array temp[6] $;
	min=8888;
	*calculate min;
	Do i=1 to dim(xall);
		if xall(i) > 0 then do;
			if min>xall{i} then min=min(min,xall(i));
		end;
	end;
	*feed minimum value to every variable;
	Do i=1 to dim(temp);
		temp(i)=ifn(min(min,xall(i))=0,8888,min(min,xall(i)));
	end;
	drop i;
run;
ballardw
Super User

Does your data have any negative values?

andreas_lds
Jade | Level 19

It is not clear what you expect as result or why you are using the temp array, the constant 8888, please clarify.

 

sas-innovate-wordmark-gradient-background 3.pngSAS Innovate

Call for content now open!

It's your turn to help shape SAS Innovate 2027. Share your expertise and inspire the SAS community.

Submit your proposal →

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
  • 6 replies
  • 5151 views
  • 0 likes
  • 5 in conversation