Help using Base SAS procedures

format percentages to two decimal places

Reply
Contributor
Posts: 33

format percentages to two decimal places

HI,

I have a question regarding formatting percentages. Aftre running proc freq on one of the columns in my dataset, I get percentages as 33.33333 or 66.666666.

I have to ultimately export my data to excel so I want the percentages to show up as 33% or 33.33% which ever can be formatted easily.

Is there a way to format percentages in sas code itself?

thanks a ton

PROC Star
Posts: 7,366

format percentages to two decimal places

You didn't indicate if you were doing one way frequencies or crosstabulations, or whether you were getting your output from ods or as the result of an output statement.

If you are doing crosstabulations and using ods output files, then you could do something like:

proc format;

  picture pctfmt (round) other='009.99%';

run;

proc template;

  define crosstabs Base.Freq.CrossTabFreqs;

    cellvalue frequency percent rowpercent colpercent;

    define frequency;

      format=8.;

      header='Count';

    end;

    define percent;

      format=pctfmt.;

      header='Overall %';

    end;

    define rowpercent;

      format=pctfmt.;

      header='Row %';

    end;

    define colpercent;

      format=pctfmt.;

      header='Col %';

    end;

  end;

run;

proc freq data=sashelp.class;

  tables sex*age;

run;

Contributor
Posts: 33

format percentages to two decimal places

This looks good but too complex for me to understand. is there a way to do this is excel automatically after importing data?

Just a thought!

PROC Star
Posts: 7,366

format percentages to two decimal places

I would think that it would be more difficult trying to do it automatically in Excel.  Post the code you are running as there might be an easier solution.

The template code isn't difficult.  It is just assigning a specific format to the output.  Difficult to figure out on one's own but, fortunately, many examples can be found on the web.

Contributor
Posts: 33

Re: format percentages to two decimal places

Thanks for your time.

This the code that I am using:

proc freq data = temp noprint;

tables Q1/out=Q1;

run;

is there any formta thing that I can add in this code to formta the percentages? What would you suggest?

PROC Star
Posts: 7,366

Re: format percentages to two decimal places

There might be an easier way that I simply can't think of at the moment, but you could always just add one datastep and apply the desired format.  e.g.:

proc format;

   picture pctfmt (round) other='009.99%';

run;

 

proc freq data = sashelp.class noprint;

  tables age/out=Q1 ;

run;

 

data q1;

  set q1;

  format percent pctfmt.;

run;

Valued Guide
Posts: 2,175

format percentages to two decimal places

Nupur

there are interesting ways to have SAS direct Excel to format cells.

Many of these will be found in "ODS reporting". Here is a link to a relevant posting http://communities.sas.com/message/1820#1820

good luck

Peter       

Ask a Question
Discussion stats
  • 6 replies
  • 4184 views
  • 0 likes
  • 3 in conversation