DATA Step, Macro, Functions and more

calculate and display percentages

Accepted Solution Solved
Reply
Contributor
Posts: 24
Accepted Solution

calculate and display percentages

I'm pretty far along into a report.  Right now I have a data set that looks like this (not real data, i understand column 2 and 3 are not sorted, but it is in my program):

 

 

friendtreatscount
birdpop3
birdeggs134
birdfish12
catham8
catsoda12
dogmeat3
dogeggs4
dogfish1
dogcereal1
dogcandy1

 


And I'm trying to get an output of this:

 

friendtreatscountpercentage
birdpop32%
birdeggs13490%
birdfish128%
catham840%
catsoda1260%
dogmeat330%
dogeggs440%
dogfish110%
dogcereal110%
dogcandy110%

 

I'd post code that i have which attempts to do this, but suffice it to say, it's not worth sharing.  Any thoughts or ideas are much appreciated.  thank you.


Accepted Solutions
Solution
3 weeks ago
PROC Star
Posts: 1,603

Re: calculate and display percentages

[ Edited ]
Posted in reply to prolifious
data have;
input friend $	treats $	count;
cards;
bird	pop	3
bird	eggs	134
bird	fish	12
cat	ham	8
cat	soda	12
dog	meat	3
dog	eggs	4
dog	fish	1
dog	cereal	1
dog	candy	1
;

proc sql;
create table want as
select *, count/sum(count)  as pct format=percent10.
from have
group by friend;
quit;

View solution in original post


All Replies
Solution
3 weeks ago
PROC Star
Posts: 1,603

Re: calculate and display percentages

[ Edited ]
Posted in reply to prolifious
data have;
input friend $	treats $	count;
cards;
bird	pop	3
bird	eggs	134
bird	fish	12
cat	ham	8
cat	soda	12
dog	meat	3
dog	eggs	4
dog	fish	1
dog	cereal	1
dog	candy	1
;

proc sql;
create table want as
select *, count/sum(count)  as pct format=percent10.
from have
group by friend;
quit;
Contributor
Posts: 24

Re: calculate and display percentages

Posted in reply to novinosrin

This is the first I'll be using PROC SQL...thank you very much!!

PROC Star
Posts: 1,603

Re: calculate and display percentages

Posted in reply to prolifious

@prolifious You are welcome. Yes proc SQL is super cool Smiley Happy

Super User
Posts: 13,358

Re: calculate and display percentages

Posted in reply to prolifious

And one of the other report procedures:

proc tabulate data=have;
   class friend treats;
   freq count;
   table friend*treats,
         n='Count'  pctn<treats>='Percentage' *f=percent8.0
         ;
run;

if you do not actually need a data set

 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 128 views
  • 1 like
  • 3 in conversation