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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20
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

4 REPLIES 4
novinosrin
Tourmaline | Level 20
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;
prolifious
Obsidian | Level 7

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

novinosrin
Tourmaline | Level 20

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

ballardw
Super User

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

 

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
  • 4 replies
  • 832 views
  • 1 like
  • 3 in conversation