DATA Step, Macro, Functions and more

How to format the ratio and create percent variable?

Accepted Solution Solved
Reply
Super Contributor
Posts: 398
Accepted Solution

How to format the ratio and create percent variable?

Hello:

I have the following codes.  I would like to format the ratio shown as .12345 and create percent variable as 12.35%.  Please advice how to do it.  Thanks.

 

data poverty;

set poverty;

BelowPoverty=Under50+Under50to99;

Ratio=belowpoverty/total;

run;

 


Accepted Solutions
Solution
‎10-05-2017 02:39 PM
Super User
Posts: 6,935

Re: How to format the ratio and create percent variable?

format ratio 7.5;

 

That would actually get you 0.12345, not .12345.  But eliminating the leading zero is trouble, and not worth the effort.

 

To create a PERCENT variable:

 

percent = put(ratio, percent9.2);

 

The width needs to be 9, because the percent formats automatically include a leading and a trailing blank (and you may have to be able to express 100%).  Because of the leading blank, you may want to apply this statement as well:

 

percent = left(percent);

 

Note that PERCENT will be a character field, not something that you can use to perform math.

View solution in original post


All Replies
Super Contributor
Super Contributor
Posts: 269

Re: How to format the ratio and create percent variable?

    format ratio percent8.2;

Super User
Posts: 24,026

Re: How to format the ratio and create percent variable?

Solution
‎10-05-2017 02:39 PM
Super User
Posts: 6,935

Re: How to format the ratio and create percent variable?

format ratio 7.5;

 

That would actually get you 0.12345, not .12345.  But eliminating the leading zero is trouble, and not worth the effort.

 

To create a PERCENT variable:

 

percent = put(ratio, percent9.2);

 

The width needs to be 9, because the percent formats automatically include a leading and a trailing blank (and you may have to be able to express 100%).  Because of the leading blank, you may want to apply this statement as well:

 

percent = left(percent);

 

Note that PERCENT will be a character field, not something that you can use to perform math.

Super Contributor
Posts: 398

Re: How to format the ratio and create percent variable?

Thank you so much for your great help!

☑ This topic is solved.

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

Discussion stats
  • 4 replies
  • 261 views
  • 3 likes
  • 4 in conversation