turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- SAS/GRAPH and ODS Graphics
- /
- "Broken" reference line (different lines for diffe...

Topic Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

10-23-2015 10:52 AM

How can I draw a "broken" reference line, where there are different reference values for different sub-periods? For example:

**data** have;

input year x1 ref_val;

datalines;

2000 15 12

2001 20 12

2002 14 12

2003 14 20

2004 20 20

2005 40 20

;

**run**;

I've tried this code:

**proc** **sgplot** data=have;

series x=year y=x1;

refline ref_val/axis=y;

yaxis min=**0**;

**run**;

but this gives me two paralel lines for all observations, while I want the reference line to be drawn at 12 only for the first three observations and then at 20 only for the last three ones.

Combining two series produces something closer to what I want but not exactly - there is still an unneccesary line joining 12 and 20:

**proc** **sgplot** data=have;

series x=year y=x1;

series x=year y=ref_val;

yaxis min=**0**;

**run**;

Accepted Solutions

Solution

10-26-2015
10:43 AM

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to slangan

10-23-2015 12:09 PM

You can use this annotate dataset and get the results.

**data** annoLine;

retain drawspace "datavalue" linecolor "blue";

input function $ x1 y1 x2 y2;

datalines;

line 2000 12 2002 12

line 2003 20 2005 20

;

**run**;

**proc** **sgplot** data=have sganno=annoLine;

series x=year y=x1;

yaxis min=**0**;

**run**;

All Replies

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to chris2377

10-23-2015 11:22 AM

chris2377 wrote:

I want the reference line to be drawn at 12 only for the first three observations and then at 20 only for the last three ones.

If you only have a few observations, you might consider drawing 2 lines by annotating the graph

Solution

10-26-2015
10:43 AM

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to slangan

10-23-2015 12:09 PM

You can use this annotate dataset and get the results.

**data** annoLine;

retain drawspace "datavalue" linecolor "blue";

input function $ x1 y1 x2 y2;

datalines;

line 2000 12 2002 12

line 2003 20 2005 20

;

**run**;

**proc** **sgplot** data=have sganno=annoLine;

series x=year y=x1;

yaxis min=**0**;

**run**;

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to slangan

10-26-2015 10:44 AM

Thanks. I've tried to avoid annotate but I see it's the best option

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to chris2377

10-23-2015 12:02 PM

Annotate or some extra data manipulation. This may be close to what I think you're attempting:

```
data have;
input year year1 year2 x1 ref_val;
datalines;
2000 . . 15 .
2001 . . 20 .
2002 . . 14 .
2003 . . 14 .
2004 . . 20 .
2005 . . 40 .
. 2000 . . 12
. 2001 . . 12
. 2002 . . 12
. . 2003 . 20
. . 2004 . 20
. . 2005 . 20
;
run;
proc sgplot data=have;
series x=year y=x1;
series x=year1 y=ref_val;
series x=year2 y=ref_val;
yaxis min=0;
run;
```

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to ballardw

10-26-2015 10:52 AM

Thanks. Data manipulation is a nice alternative to annotate. Following the idea from your post, I've come up with a bit simpler code:

**data** have;

set have;

if year<**2003** then refline1=**12**;

if year>**2002** then refline2=**20**;

**run**;

**proc** **sgplot** data=have;

series x=year y=x1;

series x=year y=refline1;

series x=year y=refline2;

yaxis min=**0**;

**run**;

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to chris2377

10-23-2015 12:38 PM

SAS 9.4 SGPLOT includes the DROPLINE statement that is designed for just such a use case. Also, for discrere axes, you can use the DiscreteOffset feature to extend the line half way between categories.

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Sanjay_SAS

10-26-2015 10:41 AM

Thanks for the suggestion, but dropline still produces a line at 20 for all observations:

**proc** **sgplot** data=have;

series x=year y=x1;

yaxis min=**0**;

dropline x=year y=ref_val/dropto=y;

**run**;

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to chris2377

10-26-2015 10:54 AM - edited 10-26-2015 10:58 AM

See codee below. I have added specific values for the DropLine, but they can come from data.

Note, one of the dropto has to be to the Y2 axis, so there needs to be a Y2 axis. The 2nd series plot is added to create the Y2 axis, but the line thickness is set to zero. Y and Y2 have same extents.

data have;

input year x1 ref_val;

datalines;

2000 15 12

2001 20 12

2002 14 12

2003 14 20

2004 20 20

2005 40 20

;

run;

proc sgplot data=have;

series x=year y=x1;

refline ref_val/axis=y;

yaxis min=0;

run;

proc sgplot data=have;

series x=year y=x1;

series x=year y=x1 / y2axis lineattrs=(thickness=0);

dropline x=2002.5 y=12 / dropto=y;

dropline x=2002.5 y=20 / dropto=y y2axis;

yaxis min=0;

y2axis min=0;

run;

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Sanjay_SAS

10-26-2015 12:14 PM

Thanks a lot. A bit trickier than I thought but gives what I wanted.

To sum up for future readers - 3 possible ways of dealing with the issue:

Solution I - dropline

**proc** **sgplot** data=have;

series x=year y=x1;

series x=year y=x1 / y2axis lineattrs=(thickness=**0**);

dropline x=**2002.5** y=**12** / dropto=y;

dropline x=**2002.5** y=**20** / dropto=y y2axis;

yaxis min=**0**;

y2axis min=**0**;

**run**;

Solution II - data manipulation

**data** have;

set have;

if year<**2003** then refline1=**12**;

if year>**2002** then refline2=**20**;

**run**;

**proc** **sgplot** data=have;

series x=year y=x1;

series x=year y=refline1;

series x=year y=refline2;

yaxis min=**0**;

**run**;

Solution III - annotate

**data** annoLine;

retain drawspace "datavalue" linecolor "blue";

input function $ x1 y1 x2 y2;

datalines;

line 2000 12 2002 12

line 2003 20 2005 20

;

**run**;

**proc** **sgplot** data=have sganno=annoLine;

series x=year y=x1;

yaxis min=**0**;

**run**;