BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
adejames2000
Calcite | Level 5

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!

Series without regression line.png

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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;

View solution in original post

2 REPLIES 2
ballardw
Super User

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;
adejames2000
Calcite | Level 5

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

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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