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

Hello,

What is the way to create this report and shorted code and more useful way?

Also in last row (the grand total) it is better that two "total" values will me merged into one "total"  cover two fields ("branch" and "status")

Thanks

 

 

 


Data Rawtbl;
input ID branch status Y;
cards;
1 729 1 10
2 729 0 20
3 729 0 30
4 729 0 15
5 836 1 25
6 836 1 50
7 836 1 20
8 836 1 40
9 729 0 40
10 729 0 50
11 836 0 30
12 836 0 80
;
run;


PROC SQL;
	create table t1  as
	select branch,status,
		count(*) as Nr_customers  ,
	       sum(Y) as SUM_Y,
			calculated  SUM_Y/calculated Nr_customers AS AVG_Y
	from  Rawtbl
	group by branch,status
;
QUIT;


PROC SQL;
	create table t2  as
	select status,
		   count(*) as Nr_customers  ,
	       sum(Y) as SUM_Y,
			calculated  SUM_Y/calculated Nr_customers AS AVG_Y
	from  Rawtbl
	group by status
;
QUIT;


PROC SQL;
	create table t3  as
	select  count(*) as Nr_customers  ,
	       sum(Y) as SUM_Y,
			calculated  SUM_Y/calculated Nr_customers AS AVG_Y
	from  Rawtbl
;
QUIT;

Data wanted;
retain branch_char status_char;
set t1 t2 t3;
branch_char=STRIP(put(branch,best12.));
status_char=STRIP(put(status,best12.));

IF branch_char=: '.' then branch_char='Total';
IF status_char=: '.' then status_char='Total';

drop branch status;
rename branch_char=branch  status_char=status;
run;

proc report data=wanted nowd;
column branch status Nr_customers SUM_Y ;
Define branch/display;
Define status/display;
Define Nr_customers/display;
Define SUM_Y/display;
Run;

1 ACCEPTED SOLUTION

Accepted Solutions
andreas_lds
Jade | Level 19

I don't see AVG_Y in the output at all. Why are you calculating a variable that you don't use afterwards?

proc summary data=work.rawtbl;
   class branch status;
   var y;
   output out=work.calc(rename=(_freq_=Nr_customers)) sum=sum_y;
run;

proc sort data=work.calc out=work.sorted;
   where _type_ in (0, 1, 3);
   by descending _type_;
run;

proc format;
   value SumLabel
      . = 'Total'
   ;
run;

proc print data=work.sorted;
   format branch status SumLabel.;
run;

View solution in original post

4 REPLIES 4
Shmuel
Garnet | Level 18

Use next code instead the 3 sql steps.

I leave to you to create the report from its output:

proc summary data=rawtbl;
   class branch status;
   var y;
   output out=sums sum=SUM_Y mean=AVG_Y;
run;

There are more options using PROC SUMMARY or PROC MEANS.

Check the documentation.

 

Pay attention to next columns:  _TYPE_  _FREQ_.

andreas_lds
Jade | Level 19

I don't see AVG_Y in the output at all. Why are you calculating a variable that you don't use afterwards?

proc summary data=work.rawtbl;
   class branch status;
   var y;
   output out=work.calc(rename=(_freq_=Nr_customers)) sum=sum_y;
run;

proc sort data=work.calc out=work.sorted;
   where _type_ in (0, 1, 3);
   by descending _type_;
run;

proc format;
   value SumLabel
      . = 'Total'
   ;
run;

proc print data=work.sorted;
   format branch status SumLabel.;
run;
Ronein
Onyx | Level 15
Thanks!
What is the way to have one total in grand total raw ?
Now you can see that in last raw it is written 2 times total.
Thanks
andreas_lds
Jade | Level 19

@Ronein wrote:
Thanks!
What is the way to have one total in grand total raw ?
Now you can see that in last raw it is written 2 times total.
Thanks

Unfortunately collapsing both cells is not possible. You could replace the format by another data-step - such a step is in the code you have posted. Changing the missing-option to display missing numerics as blank, could help getting what you want:

options missing=' ';

data work.want;
   set work.sorted(rename=(branch = numBranch status = numStatus));
   
   length Branch $ 12 Status $ 1;
   
   Branch = left(put(numBranch, 12.));
   Status = put(numStatus, 1.);
   
   if missing(Status) and not missing(Branch) then Status = 'Total';
   if missing(Branch) then Branch = 'Total';
   
   drop num: _type_;
run;


proc print data=work.want;
   var branch status Nr_Customers sum_y;
run;

options missing='.';

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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