BookmarkSubscribeRSS Feed
Kpatel306
Calcite | Level 5

Hi,

Anyone one can help me how can I calculated the average based on different columns for different rows:

e.g:

I want to calculate the field "Average" based on defined column name in "SAvg" to "EAvg":

in first row I want to calculate the average for row M5 to M10 (i.e. 85,589), in second row M5 to M9 (i.e.154,669), and so forth.

SourceM1M2M3M4M5M6M7M8M9M10SAvgEAvg Average
Coll1 £  
  12,547
£
  86,417
£  
  80,289
£
  102,858
£  
  96,533
£  
  91,042
£  
  82,901
£
  100,368
£  
  74,033
£  
  68,658
M5M10
Coll2 £
  109,891
£
  65,897
£  
  67,685
£
  456,987
£
  456,987
£  
  65,897
£
  102,985
£  
  65,897
£  
  81,577
£  
  65,897
M5M9
Coll3 £
  138,245
£
  93,213
£
  104,059
£
  456,987
£
  456,987
£
  456,987
£
  101,874
£  
  93,213
£  
  65,897
£  
  65,897
M4M8
Coll4 £
  102,852
£
  99,359
£
  101,605
£  
  98,892
£  
  93,213
£
  456,987
£
  456,987
£  
  65,897
£  
  99,617
£  
  83,941
M5M10
Coll5 £  
  65,897
£
  81,481
£  
  81,481
£
  108,522
£
  456,987
£  
  95,261
£  
  77,686
£
  100,531
£  
  95,412
£
  103,649
M4M7
Coll6 £  
  65,897
£
  86,797
£  
  82,744
£
  138,464
£
  456,987
£
  107,854
£  
  97,684
£
  116,621
£
  456,987
£
  456,987
M2M6

Thanks,

KP

2 REPLIES 2
PaigeMiller
Diamond | Level 26

I think you could do this via data step ARRAY statements.

UNtested code

data want;

     set have;

     array m m1-m10;

     start=substr(savg,2)+0;

     endd=substr(eavg,2)+0;

     sum=0;

     do i=start to endd;

           sum=sum+m(i);

     end;

     avg=sum/(endd-start+1);

     drop i sum start endd;

run;

--
Paige Miller
Kpatel306
Calcite | Level 5

Thanks Paige

its working

Thanks a lot

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 2 replies
  • 411 views
  • 0 likes
  • 2 in conversation