BookmarkSubscribeRSS Feed
sameer112217
Quartz | Level 8

Hello I am writing a code but here is my difficulty

 

data sample;
length state $50;
input state$ city$ product$ selling_price revenue;
datalines;
Maharashtra Mumbai Soap 100 10
Maharashtra Mumbai Shampoo 200 20
Maharashtra Pune Soap 100 10
Maharashtra Pune Shampoo 200 20
Gujrat Baroda Come 100 2
Gujrat Baroda Pin 300 40
Gujrat Surat Come 100 2
Gujrat Surat Pin 400 10
Punjab Amritsar Pencil 200 3
Punjab Chandigarh Rubber 20 3
;
run;
proc print data=sample;
run;
data sample;
length state $50;
length zone $100;
set sample;
if state="Maharashtra" then target=500;
if state="Gujrat" then target=400;
if state="Punjab" then target=200;
if state="Maharashtra" then Zone="AB";
if state="Gujrat" then Zone="BC/MLA";
if state="Punjab" then Zone="BC/MLA";
run;
proc print data=sample;
run;

proc report data=sample;
column zone state selling_price target diff;
define zone/group;
define state/group;
define selling_price/sum;
define revenue/sum;
define target/mean;
define diff/computed;
compute diff;
diff=selling_price.sum-target.sum;
endcomp;

compute after zone;
zone=catx('',propcase(zone),'Subtotal');
line '';
endcomp;

/*break after state/summarize;*/
break after zone/summarize;
rbrEAK AFTER /SUMMARIzE DOL DUL;

compute after;
state= 'Total';
endcomp;
run;

 

I am getting below attached results. For Target I used target.mean in the diff in compute but I need sum in subtotal and total.

 

proc report.png

7 REPLIES 7
sameer112217
Quartz | Level 8

The subtotal and total I need is sum of the target. The target column numbers I had to add it in the above code, hence I had to use mean/average but while doing total I need the sum. Is there any way to do it.  Also the zone subtotal using catx is showing shorter length. is there any way to increase it

PaigeMiller
Diamond | Level 26

Please provide the actual code that you used that produced this report.

 

The code you provided is not the actual code used, as it produces an error.

 

 WARNING: revenue is not in the report definition.
 ERROR: The variable type of TARGET.SUM is invalid in this context.
 NOTE: The preceding messages refer to the COMPUTE block for diff.
 NOTE: Will not run due to compilation errors.
 NOTE: The SAS System stopped processing this step because of errors.
 NOTE: There were 10 observations read from the data set WORK.SAMPLE.
 NOTE: PROCEDURE REPORT used (Total process time):
       real time           0.10 seconds
       cpu time            0.04 seconds
--
Paige Miller
sameer112217
Quartz | Level 8

Hello paigemiller, here i remove it

 

proc report data=sample;
column zone state selling_price target diff;
define zone/group;
define state/group;
define selling_price/sum;
define target/mean;
define diff/computed;
compute diff;
diff=selling_price.sum-target.mean;
endcomp;

compute after zone;
zone=catx('',propcase(zone),'Subtotal');
line '';
endcomp;

/*break after state/summarize;*/
break after zone/summarize;
rbrEAK AFTER /SUMMARIzE DOL DUL;

compute after;
state= 'Total';
endcomp;
run;

PaigeMiller
Diamond | Level 26

@sameer112217 wrote:

Hello paigemiller, here i remove it

 

proc report data=sample;
column zone state selling_price target diff;
define zone/group;
define state/group;
define selling_price/sum;
define target/mean;
define diff/computed;
compute diff;
diff=selling_price.sum-target.mean;
endcomp;

compute after zone;
zone=catx('',propcase(zone),'Subtotal');
line '';
endcomp;

/*break after state/summarize;*/
break after zone/summarize;
rbrEAK AFTER /SUMMARIzE DOL DUL;

compute after;
state= 'Total';
endcomp;
run;


Your code is asking for means instead of sums. So SAS gives you what you asked for.

 

if that's not what you want, then you'd have to modify the code to get what you want. Can you show us (or tell us) the numbers you want to see, and how they are calculated?

--
Paige Miller
sameer112217
Quartz | Level 8

i want average of target but sum in the subtotal and total

PaigeMiller
Diamond | Level 26

@sameer112217 wrote:

i want average of target but sum in the subtotal and total


I have never seen a table where you show the average on some rows and sum for other rows in the same column. This seems like a bad idea to me.

 

Nevertheless, I have no idea how you can get PROC REPORT to show average on some rows in the column and sum on other rows in a column. Perhaps someone has a clever solution.

 

You could certainly create such a table of means and sums using PROC SUMMARY and data steps, so that the table is exactly what you want, and then simply have PROC REPORT display the resulting table.

--
Paige Miller

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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