DATA Step, Macro, Functions and more

Percentage calculation

Accepted Solution Solved
Reply
Super Contributor
Posts: 426
Accepted Solution

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
a month ago
Super User
Posts: 5,093

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;

 

View solution in original post


All Replies
Trusted Advisor
Posts: 1,630

Re: Percentage calculation

PROC FREQ

Super User
Super User
Posts: 7,417

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: 426

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
Super User
Posts: 7,417

Re: Percentage calculation

Drop the * 100 then.

Super User
Posts: 5,093

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: 426

Re: Percentage calculation

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

Super User
Posts: 5,093

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: 426

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
a month ago
Super User
Posts: 5,093

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: 17,912

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.

 

delete percent proc freq.JPG

☑ This topic is solved.

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

Discussion stats
  • 10 replies
  • 294 views
  • 4 likes
  • 5 in conversation