HI Atul,
We can use the GROUPFORMAT option in the SET statement to subgroup the timeflag into ranges that are defined by us.
please refer:
http://support.sas.com/documentation/cdl/en/lrdict/62618/HTML/default/a000202968.htm#a002611573
example 4 for an example on GROUPFORMAT.
Here's the complete program:
I used the same data ...
/*Test Group Format option*/
OPTIONS FORMDLIM='#';
LIBNAME MYLIB 'C:\My SAS Files' ;
DATA MYLIB.Average1;
INPUT Timeflag 2. demand 4.1 ;
Datalines ;
0 18.0
1 20.0
2 11.0
3 15.0
4 16.0
5 17.0
6 5.0
7 4.0
8 8.0
9 17.0
10 16.0
11 19.0
12 20.0
13 21.0
14 22.0
15 30.0
16 31.0
17 32.0
18 33.0
19 34.0
20 35.0
21 36.0
22 37.0
23 38.0
24 39.0
;
/*Define the range of timeflag using a custom format*/
Proc format LIB = MYLIB;
value range
0-9 = '0-9'
10-18 ='10-18'
19-22 ='19-22'
23-24 ='23-24'
;
run;
/*Don't need to sort our data since our timeflag is already in order but just to generalize things i did a sort on timeflag */
proc sort Data = MYLIB.average1 out=sorted_average1;
by timeflag;
run;
Options FMTSEARCH = (MYLIB);
/*This option gives the path where SAS searches for custom formats*/
data temp;
Format timeflag range.; /*Assign our custom format range to timeflag variable*/
set sorted_average1;
by timeflag
GROUPFORMAT;
/*Now the logic to calculate the sum and then the averages of each timeflag range*/
If FIRST.timeflag then do;
sum_demand=0;
count=0;
end;
sum_demand+demand;
count+1;
IF LAST.timeflag then do;
average_demand = sum_demand/(count);
end;
run;
proc print data =temp;
run;