Hi, I'm not sure how to code the problem below. Your kind help is greatly appreciated!
Given the set of numbers: 11, 23, 17, 39, 50, using the array to find averages of any two numbers from a set of numbers. The two numbers can be the same.
Hi @Amy0223
Does the following code answer your question?
Best,
data want;
array number(5) (11 23 17 39 50);
do i=1 to dim(number);
do j=i to dim(number);
couple_1 = number(i);
couple_2 = number(j);
average = mean(number(i),number(j));
output;
end;
end;
keep couple_1 couple_2 average;
run;
Use two nested do loops (assuming n to be the number of elements): one counts index1 from 1 to n-1, and the inner loop from index1+1 to n. Build the average of array{index1} and array{index2}.
Hi @Amy0223
Does the following code answer your question?
Best,
data want;
array number(5) (11 23 17 39 50);
do i=1 to dim(number);
do j=i to dim(number);
couple_1 = number(i);
couple_2 = number(j);
average = mean(number(i),number(j));
output;
end;
end;
keep couple_1 couple_2 average;
run;
Thank you @Amy0223 !
Hi @Amy0223 ,
As @Kurt_Bremser wrote do it with double loop.
I'm not sure what do you mean by "The two numbers can be the same.", is it that you want also pair of (first,first), (second,second), (third, third) etc. or that just values can repeat? If you mean first option then change `next` macrovariable to 0.
%let next = 1;
data want;
array A[5] _temporary_ (11, 23, 17, 39, 50);
do i = 1 to dim(a);
do j = i + &next. to dim(a);
first = a[i];
second = a[j];
avg = mean(first, second);
output;
end;
end;
drop i j;
run;All the bets
Bart
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.