data have ;
input product $1. month amount ;
datalines;
A 1601 1
A 1602 2
A 1603 3
A 1604 4
A 1605 5
A 1606 6
A 1607 7
A 1608 8
A 1609 9
A 1610 10
A 1611 11
A 1612 12
A 1701 13
A 1702 14
A 1703 15
A 1704 16
A 1705 17
A 1706 18
A 1707 19
A 1708 20
A 1709 21
A 1710 22
A 1711 23
A 1712 24
B 1601 1
B 1602 2
B 1603 3
B 1604 4
B 1605 5
B 1606 6
B 1607 7
B 1608 8
B 1609 90
B 1610 100
B 1611 110
B 1612 120
B 1701 130
B 1702 140
B 1703 150
B 1704 160
B 1705 170
B 1706 180
B 1707 190
B 1708 200
B 1709 210
B 1710 220
B 1711 230
B 1712 240
;
run;Hello,
this is my input dataset.
I try to get a column that woulb be the cumulative sum of the current row and the next 12.
And that for each product.
for example , for product A and month 201609 , the result should be 195 because that is the sum of 9,10,11.....20,21
for example , for product B and month 201609 , the result should be 2170 because that is the sum of 90,100....210,220
thanks in advance for your help
regards,
Nasser
data have ;
input product $1. month amount ;
datalines;
A 1601 1
A 1602 2
A 1603 3
A 1604 4
A 1605 5
A 1606 6
A 1607 7
A 1608 8
A 1609 9
A 1610 10
A 1611 11
A 1612 12
A 1701 13
A 1702 14
A 1703 15
A 1704 16
A 1705 17
A 1706 18
A 1707 19
A 1708 20
A 1709 21
A 1710 22
A 1711 23
A 1712 24
B 1601 1
B 1602 2
B 1603 3
B 1604 4
B 1605 5
B 1606 6
B 1607 7
B 1608 8
B 1609 90
B 1610 100
B 1611 110
B 1612 120
B 1701 130
B 1702 140
B 1703 150
B 1704 160
B 1705 170
B 1706 180
B 1707 190
B 1708 200
B 1709 210
B 1710 220
B 1711 230
B 1712 240
;
run;
data want;
if _n_=1 then do;
if 0 then set have;
dcl hash H (dataset:'have',ordered: "A") ;
h.definekey ("product","month") ;
h.definedata ("amount") ;
h.definedone () ;
dcl hiter ih('h');
end;
set have(drop=amount);
by product month;
RC = IH.SETCUR() ;
_p=product;
do count = 1 to 13 while (RC = 0 and _p=product) ;
sum=sum(sum,amount);
RC = IH.next() ;
end ;
if count < 13 then sum=0;
drop rc count product;
rename _p=product;
run;
hello
whenever there are less than 12 month , no need so I would like get null
thanks
data have ;
input product $1. month : yymmn4. amount ;
format month yymmn6.;
datalines;
A 1601 1
A 1602 2
A 1603 3
A 1604 4
A 1605 5
A 1606 6
A 1607 7
A 1608 8
A 1609 9
A 1610 10
A 1611 11
A 1612 12
A 1701 13
A 1702 14
A 1703 15
A 1704 16
A 1705 17
A 1706 18
A 1707 19
A 1708 20
A 1709 21
A 1710 22
A 1711 23
A 1712 24
B 1601 1
B 1602 2
B 1603 3
B 1604 4
B 1605 5
B 1606 6
B 1607 7
B 1608 8
B 1609 90
B 1610 100
B 1611 110
B 1612 120
B 1701 130
B 1702 140
B 1703 150
B 1704 160
B 1705 170
B 1706 180
B 1707 190
B 1708 200
B 1709 210
B 1710 220
B 1711 230
B 1712 240
;
run;
proc sql;
create table want as
select *,(select sum(amount) from have where product=a.product and
month between a.month and intnx('month',a.month,12)) as want
from have as a;
quit;
Hello Ksharp
sorry but it does not seem to work.maybe my need was not clear enough.
in the output table want, the resut for product A and 1609 month is 264 but 195 is atended coming from sum of 9+10+11+12+13+14+15+16+17+18+19+20+21
data have ;
input product $1. month amount ;
datalines;
A 1601 1
A 1602 2
A 1603 3
A 1604 4
A 1605 5
A 1606 6
A 1607 7
A 1608 8
A 1609 9
A 1610 10
A 1611 11
A 1612 12
A 1701 13
A 1702 14
A 1703 15
A 1704 16
A 1705 17
A 1706 18
A 1707 19
A 1708 20
A 1709 21
A 1710 22
A 1711 23
A 1712 24
B 1601 1
B 1602 2
B 1603 3
B 1604 4
B 1605 5
B 1606 6
B 1607 7
B 1608 8
B 1609 90
B 1610 100
B 1611 110
B 1612 120
B 1701 130
B 1702 140
B 1703 150
B 1704 160
B 1705 170
B 1706 180
B 1707 190
B 1708 200
B 1709 210
B 1710 220
B 1711 230
B 1712 240
;
run;
data want;
if _n_=1 then do;
if 0 then set have;
dcl hash H (dataset:'have',ordered: "A") ;
h.definekey ("product","month") ;
h.definedata ("amount") ;
h.definedone () ;
dcl hiter ih('h');
end;
set have(drop=amount);
by product month;
RC = IH.SETCUR() ;
_p=product;
do count = 1 to 13 while (RC = 0 and _p=product) ;
sum=sum(sum,amount);
RC = IH.next() ;
end ;
if count < 13 then sum=0;
drop rc count product;
rename _p=product;
run;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
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.
Ready to level-up your skills? Choose your own adventure.