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

Showing results for

- Home
- /
- Programming
- /
- Graphics
- /
- Segmented regression plot or interrupted time series

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Posted 03-07-2016 09:27 AM
(3584 views)

Hi!

I have similar data (just a small part of it):

Rate time

7.5 1

7.4 2

7.8 3

7.2 4

8.0 5

7.9 6

8.2 7

I han information that there was a change in policy and it hapend when time=4. I want to plot in one graph rate*time and regression lines (before and after policy was introduce)

How can I do it?

10 REPLIES 10

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

The way I would do it is to introduce a third column that grouped the data into "before" and "after" the policy change:

```
data rates;
length policy $ 6;
input rate time policy $;
cards;
7.5 1 before
7.4 2 before
7.8 3 before
7.2 4 before
8.0 5 after
7.9 6 after
8.2 7 after
;
run;
proc sgplot data=rates;
reg x=time y=rate / group=policy;
run;
```

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Dan has the right idea. For less coding, you can create the indicator variable without changing the data:

policy = (time < 4);

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Thanks, it does not do what i want. I would like to see something as the Figure1a in the link below

http://www.cdc.gov/pcd/issues/2013/12_0268.htm

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

You need to use a technique called "scoring the regression model" to add a new value to your data set. You want to add an observation with coordinates x=Xmax and y=(predicted value at XMax).

This will require that you fit the model in one of the regression procedures, such as PROC GLM, and output the predicted value to a SAS data set. The following code provides the main ideas:

```
data rates;
input rate time;
group = (time<4);
cards;
7.5 1
7.4 2
7.8 3
7.2 4
8.0 5
7.9 6
8.2 7
;
/* create scoring observations with
x=XMax and Y=. for the group=1 */
data Fake;
rate=.; time=7; group=1;
run;
/* merge data sets */
data All;
set rates fake;
run;
/* evaluate model; create output data set that contains the
new predicted value for XMax for group=1 */
proc glm data=All plots=fitplot;
class group;
model rate = time group;
output out=FitOut p=pred;
quit;
proc sgplot data=FitOut;
scatter x=time y=rate / group=group;
series x=time y=pred / group=group;
run;
```

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

To join the lines, they need to be in the same group.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

```
proc sgplot data=import;
reg x=date y=y_variable/group=intervention;
run;
```

Above is the code I'm using, and the goal is to show that after the intervention was implemented, there was a slope change. Essentially, the code above gave me two separate slopes: a pre intervention and a post-intervention slope, which is great. I want to connect the end of my pre-intervention slope to the start of my post-intervention slope.

Is there another code that I can use for this?

The other thing I tried doing was to use proc autoreg to generate values for "ytrend" or the "predicted trend" according to the SAS support guide on proc autoreg. When I plotted that against the date, it was able to connect the separate lines the way I want it. However, the "ytrend" lines don't fit the data points/scatter points as well as the regression lines described earlier. My question is, in time series analysis, is the "ytrend" line the same as the "regression line" - are they analogous?

I would really appreciate your expert advice as I am clearly very new to SAS graphics.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

The regression lines you are fitting by using the REG statement in PROC SGPLOT are similar to PROC GLM, not PROC AUTOREG. You can fit the model in PROC GLM, output the data, and then plot it any way you want:

```
/* generate example data */
data import;
call streaminit(1);
retain intervention 0;
do x = 1 to 100;
y = x + rand("Normal", 0, 3);
if x > 50 then do;
intervention = 1;
y = y - 20;
end;
output;
end;
proc sgplot data=import;
reg x=x y=y /group=intervention;
run;
/********************************************************/
/* use PROC GLM to make the same predictions */
proc glm data=import plots=none;
class intervention;
model y = x | intervention;
output out=ModelOut Predicted=Pred_Y;
run;
proc sort data=ModelOut; by x; run;
proc sgplot data=ModelOut;
scatter x=x y=y /group=intervention;
series x=x y=Pred_y / lineattrs=(color=black);
run;
```

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Thank you! I will try the steps you proposed.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

This right here is why you're a true SAS expert. This is exactly what I needed! and it worked. Thank you!

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

**If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. **

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.