Data visualization with SAS programming

Vertical histogram

Accepted Solution Solved
Reply
Contributor L_L
Contributor
Posts: 56
Accepted Solution

Vertical histogram

Hello everyone

I wolud like to create an histogram like the one in the excel file attached (the  input file is shown in colums B,C,D).

I'm new in using SAS graph procedures. Could anyone help me?

Thanks in advance

Kind regards


Accepted Solutions
Solution
‎07-31-2012 08:09 AM
SAS Employee
Posts: 967

Re: Vertical histogram

For the labels to fit, sometimes you have to make the bar width= wider (which also might mean you have to make the space= smaller, if you're running out of space).

And/or, you can make the text size smaller (goptions htext=6ptSmiley Wink

And I can't remember right offhand whether the format of the response variable affects the 'inside=' label - but you could try a format that shows fewer (or no) decimal places, etc.

View solution in original post


All Replies
Esteemed Advisor
Posts: 7,293

Re: Vertical histogram

Valued Guide
Posts: 763

Re: Vertical histogram

Hi ... I'm sure there are ways to do this with SG procedures, but for a neat look at how to do Excel-style graphics in SAS/Graph (including Excel colors) try graphics wizard Robert Allison's web site, specifically ...


Robert Allison's SAS/GRAPH Examples (#7)
SAS Version of 'Microsoft Excel' Charts

http://robslink.com/SAS/democd7/aaaindex.htm

There are thumbnails of output plus all the SAS code used to produce the graphics.

SAS Super FREQ
Posts: 889

Re: Vertical histogram

The SG procedure code to create a histogram:

proc sgplot data=sashelp.class;

histogram weight;

run;

Contributor L_L
Contributor
Posts: 56

Re: Vertical histogram

Thank you

I'm using the code

proc gchart data=INPUT;

    vbar YEAR / discrete

     type=sum sumvar=pctN_01

     subgroup=CAT /

     autoref clipref cref=graycc

     coutline=black

     width=8

     space=3

;

run;

In this way I have an histogram without the label data  of each categories (value of percentage) displayed. How to display percentage?

Thanks very much

SAS Employee
Posts: 967

Re: Vertical histogram

Here is some SAS code that will read in the data from the spreadsheet (assuming you have SAS/Access to PC Files), and then transpose it into a form that can be used by Gchart, and then plots the data very similarly to the bar chart in the spreadsheet:

PROC IMPORT OUT= WORK.my_data
            DATAFILE= "Ex_hist..xls"
            DBMS=EXCEL REPLACE;
     RANGE="Example$F8:I11";
     GETNAMES=YES;
     MIXED=NO;
     SCANTEXT=YES;
     USEDATE=YES;
     SCANTIME=YES;
RUN;

data my_data (rename=(F1=year)); set my_data;
run;

proc sort data=my_data out=my_data;
by year;
run;

proc transpose data=my_data out=my_data2 (rename=(col1=value _name_=category));
by year;
run;

legend1 label=none position=(right middle) across=1 shape=bar(.15in,.15in);
axis1 label=none order=(0 to 1 by .2) minor=none offset=(0,0);
axis2 label=none;

pattern1 v=s c=cx9bbb59;
pattern2 v=s c=cxc0504d;
pattern3 v=s c=cx4f81bd;

proc gchart data=my_data2;
format value percent7.0;
vbar year / discrete
type=sum sumvar=value
subgroup=category
autoref cref=graydd clipref
width=8 space=8
raxis=axis1
maxis=axis2
inside=sum
legend=legend1;
run;

Contributor L_L
Contributor
Posts: 56

Re: Vertical histogram

Thank you

I tried and the code doesn't work with the real input file (331 observations).

So I tried with the code:

proc tabulate data=input out=input2;
     class cat year;

     table cat='' all='Total', (Year='' all='Total')*(N colpctn);

run;


data input2;

     set input2;

     if _type_^='11' then delete;

     rename PCTN_01=Percentage;

run;

proc sort;by year cat;

run;

legend1 position=(right middle) across=1 shape=bar(.15in,.15in);

axis1 label=none order=(0 to 100 by 20) minor=none offset=(0,0);

axis2 label=none;

v=solid color=cx9999ff;  /* light blue */

v=solid color=cx993366;  /* purplish */

v=solid color=cxffffcc;  /* pale yellow */


title 1 "Graphic 1";
proc gchart data=input2;

vbar year/ discrete

/*type=sum*/ sumvar=percentage

subgroup=cat/* this controls the coloring */

autoref clipref cref=graycc

coutline=black

width=8

space=3

raxis=axis1

maxis=axis2

inside=sum

legend=legend1; 

run;
;

The code seems to work but in the log I find the warning:

WARNING: Some INSIDE= labels were not drawn due to space limitations.

Thanks a lot for your help

Solution
‎07-31-2012 08:09 AM
SAS Employee
Posts: 967

Re: Vertical histogram

For the labels to fit, sometimes you have to make the bar width= wider (which also might mean you have to make the space= smaller, if you're running out of space).

And/or, you can make the text size smaller (goptions htext=6ptSmiley Wink

And I can't remember right offhand whether the format of the response variable affects the 'inside=' label - but you could try a format that shows fewer (or no) decimal places, etc.

Esteemed Advisor
Posts: 7,293

Re: Vertical histogram

Alternatively, I think that the following closely approximates what you are trying to accomplish:

proc format;
  value category
  1='low'
  2='intermediate'
  3='high'
;
run;

data have;
  input Year Type;
  cards;
2010 1
2010 2
2011 1
2012 1
2012 2
2011 3
2010 3
2011 1
2011 2
2010 3
2012 1
2012 1
2012 2
;

legend1 label=none position=(right middle) across=1 shape=bar(.15in,.15in);
axis1 label=none value=none;
axis2 label=none;
pattern3 v=s c=cx9bbb59;
pattern2 v=s c=cxc0504d;
pattern1 v=s c=cx4f81bd;
proc gchart data=have;
  format Type category.;
  vbar year / discrete
       type        =  percent
       nozero
       g100
       group       =  year
       gaxis       =  axis1
       subgroup    =  type
       width       =  7
       inside     =  percent
       patternid   =  subgroup
    legend=legend1
      ;
run;
quit;

☑ This topic is SOLVED.

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

Discussion stats
  • 8 replies
  • 1073 views
  • 6 likes
  • 5 in conversation