BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
NewSASPerson
Quartz | Level 8

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

1 ACCEPTED SOLUTION

Accepted Solutions
BrunoMueller
SAS Super FREQ

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.

data have;
  infile cards;
 
input
  mon :
8.
  date :
anydtdte.
  day : $
8.
  x :
8.
  ;
  week = substr( put( date, weeku.), 1, 8);
  format date date9.;
 
cards;
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/30/13 Wed 20
1 1/31/13 Thur 2
2 2/1/13 Fri 4
2 2/2/13 Sat 5
2 2/28/13 Thur 7
;

data want;
  set have;
  by mon week;
  mFirst = first.mon;
  mLast = last.mon;
  wFirst = first.week;
  wlast = last.week;
run;

proc report data=want nowd;
 
column mon week date day x mtd ytd mFirst mLast wFirst wLast _dummy;
  define mon / order;
 
define date / order;
 
define week / order;
 
define day / display;
 
define x / analysis sum;
 
define mtd / computed;
 
define ytd / computed;
 
define mFirst / display;
 
define mLast / display;
 
define wFirst / display;
 
define wLast / display;
 
define _dummy / computed;

 
compute _dummy / char length=64;
  _dummy = _break_;

 
if mFirst = 1 then do;
  h_mtd =
0;
 
end;

 
if missing(_break_) = 1 then do;
  h_mtd + x.sum;
  h_ytd + x.sum;
 
end;

 
if wLast = 1 then do;
  mtd = h_mtd;
  ytd = h_ytd;
 
end;
 
endcomp;
run;

View solution in original post

2 REPLIES 2
BrunoMueller
SAS Super FREQ

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.

data have;
  infile cards;
 
input
  mon :
8.
  date :
anydtdte.
  day : $
8.
  x :
8.
  ;
  week = substr( put( date, weeku.), 1, 8);
  format date date9.;
 
cards;
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/30/13 Wed 20
1 1/31/13 Thur 2
2 2/1/13 Fri 4
2 2/2/13 Sat 5
2 2/28/13 Thur 7
;

data want;
  set have;
  by mon week;
  mFirst = first.mon;
  mLast = last.mon;
  wFirst = first.week;
  wlast = last.week;
run;

proc report data=want nowd;
 
column mon week date day x mtd ytd mFirst mLast wFirst wLast _dummy;
  define mon / order;
 
define date / order;
 
define week / order;
 
define day / display;
 
define x / analysis sum;
 
define mtd / computed;
 
define ytd / computed;
 
define mFirst / display;
 
define mLast / display;
 
define wFirst / display;
 
define wLast / display;
 
define _dummy / computed;

 
compute _dummy / char length=64;
  _dummy = _break_;

 
if mFirst = 1 then do;
  h_mtd =
0;
 
end;

 
if missing(_break_) = 1 then do;
  h_mtd + x.sum;
  h_ytd + x.sum;
 
end;

 
if wLast = 1 then do;
  mtd = h_mtd;
  ytd = h_ytd;
 
end;
 
endcomp;
run;
NewSASPerson
Quartz | Level 8

your logic worked in a data step for me. Thanks for your help.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 1340 views
  • 4 likes
  • 2 in conversation