BookmarkSubscribeRSS Feed
andreas_gryn
Calcite | Level 5

Hi

 

I created a report on a simple dataset using tabulate.

Now I want to add a column with subtotals.

The subtotal needs to be grouped based on the first position of the valuees in the first column.

I can create an extra variable in my dataset with the result.

 

 

 

2014

2015

Totaal

A1

0

13

13

13

A2

0

0

0

B1

2

18

20

25

B2

1

4

5

C1

6

18

24

61

C2

8

29

37

D1

12

29

41

41

E1

67

202

269

451

E2

34

148

182

 

 

PROC TABULATE DATA=akkoord_p_r_age ;
CLASS csymp nr_year;
VAR aantal;
KEYLABEL SUM='';
TABLE csymp='' ALL=&&tot_&taal ,((nr_year='' ALL="Totaal &year_from - &year_to"))*aantal=''*(sum=''*f=10. colpctsum=''*f=pctpic7.2)
/BOX="" MISSTEXT=' ';
RUN;
QUIT;

 

Does anybody have an idea on how to do this easily?

 

Thanks in advance.

 

1 REPLY 1
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Hi,

 

Some tips first.  Use the code window - in post a message its the {i} button above - this retains spacing and such like.  Code formatting is also very important.  Your code is very hard to read - mixed casings, line breaks all over, no indetation, and there is a whole lot of macro variables/code in there.  You also haven't provided any test data (in the form of a datastep) which would give us something to run code on.  I have made a best guess below, this is based on the fact that I personally don't really use proc tabulate, and prefer to do summaries using other methods:

data have;
  input col1 $ year res;
  cat=substr(col1,1,1);
datalines;
A1 2014 1
A1 2014 5
A1 2015 2
A1 2016 8
A2 2014 10
A2 2014 3
A2 2015 4
;
run;

proc sql;
  create table WANT as
  select  distinct 
          CAT,
          COL1,
          (select sum(RES) from HAVE where CAT=A.CAT and COL1=A.COL1 and YEAR=2014) as YEAR2014, 
          (select sum(RES) from HAVE where CAT=A.CAT and COL1=A.COL1 and YEAR=2015) as YEAR2015,
          (select sum(RES) from HAVE where CAT=A.CAT and COL1=A.COL1 and YEAR=2016) as YEAR2016,
          (select sum(RES) from HAVE where CAT=A.CAT and COL1=A.COl1) as TOTAL,
          (select sum(RES) from HAVE where CAT=A.CAT) as TOTAL_CAT 
  from    HAVE A;
quit;

proc report data=want...;
run;

As you will see, you will need another variable whichever way you go to identify which grouping of data to sum.  

sas-innovate-2024.png

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.

 

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
  • 1 reply
  • 989 views
  • 0 likes
  • 2 in conversation