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

Hi There,

 

I have following code for my SAS query

 

PROC SQL;
CREATE TABLE WORK."DIRECTOR_ME_2019Q1"n AS
SELECT t1.DIRECTOR_NAME,
/* Medium(Oct-2019) */
(SUM(case when t1.YR=2019 and t1.MT=10 and t1.SEGMENT='Medium' then t1.TotalAGA else 0 end)) AS
'Medium(Oct-2019)'n,
/* Medium(Nov-2019) */
(SUM(case when t1.YR=2019 and t1.MT=11 and t1.SEGMENT='Medium' then t1.TotalAGA else 0 end)) AS
'Medium(Nov-2019)'n,
/* Medium(Dec-2019) */
(SUM(case when t1.YR=2019 and t1.MT=12 and t1.SEGMENT='Medium' then t1.TotalAGA else 0 end)) AS
'Medium(Dec-2019)'n,
/* Medium(Q4-2019) */
(SUM(case when t1.Quarter='2019-Q4' and t1.SEGMENT='Medium' then t1.TotalAGA else 0 end)) AS
'Medium(Q4-2019)'n,
/* Enterprise(Oct-2019) */
(SUM(case when t1.YR=2019 and t1.MT=10 and t1.SEGMENT='Enterprise' then t1.TotalAGA else 0 end)) AS
'Enterprise(Oct-2019)'n,
/* Enterprise(Nov-2019) */
(SUM(case when t1.YR=2019 and t1.MT=11 and t1.SEGMENT='Enterprise' then t1.TotalAGA else 0 end)) AS
'Enterprise(Nov-2019)'n,
/* Enterprise(Dec-2019) */
(SUM(case when t1.YR=2019 and t1.MT=12 and t1.SEGMENT='Enterprise' then t1.TotalAGA else 0 end)) AS
'Enterprise(Dec-2019)'n,
/* Enterprise(Q4-2019) */
(SUM(case when t1.Quarter='2019-Q4' and t1.SEGMENT='Enterprise' then t1.TotalAGA else 0 end)) AS
'Enterprise(Q4-2019)'n
FROM WORK.QUERY_FOR_ACTS_MONTHLYTOTALS t1
WHERE t1.Quarter = '2019-Q4'
GROUP BY t1.DIRECTOR_NAME;
QUIT;

 

Which produces following table out put which is sent out in an email

 

Director _NameMedium (Oct 2019)Medium (Nov 2019)Med (Dec 2019)
Tom102030
John405060
Harry708090

 

I want total to appear at bottom of all the column so it sums up values in each column. Since I am a novice can anyone help me how to do it in current code or in query builder?

 

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

@jgos wrote:

Hi PaigeMiller,

I replace have2 with my input data and it returned with an error

 

Variable date is not on file WORK.QUERY_FOR_ACTS_MONTHLYTOTALS

 

My input data has following columns for date :

YR : e.g 2019

MT : e.g 10

Month : e.g October

Quarter : e.g 2019-q4


But you have to create data set HAVE2, because in the creation of HAVE2, variable DATE is created. You can't leave out that important step.

--
Paige Miller

View solution in original post

18 REPLIES 18
PaigeMiller
Diamond | Level 26

I would do this in PROC REPORT, it's pretty simple there. But I don't know how to do this in Query Builder.

--
Paige Miller
jgos
Calcite | Level 5

Hi PaigeMiller,

 

Can you show me how syntax would look like. Please keep in mind name of month and quarter will change every quarter?

 

Thanks

Reeza
Super User

This can get you started, it's untested as there was no sample input data.

 

proc means data=query..... SUM;
class director_name YR SEGMENT;
var totalaga;
where quarter = '2019-Q4';
ods output summary = middle;
run;

data middle2;
set middle;
date = mdy(yr, 1, mt);
IDvar = catx("_", segment, put(date, monyy7.);
display_name = catt(Segment, "(",  put(date, monname3.), "-", YR, ")");
run;

proc transpose data=middle2 out=want;
by director_name;
id idvar;
idlabel  display_name;
var sum;
run;

@jgos wrote:

Hi PaigeMiller,

 

Can you show me how syntax would look like. Please keep in mind name of month and quarter will change every quarter?

 

Thanks


 

jgos
Calcite | Level 5

Hi Reeza,

 

Sample data looks like this. Is there anywhere query I need to refer to table it is coming from?

 

YRMTMonthQuarterDIRECTOR_NAMESEGMENTTotalAGA
201910  October2019-Q4John SmithMedium6
201910  October2019-Q4John SmithMedium13
201910  October2019-Q4Joe DoeMedium1
201911November2019-Q4Steve SmithEnterprise3
20191January2019-Q1John SmithMedium1
20195May2019-Q2Tony SmithMedium2
Reeza
Super User
Replace the query.... with your input data set name and try running it.
jgos
Calcite | Level 5
Hi Reeza,

I did that and replaced it with my input data set name, However for Proc transpose step it gives me an error that variable SUM not found.

Thanks

Jgos
Reeza
Super User
Check the data set for the actual variable name then.
jgos
Calcite | Level 5

Hi Reeza,

 

Output of want is now as following.

1. Can we hide _name_ and _label_ column?

2. Can we group all enterprise i.e enterprise oct, enterprise nov, Medium Oct, medium nov and add a column for quarter total so it adds up all three months for each segment

3. It is still not summing up totals of each column

 

DIRECTOR_NAME_NAME__LABEL_Enterprise_OCT2019Medium_OCT2019Enterprise_NOV2019Medium_NOV2019
ATotalAGA_SumSum10509030
BTotalAGA_SumSum206010040
CTotalAGA_SumSum30701050
DTotalAGA_SumSum40802060

 

PaigeMiller
Diamond | Level 26

@jgos wrote:

Hi PaigeMiller,

 

Can you show me how syntax would look like. Please keep in mind name of month and quarter will change every quarter?

 

Thanks


The changing name of the month and quarter is not a problem.

 

However, you need to show me a representative portion of the data from data set WORK.QUERY_FOR_ACTS_MONTHLYTOTALS t1 that will be used here.

--
Paige Miller
jgos
Calcite | Level 5

Sample data looks like this

 

YRMTMonthQuarterDIRECTOR_NAMESEGMENTTotalAGA
201910  October2019-Q4John SmithMedium6
201910  October2019-Q4John SmithMedium13
201910  October2019-Q4Joe DoeMedium1
201911November2019-Q4Steve SmithEnterprise3
20191January2019-Q1John SmithMedium1
20195May2019-Q2Tony SmithMedium2
PaigeMiller
Diamond | Level 26

@jgos wrote:

Sample data looks like this

 

YR MT Month Quarter DIRECTOR_NAME SEGMENT TotalAGA
2019 10   October 2019-Q4 John Smith Medium 6
2019 10   October 2019-Q4 John Smith Medium 13
2019 10   October 2019-Q4 Joe Doe Medium 1
2019 11 November 2019-Q4 Steve Smith Enterprise 3
2019 1 January 2019-Q1 John Smith Medium 1
2019 5 May 2019-Q2 Tony Smith Medium 2

Sadly this isn't helpful. Your original desired output did not contain a segment named ENTERPRISE, what should we do with that?

 

Also, your original desired output contained only three months, all in one quarter, but ... here there are four distinct months, spread over three quarters. What should we do with that?

 

Please provide a desired output table for this new data.

--
Paige Miller
jgos
Calcite | Level 5

Sorry for that. My bad. Desired output would look like following. From total data for whole year email output will show only values for current quarter and months. Maybe somewhere in query I can filter on quarter if it makes sense.

 

Director_NameMedium (Oct-2019)Medium (Nov-2019)Medium (Dec-2019)Medium (Q4-2019)Enterprise (Oct-2019)Enterprise (Nov-2019)Enterprise (Dec-2019)Enterprise (Q4-2019)Total
John Smith12361011123339
Joe Doe456151314154257
Steve Smith789241617185175
Tony Smith101112331920216093
Total22263078586266186264
PaigeMiller
Diamond | Level 26
/* UNTESTED CODE */
data have2;
    set have;
    date=mdy(mt,1,yr);
    quarter=date;
    format date monyy7. qtr yyq.;
run;

proc report data=have2;
    columns director_name segment,(date quarter),total_aga;
    define director_name/group 'Director Name';
    define segment/across 'Segment';
    define date/across;
    define quarter/across;
    define total_aga/sum;
    rbreak after/summarize;
run;
    
--
Paige Miller
jgos
Calcite | Level 5

Hi PaigeMiller,

I replace have2 with my input data and it returned with an error

 

Variable date is not on file WORK.QUERY_FOR_ACTS_MONTHLYTOTALS

 

My input data has following columns for date :

YR : e.g 2019

MT : e.g 10

Month : e.g October

Quarter : e.g 2019-q4

 

Thanks

 

Jgos

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 18 replies
  • 2093 views
  • 4 likes
  • 3 in conversation