DATA Step, Macro, Functions and more

cumulative mean

Reply
Contributor
Posts: 29

cumulative mean

I have a monthly dataset going back a couple years. is there a way in SQL I can
compute an average for each row going back to the first row.

values
month value
jan 1
Feb 2
Mar 3
Apr 4

For example, average for first row would be '1' average for second row would be 1.5 and 3rd would be 3
N/A
Posts: 0

Re: cumulative mean

not sure about sql but you can do it in data step using retain function..
Super User
Posts: 3,260

Re: cumulative mean

Posted in reply to deleted_user
Try this:

data average;
set xxx;
total + value;
average = total/_n_;
run;
Contributor
Posts: 29

Re: cumulative mean

how about in SQL?
Super User
Posts: 3,260

Re: cumulative mean

Your task is far easier to do in a DATA step because you can process one row at a time and accumulate results as you go. This technique is impossible in simple SQL because it does not process row by row. An SQL solution would require multiple queries and probably joining as well. Why not just use the easy DATA step way?!
Contributor
Posts: 29

Re: cumulative mean

what I don't understand is that when I started working on this I found SQL functions (I think it was in oracle) that would compute cumulative means etc but when I tried them in SAS they did not work
Super Contributor
Posts: 281

Re: cumulative mean

Not everything is identical in SQL implemented for Oracle and SQL implemented in SAS. There are differences!
Contributor
Posts: 30

Re: cumulative mean

Hi,

I have the slightly different problem. I want to calculate the cumulative mean but want to insert classification by an identification number. Take a look at the sample below. The first table show what i get when i run the data stpe by SASkiwin above. But i want tell SAS that it has to repeat the same thing for difference classes within the same dataset. The BY variable did not help! Any suggestions guys? I am relatively new to SAS!

time_periodIDincometotalaverage
11505050
21439371.5
311210582.66667
413413996.75
1260199117.2
2221220134.3333
3234254151.4286
4212266165.75
this is what I want (below)
time_periodIDincometotalaverage
11505050
21439371.5
311210582.66667
413413996.75
12603434
22218157.5
323411576.66667
421212789.25

Jessica

Super User
Super User
Posts: 7,981

Re: cumulative mean

Posted in reply to Jessica98

Hi,

In answer to your initial post, it should be real easy to get the cumulative average:

data have;
  attrib month format=$20. month_id value format=best.;
  infile datalines delimiter=",";
  input month $ month_id value;
datalines;
jan,1,45
feb,2,32
mar,3,67
apr,4,34
;
run;

proc sql;
  create table WANT as
  select  A.*,
          (select SUM(VALUE) from WORK.HAVE where MONTH_ID <= A.MONTH_ID) / (select COUNT(MONTH) from WORK.HAVE where MONTH_ID <= A.MONTH_ID) as CUMULATIVE_AVG
  from    HAVE A;
quit;

In answer to your latest post (which I just posted on the other post), with the groupings:

data have;
  attrib id time_period income format=best.;
  infile datalines delimiter=",";
  input id time_period income;
datalines;
1,1,45
1,2,32
1,3,67
1,4,34
2,1,23
2,2,89
2,3,78
2,4,10
;
run;

proc sql;
  create table WANT as
  select  A.*,
          (select SUM(INCOME) from WORK.HAVE where ID=A.ID and TIME_PERIOD <= A.TIME_PERIOD) as TOTAL,
          CALCULATED TOTAL / (select COUNT(ID) from WORK.HAVE where ID=A.ID and TIME_PERIOD <= A.TIME_PERIOD) as CUMULATIVE_AVG
  from    HAVE A;
quit;

Ask a Question
Discussion stats
  • 8 replies
  • 1006 views
  • 0 likes
  • 6 in conversation