I am trying to figure out the cumulative total for each week end (Saturday) and the total for all weeks within each month so that the cumulative total resets each month for mtd and adds for ytd. Below is my sample code and desired results. My code adds the break values which is not what I want…any help in understanding how to approach this is appreciated. Thanks!
Mon Date Day x
1 1/1/13 Tue 10
1 1/2/13 Wed 5
1 1/3/13 Thur 10
1 1/4/13 Fri 8
1 1/5/13 Sat 6
1 ...
1 1/30/13 Wed 20
1 1/31/13 Thur 2
2 2/1/13 Fri 4
2 2/2/13 Sat 5
2 ...
2 2/28/13 Thur 7
proc report data=test;
column mon day date x mtd_x ytd_x;
define mon/ group noprint;
define x/analysis sum;
define mtd_x/computed;
define ytd_x/computed;
break after month_num/summarize;
compute mtd_x; total+mtd_x.sum;
IF day = "Sat" then mtd_x=total;
if _break_="mon" then mtd_x=x.sum;
endcomp;
compute ytd_x;
total_1+ytd_x.sum;
IF day = "Sat" then ytd_x=total_1;
if _break_="mon" then ytd_x=total_1;
endcomp;
run;
Sample Result needed
Mon Date Day x mtd_x ytd_x
1 1/1/13 Tue 10
1 1/2/13 Wed 5
1 1/3/13 Thur 10
1 1/4/13 Fri 8
1 1/5/13 Sat 6 39 39
1 ...
1 1/30/13 Wed 20
1 1/31/13 Thur 2 61 61
2 2/1/13 Fri 4
2 2/2/13 Sat 5 9 70
2 ...
2 2/28/13 Thur 7 16 77
Hi
With proc report you can find out when a "group" begins, but there is no way to find out whether you are in the last line for a group. But you can use a DATA Step giving you this information.
See sample code below. currently the report does print more columns than needed, but it will in understanding what is happening.
Hi
With proc report you can find out when a "group" begins, but there is no way to find out whether you are in the last line for a group. But you can use a DATA Step giving you this information.
See sample code below. currently the report does print more columns than needed, but it will in understanding what is happening.
your logic worked in a data step for me. Thanks for your help.
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.