## Percentage calculation

Solved
Super Contributor
Posts: 625

# Percentage calculation

I've a datasets like this.

Have 1:

ticket_count  Resolved_by
16                           L2
5                             L3

Have 2:

total_ticket_count
21

I want the output as follows:

ticket_count  Resolved_by Total_ticket_count Percentage_of _tickets_resolved
16                  L2                  21                         76.16%
5                    L3                  21                         23.80%

Appreciate if someone of you help me calculating 'Percentage_of _tickets_resolved'.

Accepted Solutions
Solution
‎07-25-2017 11:12 AM
Super User
Posts: 6,773

## Re: Percentage calculation

Here's code:  tested and producing the proper result.

data have1;

input ticket_count resolved_by \$;

cards;

16 L2

5 L3

;

data have2;

total_ticket_count=21;

run;

data want;

if _n_=1 then set have2;

set have1;

percentage_of_tickets_resolved = ticket_count / total_ticket_count;

format percentage_of_tickets_resolved percent9.2;

run;

proc print;

run;

All Replies
Posts: 3,016

PROC FREQ

--
Paige Miller
Super User
Posts: 9,599

## Re: Percentage calculation

Not tested as not typing that in:

data want;
merge have1 have2;
percent=(ticket_count / total_ticket_count) * 100;
format percent percent8.2;
run;
Super Contributor
Posts: 625

## Re: Percentage calculation

I've ran the similar code already and it is not producing the desired perecentage. Code below is producing the output as follows.

total_ticket_count Resolved_by total_ticket_counts percentage
16                          L2                 21                           13125%
5                            L3                 21                            525%

Super User
Posts: 9,599

## Re: Percentage calculation

Drop the * 100 then.

Super User
Posts: 6,773

## Re: Percentage calculation

To get the total ticket count to remain on all the observations coming in, try a slightly different syntax:

data want;

if _n_=1 then set have2;

set have1;

percentage_of_tickets_resolved = ticket_count / total_ticket_count;

format percentage_of_tickets_resolved percent9.2;

run;

Super Contributor
Posts: 625

## Re: Percentage calculation

Code which mentioned above is not producing the desired percentage value.

Super User
Posts: 6,773

## Re: Percentage calculation

That's true, because your original percentages are wrong.  (Did you notice that they don't add up to 100%?)  But the results match up to 1 decimal place, with the correct values generated by the program I posted.

Super Contributor
Posts: 625

## Re: Percentage calculation

Desired percentage is close to 100 (i.e. 99.6%) and it should be 100%. But I'm not getting the desired 100% value from the program which you posted.  Appreciate if could elaborate this sentence 'But the results match up to 1 decimal place, with the correct values generated by the program I posted.'

Solution
‎07-25-2017 11:12 AM
Super User
Posts: 6,773

## Re: Percentage calculation

Here's code:  tested and producing the proper result.

data have1;

input ticket_count resolved_by \$;

cards;

16 L2

5 L3

;

data have2;

total_ticket_count=21;

run;

data want;

if _n_=1 then set have2;

set have1;

percentage_of_tickets_resolved = ticket_count / total_ticket_count;

format percentage_of_tickets_resolved percent9.2;

run;

proc print;

run;

Super User
Posts: 23,722

## Re: Percentage calculation

1. Do you actually need HAVE2 here? That information can be calculated from HAVE1.

2. Did you try a 'plain jane' PROC FREQ? That works fine for me.

☑ This topic is solved.