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

Hi, I'm still a newbie with SAS an I need help with maybe a simple question.

 

How do you add a subtotal column in a database?

 

Say this is my sample dataset:

 

data have;
	input zip $ branch $ count;

	datalines;
	501 52--- 1
	501 81--- 2
	501 99--- 1
	101 22--- 2
	101 23--- 48
	101 31--- 40
	101 42--- 56
	;
run;

proc print;
run;

Which gives me this in the results window:

 


Input dbaseInput dbase

Table below shows what I want to happen with the database:

Output dbaseOutput dbase

I tried to Google and I'm confused whether I should use tabulate or sql or what. (I also don't know SQL by the way.)

Any help would be greatly appreciated.

 

Regards,

Tony

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

Do like this

 

data have;
	input zip $ branch $ count;

	datalines;
	501 52--- 1
	501 81--- 2
	501 99--- 1
	101 22--- 2
	101 23--- 48
	101 31--- 40
	101 42--- 56
	;
run;

proc sql;
   create table want as
   select *
         ,sum(count) as Count_Subtotals
         ,count/ calculated Count_Subtotals as Percent_Count format=percent.
   from have
   group by zip;
quit;

View solution in original post

4 REPLIES 4
PeterClemmensen
Tourmaline | Level 20

Do like this

 

data have;
	input zip $ branch $ count;

	datalines;
	501 52--- 1
	501 81--- 2
	501 99--- 1
	101 22--- 2
	101 23--- 48
	101 31--- 40
	101 42--- 56
	;
run;

proc sql;
   create table want as
   select *
         ,sum(count) as Count_Subtotals
         ,count/ calculated Count_Subtotals as Percent_Count format=percent.
   from have
   group by zip;
quit;
Tony5
Fluorite | Level 6

Thanks for the quick response draycut!

 

The code worked flawlessly. I'm super happy to see that the code is simpler than I expected.

 

Curious question though. Just in case I have multiple 'count' columns in another database, can I just do this? (see code)

 

proc sql;
   create table want as
   select *
         ,sum(count) as Count_Subtotals
         ,count/ calculated Count_Subtotals as Percent_Count format=percent.
         ,sum(count_2) as Count_Subtotals_2
         ,count/ calculated Count_Subtotals_2 as Percent_Count_2 format=percent.
   from have
   group by zip;
quit;

 

Or do I need to repeat the proc sql blocks per column?

PeterClemmensen
Tourmaline | Level 20

No problem, glad to help 🙂

 

If the count colums are in different data sets, you can not.

 

If they are, you can use the same code

Tony5
Fluorite | Level 6
Yes, they're just 2 columns in the same database. Awesome. Thanks again draycut.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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