BookmarkSubscribeRSS Feed
wkossack
Calcite | Level 5
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
8 REPLIES 8
deleted_user
Not applicable
not sure about sql but you can do it in data step using retain function..
SASKiwi
PROC Star
Try this:

data average;
set xxx;
total + value;
average = total/_n_;
run;
wkossack
Calcite | Level 5
how about in SQL?
SASKiwi
PROC Star
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?!
wkossack
Calcite | Level 5
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
Paige
Quartz | Level 8
Not everything is identical in SQL implemented for Oracle and SQL implemented in SAS. There are differences!
Jessica98
Calcite | Level 5

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

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 8 replies
  • 3822 views
  • 0 likes
  • 6 in conversation