How to handle percent (%) values in SAS

Being that 2013 is the International Year of Statistics, I wanted to make sure everyone knows how to handle my favorite statistic - percent (%) - in SAS!

I often see data in spreadsheets/csv/etc that purport to represent "percents"... but you have to be a bit careful when working with those values.  Are they the raw numeric values, or are they the formatted values?

In SAS, to represent 12.5%, you'll want to store the numeric value .125, and then apply the 'percent.' format so that it will print out (in tables, graphs, etc) as 12.5%.

Something like this...

data foo;
format y percent7.1;
y=.125;
run;

proc print data=foo;
run;

That was simple enough!... but what about a graph where you want to show the value rounded to a whole percent on the axis, but show more precision (let's say 2 decimal places) in the pointlabels for the markers in the plot???

Yes - you can do that in SAS!

I recommend using the simple format statement to control the graph's axis, and then create a special extra/temporary text variable containing the marker values, formatted to show a percent with 2 decimal places, to use as the pointlabels.

data my_data;
set my_data;
length custom_text $50;
custom_text=put(percent_value,percent8.2);
run;

symbol1 color=red value=circle height=6 pointlabel=(height=11pt color=blue "#custom_text");

proc gplot data=my_data;
format percent_value percent7.0;
plot percent_value*letter;
run;

Now the axis shows nice/rounded values (with no decimal places), and the pointlabels show 2 decimal places:)

 

 

Hopefully this simple example will help teach you the basics, so you can customize your graphs in endless ways!

Here is the complete SAS code for the above example.

 

tags: international year of statistics, percent format, sas statistics

6 Comments

  1. Dorota Jarosz
    Posted January 15, 2013 at 4:58 pm | Permalink

    Why do you need length $50 for the labels that clearly have length 8? Wouldn't length $8 save space when a large dataset is processed? Since this is a percent relative to the total, its value cannot be larger than 100%.

    Also, I got the following:
    WARNING: This CREATE TABLE statement recursively references the target table. A consequence of this is a possible data integrity problem.

    • Dorota Jarosz
      Posted January 15, 2013 at 5:00 pm | Permalink

      P.S. I know how to fix the warning, I just don't like teaching examples that create warnings.

    • Robert Allison Robert Allison
      Posted January 15, 2013 at 5:13 pm | Permalink

      When I create a text variable, I like to err on the side of making them a bit longer than needed, rather than taking a chance on making them too short. Memory is cheap these days, and that extra space can save you time & frustration down the road.

      Many times in my 20 years of programming, I have created text variables the minimal/shortest length, and then later gone back and edited the program to add some additional text to the variable, and part of the text got truncated (because the variable wasn't long enough). It often took a bit of trouble shooting to figure out what the problem was. By making the text variables a bit longer than I need them, I typically avoid this problem.

      • Dorota Jarosz
        Posted January 15, 2013 at 5:34 pm | Permalink

        This is true, I agree some room is desirable, but length $50 is an over kill here. Also, long text values do not print well in standard proc print, because SAS allocates the full length to display these short values. In order to have compact tables, I often had to assign much shorter formats for output.

        Also, I was surprised that the length $50 did not matter for the graph labels.

        • Robert Allison Robert Allison
          Posted January 15, 2013 at 8:59 pm | Permalink

          Certainly feel free to adapt the code to suit your needs! That's one of the great things about SAS - it gives each user total control over their own code :)

        • Robert Allison Robert Allison
          Posted January 15, 2013 at 9:18 pm | Permalink

          Let me give one example of why I generally declare this kind of text variable longer than the immediate need...

          I often use pointlabel text and/or html mouse-over text to do ad-hoc trouble shooting and verification while I'm creating a graph. I will often temporarily add various other variables to the text (for example there might be a person's name, city, or other identifying information in the data). If I was using the minimum length for the text variable, then I'd have to remember to temporarily increase that length (and then shorten it again). And in the case of html hover-text, if the variable is too short, part of the text and closing quote might get chopped off ... which would make the hover text for that plot marker not work. This is an example of why I like to declare such variables longer than might be needed for the immediate use.

One Trackback

  1. […] How to handle percent (%) values in SAS by Robert […]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <p> <pre lang="" line="" escaped=""> <q cite=""> <strike> <strong>