Data visualization with SAS programming

How to use Proc sgplot to generate multiple lines in one graph?

Accepted Solution Solved
Reply
Contributor
Posts: 70
Accepted Solution

How to use Proc sgplot to generate multiple lines in one graph?

I have no problem to generate one line using Proc sgplot like this:

data graphdata;

  input month $ store $ rate;

  datalines;

  Jan store1 20

   Feb stroe1 25

  ;

run;

proc sgplot data=graphdata;

   series x=month y=rate/markers

                 break

                 lineattrs=(color=blue)

                 markerattrs=(symbol=circlefilled color=blue)

                 legendlabel='rates for store1';

   xaxis type=discrete grid label=' ';

   yaxis label= 'rate' grid values=(0 to 60 by 10);

run;

Above code generate a line for store1, with xaxis having month and yaxis with rates.

However, I have problem to try to generate multiple for multiple stores when my dataset has data for more than one stores, E.g. if my data is like this:

data graphdata;

  input month $ store $ rate;

  datalines;

  Jan store1 20

  Feb store1 25 

  Jan store2 30

  Feb store2 40 

  Jan store3 55

  Feb store3 45

  ;

run;

Thanks a lot in advance.


Accepted Solutions
Solution
‎01-12-2012 02:06 PM
SAS Super FREQ
Posts: 1,044

How to use Proc sgplot to generate multiple lines in one graph?

Use:  series x=month y=rate / group=store;

View solution in original post


All Replies
SAS Employee
Posts: 11

How to use Proc sgplot to generate multiple lines in one graph?

What if you use a group variable on your series plot statement.

Series x=month y=rate group=store / ...

Solution
‎01-12-2012 02:06 PM
SAS Super FREQ
Posts: 1,044

How to use Proc sgplot to generate multiple lines in one graph?

Use:  series x=month y=rate / group=store;

Contributor
Posts: 70

Re: How to use Proc sgplot to generate multiple lines in one graph?

Excellent! Thanks Sanjay. I had thought I need to add multiple series statement somehow.

BTW, this way the SAS will automatically assign the graph bagroud color, line types and colors. How can I customize them, such as changing the background color as white, and use only solid line? I tried goptions cback=white, but it didn't seems work.

Thanks

SAS Super FREQ
Posts: 1,044

Re: How to use Proc sgplot to generate multiple lines in one graph?

If your data is grouped (as it is, with different store names), then the group attributes come from the style.  Each series will use different attributes like color, pattern from GraphData1 - GraphDataN.  You can force all lines to be solid by setting the lineattrs option:

series x=month y=rate / group=store lineattrs=(pattern=solid);

Note, the attribute change is an override, so all the lines will now have solid pattern.  To change the colors, you will have to change the settings in the style, unless you have SAS 9.3.  In SAS 9.3, you can use an ATTRMAP to assign custom colors by store name.

If you have multiple response type data structure, where each store has its own column (below), then you will use multiple SERIES plot statements, one for each series.  In this case, you can assign the attributes for each series independently

data graphdata;
input month $ store1 store2 store3;
datalines;
Jan 20 25 30
Feb 20 35 40
;
run;

proc sgplot data=graphdata nocycleattrs;
  series x=month y=store1 / lineattrs=(color=blue);
  series x=month y=store2 / lineattrs=(color=red);
  run;
Post a Question
Discussion Stats
  • 4 replies
  • 9082 views
  • 7 likes
  • 3 in conversation