BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
L_L
Calcite | Level 5 L_L
Calcite | Level 5

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

1 ACCEPTED SOLUTION

Accepted Solutions
GraphGuy
Meteorite | Level 14

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=6pt;)

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

8 REPLIES 8
MikeZdeb
Rhodochrosite | Level 12

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.

DanH_sas
SAS Super FREQ

The SG procedure code to create a histogram:

proc sgplot data=sashelp.class;

histogram weight;

run;

L_L
Calcite | Level 5 L_L
Calcite | Level 5

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

GraphGuy
Meteorite | Level 14

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;

L_L
Calcite | Level 5 L_L
Calcite | Level 5

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

GraphGuy
Meteorite | Level 14

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=6pt;)

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.

art297
Opal | Level 21

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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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