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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
ed_sas_member
Meteorite | Level 14

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;

 Capture d’écran 2019-12-04 à 07.26.29.png

 

View solution in original post

6 REPLIES 6
Kurt_Bremser
Super User

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}.

ed_sas_member
Meteorite | Level 14

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;

 Capture d’écran 2019-12-04 à 07.26.29.png

 

Amy0223
Quartz | Level 8
Wonderful! This is exactly what I've been looking for. Thank you so much for helping me!! I greatly appreciate your help!
yabwon
Onyx | Level 15

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

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



Amy0223
Quartz | Level 8
Thank you so much!! I greatly appreciate your help!

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