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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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