Data visualization with SAS programming

Help with SAS Graph

Reply
Occasional Contributor
Posts: 13

Help with SAS Graph

Guys -

I am a complete novice to SAS Code as i use SAS EG. I was recommended to use SAS Code for the following task;

I want to create a SAS Graph with three variables; say two bars and one line, or two lines and a bar.

Can this be done using SAS Code?

Any help/advise would be appreciated,

Thanks,
Regular Contributor
Posts: 165

Re: Help with SAS Graph

SGPLOT may be a good place to look. Is this what you are trying to do?
http://support.sas.com/documentation/cdl/en/grstatproc/62603/HTML/default/a003199396.htm
SAS Super FREQ
Posts: 1,136

Re: Help with SAS Graph

Also see the programs I posted to this thread:
http://support.sas.com/forums/thread.jspa?threadID=14351&tstart=0
SAS Super FREQ
Posts: 1,136

Re: Help with SAS Graph

If you have SAS 9.2(TS2M3), you can use the ODS Graphics Designer to create your graphs without knowing anything about SAS/GRAPH. This will also work for pre M3 version, but it is pre-production.

Submit %sgdesign; in the program window to start Designer. Then, start with a graph from the gallery, and supply your data. You can add more bars or series to the graph incrementally. See this link to get started:

http://support.sas.com/resources/papers/proceedings09/331-2009.pdf
Occasional Contributor
Posts: 13

Re: Help with SAS Graph

Sanjay -

I tried using the code you have mentioned in the other thread - but since i am using SAS Enterprise Guide, I am unable to use proc template with SAS Enterprise Guide.

Is there a way i can counter this?

Thank you for your assistance,

Best Regards,
Abhinav
Occasional Contributor
Posts: 13

Re: Help with SAS Graph

Rick -

yes this is what i am trying to do in essence - but two bars and one line.

I will look over the code,

Thanks,
SAS Employee
Posts: 977

Re: Help with SAS Graph

One way to do 2 bars (aka, a "grouped bar chart") and a line is to use "proc gchart" to draw the bars, and use "annotate" to draw the line (this requires intermediate, to advanced, programming). Here's a minimal/simple example:

http://robslink.com/SAS/democd29/barline.htm

http://robslink.com/SAS/democd29/barline_info.htm
SAS Employee
Posts: 977

Re: Help with SAS Graph

You might also look into "proc gbarline" for simple bar charts (ie, not grouped/paired bars) with lines overlaid.

For both gbarline, and sgplot, you'll need a fairly recent version of SAS - I would recommend using the most recent version, as both of these fairly new procs had a lot of enhancements in 9.2.
SAS Super FREQ
Posts: 925

Re: Help with SAS Graph

When you say, "unable to use proc template in EG", what kind of problems are you seeing? Is there an template store writing issue, or is there a compile issue with the template code?

Thanks!
Dan
Occasional Contributor
Posts: 13

Re: Help with SAS Graph

It says nothing - it displays the table but will not show the graph at all.

I can not link the code to my stored process in Enterprise Guide.

I had a quick call with a SAS Customer Support Representative and he mentioned that proc template can not be used in SAS Enterprise Guide.
SAS Super FREQ
Posts: 925

Re: Help with SAS Graph

Do you see any messages in the log from the stored process? If it is a template store writing issue, I think we can work around that. If that is the problem, you will see a message to that effect in the log.
Occasional Contributor
Posts: 13

Re: Help with SAS Graph

Dan -

Thank you for the reply.

Just to give a quick background - i have a Stored process where i want to graph two variables on a graph and one on a line. When i query up the data sets, and have the ouptput data set which i want to graph in said format, i insert code in there

and it gives me the following errors; I am pasting the whole log here - apologies on the long post;

1 The SAS System 16:25 Tuesday, May 31, 2011

1 ;*';*";*/;quit;run;
2 OPTIONS PAGENO=MIN;
3 %LET _CLIENTTASKLABEL='Program';
4 %LET _CLIENTPROJECTPATH='/Libraries/SASLibraries/AceTest/edited Workquery
4 ! Ace SAS Graph.egp';
5 %LET _CLIENTPROJECTNAME='Edited edited Workquery Ace SAS Graph.egp';
6 %LET _SASPROGRAMFILE=;
7
8 ODS _ALL_ CLOSE;
9 OPTIONS DEV=ACTIVEX;
NOTE: Procedures may not support all options or statements for all devices. For details, see the
documentation for each procedure.
10 GOPTIONS XPIXELS=0 YPIXELS=0;
11 FILENAME EGSR TEMP;
12 ODS tagsets.sasreport12(ID=EGSR) FILE=EGSR STYLE=Copy_of_Analysis
12 ! STYLESHEET=(URL="file:///C:/Documents%20and%20Settings/ace/Desktop/Copy%20of%20Analysis.cs
12 ! s") NOGTITLE NOGFOOTNOTE GPATH=&sasworklocation ENCODING=UTF8 options(rolap="on");
WARNING: Style COPY_OF_ANALYSIS not found; Default style will be used instead.
NOTE: Writing TAGSETS.SASREPORT12(EGSR) Body file: EGSR
13
14 GOPTIONS ACCESSIBLE;
15
16 /* -------------------------------------------------------------------
17 Code generated by SAS Task
18
19 Generated on: Tuesday, May 31, 2011 at 3:07:11 PM
20 By task: Test graph
21
22 Input Data: WORK.QUERY_FOR_COMBINED_FILE_DUM_0015
23 Server: SASApp
24 ------------------------------------------------------------------- */
25
26 %_eg_conditional_dropds(WORK.SORTTempTableSorted);
27 /* -------------------------------------------------------------------
28 Sort data set WORK.QUERY_FOR_COMBINED_FILE_DUM_0015
29 ------------------------------------------------------------------- */
30
31


32 data WORK.QUERY_FOR_COMBINED_FILE_DUM_0015;
33 input month $ Calculation2 metric_b SUM_of_random;
34 datalines;

NOTE: The data set WORK.QUERY_FOR_COMBINED_FILE_DUM_0015 has 5 observations and 4 variables.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds

40 ;

41 run;
42 proc template;
43 define statgraph bars;
2 The SAS System 16:25 Tuesday, May 31, 2011

44 begingraph;
45 entrytitle "test three Variable Graph";
46 layout overlay / yaxisopts=(offsetmin=0);
47 barchart x=month y=Calculation2 / discreteoffset=-0.2 barwidth=0.4 fillattrs=graphdata1
47 ! name='a';
48 barchart x=month y=metric_b / discreteoffset= 0.2 barwidth=0.4 fillattrs=graphdata2 name='b';
49 seriesplot x=age y=SUM_of_random / lineattrs=graphdata3(pattern=solid thickness=4)
49 ! yaxis=y2 name='c';
50 discretelegend 'a' 'b' 'c';
51 endlayout;
52 endgraph;a
_
180
ERROR 180-322: Statement is not valid or it is used out of proper order.
53 end;
54 run;
NOTE: PROCEDURE TEMPLATE used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds

ERROR: Parse environments were left open. You may be missing an END statement.
NOTE: The SAS System stopped processing this step because of errors.
55
56 ods graphics / width=4in height=3in imagename='BarsGTL';


57 proc sgrender data=WORK.QUERY_FOR_COMBINED_FILE_DUM_0015 template=bars;
58 run;

ERROR: Unable to restore 'bars' from template store!
NOTE: The SAS System stopped processing this step because of errors.
NOTE: There were 5 observations read from the data set WORK.QUERY_FOR_COMBINED_FILE_DUM_0015.
NOTE: PROCEDURE SGRENDER used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds

59
60
61
62 GOPTIONS NOACCESSIBLE;
63 %LET _CLIENTTASKLABEL=;
64 %LET _CLIENTPROJECTPATH=;
65 %LET _CLIENTPROJECTNAME=;
66 %LET _SASPROGRAMFILE=;
67
68 ;*';*";*/;quit;run;
69 ODS _ALL_ CLOSE;
70
71
72 QUIT; RUN;
73
SAS Employee
Posts: 170

Re: Help with SAS Graph

I believe your problem is on line 52. There is a stray 'a' there.
This prevents the 'end' from being recognized on line 53.

52 endgraph;a
_
180
ERROR 180-322: Statement is not valid or it is used out of proper order.
53 end;
Occasional Contributor
Posts: 13

Re: Help with SAS Graph

Darrell -

Yeah that was it - thanks a ton for your help on this thread guys.

Thank you,
SAS Super FREQ
Posts: 925

Re: Help with SAS Graph

Try this in your stored process:

1. At the beginning, add this line: ods path (prepend) work.templat(update);
2. In the proc template code, where it say DEFINE STATGRAPH ;
add this: / store=work.templat;
3. At the end, add this line: ods path reset;

Let me know if that works for you.

Thanks!
Dan
Ask a Question
Discussion stats
  • 14 replies
  • 1294 views
  • 0 likes
  • 6 in conversation