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.
Please help, thank you!
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;
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;
I previously only sorted "by monthyear", sorting by "by period monthyear" does the trick. Thank you very much.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.