output results as a fraction

Reply
Contributor
Posts: 21

output results as a fraction

I am tasked with outputting results for a monetary amount in a fraction format: numerator/denominator.

 

If my denominator is my total sample and my numerator is flag=1, my output needs to look like the following:

 

total monetary amount when flag=1/total monetary amount for total sample

 

I am not sure where to begin. Any ideas would be appreciated!!

 

 

Respected Advisor
Posts: 2,823

Re: output results as a fraction

You can use the formats WORDF. or FRACT.

--
Paige Miller
Super User
Posts: 23,275

Re: output results as a fraction

And if you can't figure it out using the formats, show some example data and what you want to see.

 

 

Contributor
Posts: 21

Re: output results as a fraction

Thank you! ok, this is an example of the data and what I would like to see as the result.

 

data:    
type cost flag
a 20 1
a 30 0
a 40 0
b 10 1
b 15 1
b 70 0
     
     
results:    
a 20/90  
b 25/95  
Respected Advisor
Posts: 2,823

Re: output results as a fraction

[ Edited ]

PROC SUMMARY will provide you with the SUM in each value of TYPE. It can also sum the flagged values by each value of TYPE.

 

Then you can merge the results of the two PROC SUMMARY, and produce the output 20/90. You wouldn't even need a FORMAT, you can do it in a DATA step.

 

As a general rule, you would be wise to show your data in the original problem statement instead of providing later in the thread.

--
Paige Miller
Super User
Posts: 23,275

Re: output results as a fraction

Here's an example that uses SQL

 

data have;
input type $ cost flag;
cards;
a	20	1
a	30	0
a	40	0
b	10	1
b	15	1
b	70	0
;
run;

proc sql;
create table want as
select type, sum(cost*flag) as numerator, sum(cost) as denominator, 
    CALCULATED NUMERATOR/ CALCULATED DENOMINATOR AS FRAC FORMAT=FRACT.,
    CATX("/", put(calculated numerator, 8.), put(calculated denominator, 8.)) as frac_text
from have
group by type;
quit;
Ask a Question
Discussion stats
  • 5 replies
  • 164 views
  • 1 like
  • 3 in conversation