BookmarkSubscribeRSS Feed
R_Win
Calcite | Level 5
i have

data report;
input dat tran;
format dat mmddyy8.;
cards;
10/12/98 45
10/16/98 66
10/26/98 23
11/06/00 89
11/20/00 75
11/23/00 45
11/29/00 63

Now i want the sum of tran between 10/12/98 to 10/16/98, and i want the sum of tran from 11/06/00 to 11/23/00 in to a new variable Tot.if the trans is not between the mentioned dates it should remain same .


output shd be

dat tran Total
10/12/98 45 -
10/16/98 66 101
10/26/98 23 23
11/06/00 89 -
11/20/00 75 -
11/23/00 45 203
11/29/00 63 63

Message was edited by: Main

Message was edited by: Main Message was edited by: Main
2 REPLIES 2
1162
Calcite | Level 5
My editing didn't work. I'll try reposting Message was edited by: 1162
1162
Calcite | Level 5
Well, this isn't very elegant, but it produces the output you're looking for. In order to run this, you have to hard code each date range (and increment the Period by 1) in a table called ranges. After that, I think it's pretty automatic.

[pre]data report;
input dat mmddyy8. tran;
format dat mmddyy8.;
cards;
10/12/98 45
10/16/98 66
10/26/98 23
11/06/00 89
11/20/00 75
11/23/00 45
11/29/00 63
;
run;

data ranges;
set report;
if '12Oct1998'd le dat le '16Oct1998'd then do; Period=1; output; end;
if '06Nov2000'd le dat le '23Nov2000'd then do; Period=2; output; end;
run;

proc sql;
create table sums as
select period, dat, max(dat) as End, case when dat=max(dat) then sum(tran) else . end as Total
from ranges
group by period
order by period, dat;
quit;

data report (drop=period end);
merge report (in=a) sums (in=b);
by dat;
if a and not b then total=tran;
run;[/pre]

sas-innovate-white.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.

 

Early bird rate extended! Save $200 when you sign up by March 31.

Register now!

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
  • 2 replies
  • 1216 views
  • 0 likes
  • 2 in conversation