Help using Base SAS procedures

How to get the average value of a variable for every 5th row

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 6
Accepted Solution

How to get the average value of a variable for every 5th row


Hi,

I have a file with over 43000 unique rows  which has a variable called model, I would like to get the average of this variable model for every fifth row.

I also have the file number indexed from 1 to 43000 but I am stuggling to get it to average it. I would even appreciate help with how to code every row into the same group by creating a new headin as then I could get the means by selecting unique group.

The file currently looks like;

position     model     n

483_1       0.1234     1

678_1       0.2345     2

876_1       0.1456     3

Thank you! Smiley Happy


Accepted Solutions
Solution
‎03-05-2013 01:27 PM
Super Contributor
Posts: 1,636

Re: How to get the average value of a variable for every 5th row

data have;
do i=1 to 50;
output;end;
run;

data want;
set have;
n=int((i-1)/5)+1;
proc print;run;

View solution in original post


All Replies
Solution
‎03-05-2013 01:27 PM
Super Contributor
Posts: 1,636

Re: How to get the average value of a variable for every 5th row

data have;
do i=1 to 50;
output;end;
run;

data want;
set have;
n=int((i-1)/5)+1;
proc print;run;

Respected Advisor
Posts: 4,937

Re: How to get the average value of a variable for every 5th row

I'm not sure I understand the question fully but would something like this do what you want :

proc means data=MyData(where=(mod(n,5)=0));

var model;

run;

PG

PG
Super Contributor
Posts: 334

Re: How to get the average value of a variable for every 5th row

I think we are all getting to the same point ... but here is my version:

data test3;

  set test2;

  if n = 1 then cat = 0;

  group = mod(n,5);

  if group = 1 then cat + 1;

run;

the group variable is basically providing an indicator for every 5th observation. The cat variable is categorizing the group of 5 observations (1-5 cat 1 , 6 - 10 cat2 , etc...).

I wasnt clear if you are average the five observations (average obs 1 through 5, 6 through 10, etc... which would be over 8000 averages) or eliminate all but every 5th obs and average those obs for an outcome of one number.

Anyway .. hope this helps!

EJ

Occasional Contributor
Posts: 6

Re: How to get the average value of a variable for every 5th row

Thanks to everyone for their help, that worked perfectly!first time posting in a forum and was great to get such answers!

Thank you! Smiley Happy

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 763 views
  • 6 likes
  • 4 in conversation