Calcite | Level 5

## Overlay Series With Regression/Trend Lines for Different (Half) Series using PROC SGPLOT

Hello all,

I am trying to lay a regression line on each of two different (half) series covering different times of period.

``````proc sql;
create table have as
select date
, region
, sum(sale) as sale
from sashelp.pricedata
group by region, date
order by region, date
;
quit;

data have2;
set have (rename=(region=period));
if period=3 then delete;
if period=2 then monthyear=intnx('year',date,5, "sameday");
else monthyear=date;
drop date;
format monthyear monyy5.;
run;

/*CODE 1 PRODUCES SERIES AS EXPECTED*/
proc sgplot data=have2;
series x=monthyear y=sale / group=period markers;    label period="PERIOD";
run;

/*CODE 2 PRODUCES REGRESSION LINE AS EXPECTED*/
proc sgplot data=have2;
reg x=monthyear y=sale / group=period ;    label period="PERIOD";
run;

/*CODE 3 DOES NOT PRODUCES SERIES OVERLAID WITH REGRESSION LINE*/
*When I add the regression line, all the series line are all over the place;
proc sgplot data=have2;
series x=monthyear y=sale / group=period markers;* markerattrs=(color=lightgreen size=10 symbol=trianglefilled);
reg x=monthyear y=sale / group=period ;    label period="PERIOD";
run;``````

I saw a similar example here but it doesn't work for me, presumably because the periods do not overlap.

Below is the plot obtained from CODE 1, I just need to have the regression lines through EACH (half) series without distorting the series as CODE 3 does.

1 ACCEPTED SOLUTION

Accepted Solutions
Super User

## Re: Overlay Series With Regression/Trend Lines for Different (Half) Series using PROC SGPLOT

Try forcing a sort prior to the last Sgplot and see if that helps. It did on my system (Thank you for using a set we have access to).

I an guessing that without the specified sort order that mixing Reg with series brings up some internal question about "order", which is typically the cause of that type of connections in a series plot.

```proc sort data=have2;
by period monthyear;
run;

proc sgplot data=have2;
series x=monthyear y=sale / group=period markers;* markerattrs=(color=lightgreen size=10 symbol=trianglefilled);
reg x=monthyear y=sale / group=period ;
label period="PERIOD";
run;```
2 REPLIES 2
Super User

## Re: Overlay Series With Regression/Trend Lines for Different (Half) Series using PROC SGPLOT

Try forcing a sort prior to the last Sgplot and see if that helps. It did on my system (Thank you for using a set we have access to).

I an guessing that without the specified sort order that mixing Reg with series brings up some internal question about "order", which is typically the cause of that type of connections in a series plot.

```proc sort data=have2;
by period monthyear;
run;

proc sgplot data=have2;
series x=monthyear y=sale / group=period markers;* markerattrs=(color=lightgreen size=10 symbol=trianglefilled);
reg x=monthyear y=sale / group=period ;
label period="PERIOD";
run;```
Calcite | Level 5

## Re: Overlay Series With Regression/Trend Lines for Different (Half) Series using PROC SGPLOT

I previously only sorted   "by monthyear", sorting by "by period monthyear" does the trick. Thank you very much.

Discussion stats
• 2 replies
• 460 views
• 1 like
• 2 in conversation