Data visualization with SAS programming

SAS Graphics - scatterplot, symbols, and confidence intervals

Accepted Solution Solved
Reply
Contributor
Posts: 34
Accepted Solution

SAS Graphics - scatterplot, symbols, and confidence intervals

[ Edited ]

Hi,

I am faily new to SAS. I have this dataset on lower (li) and upper income (ui) and year (y):

data new;

 input li ui year;

 datalines;

2015       1200       1360

2014       1170       1330

2013       1170       1330

2012       1140       1330

2011       1130       1320

2010       1110       1310

2009       1140       1310

2008       1120       1290

2007       1130       1300

2006       1150       1310

 ;

run;

 

I need help in writing SAS code for a scatterplot with the lower income on the vertical axis versus the year on the horizontal axis.  How can I make these observations on the plot with the character v.  On the same plot, what is the SAS code to show the upper income plotted against the year and how to mark these points with a ^. I want to use this idea to show confidence intervals or prediction intervals on a plot.

Thanks!


Accepted Solutions
Solution
‎07-28-2017 11:15 AM
SAS Super FREQ
Posts: 295

Re: SAS Graphics - scatterplot, symbols, and confidence intervals

Posted in reply to sas_user4

There are cleaner ways, but I have to run to a meeting, and this is the way I remember off of the top of my head.  Your SAS/GRAPH statements have no effect on ODS Graphics.

data new;
 input Year li ui;
 v = 'v';
 c = '^';
 datalines;
2015       1200       1360
2014       1170       1330
2013       1170       1330
2012       1140       1330
2011       1130       1320
2010       1110       1310
2009       1140       1310
2008       1120       1290
2007       1130       1300
2006       1150       1310
;

ods html body='b.html';
proc sgplot data =new noautolegend;
  scatter x=year y=li / markerchar=v;
  scatter x=year y=ui / markerchar=c;
run;
ods html close;

View solution in original post


All Replies
SAS Super FREQ
Posts: 295

Re: SAS Graphics - scatterplot, symbols, and confidence intervals

Posted in reply to sas_user4

I don't much like the idea of using a caret as one of the symbols, so I did not show that, but this makes the plot you request.  It does not work all that well with the data you have.

 

data new;
 input li ui Year;
 Income = li; type = 'Lower Income'; output;
 income = ui; type = 'Upper Income'; output;
 datalines;
2015       1200       1360
2014       1170       1330
2013       1170       1330
2012       1140       1330
2011       1130       1320
2010       1110       1310
2009       1140       1310
2008       1120       1290
2007       1130       1300
2006       1150       1310
;

proc sgplot data=new;
   reg x=year y=income / group=type cli clm;
run;
Contributor
Posts: 34

Re: SAS Graphics - scatterplot, symbols, and confidence intervals

Posted in reply to WarrenKuhfeld
Thanks Warren. Is this code correct:
x*year=income
Is this correct?


##- Please type your reply above this line. Simple formatting, no
attachments. -##
SAS Super FREQ
Posts: 295

Re: SAS Graphics - scatterplot, symbols, and confidence intervals

Posted in reply to sas_user4

I believe that is a form of syntax for GPLOT, which is part of SAS/GRAPH and has its roots in 1980s technology.  I showed you PROC SGPLOT, which is part of Base SAS and is based on modern technology.  Here are a couple more variations on the theme.

 

proc sgplot data=new;
   reg x=year y=income / group=type cli clm;
   keylegend / title=' ';
run;

proc sgplot data=new;
   reg x=year y=income / group=type cli clm nomarkers legendlabel=' ' name='a';
   scatter x=year y=income / group=type markerchar=type legendlabel=' ';
   format type $1.;
   keylegend 'a' / title=' ';
run;
SAS Super FREQ
Posts: 295

Re: SAS Graphics - scatterplot, symbols, and confidence intervals

Posted in reply to WarrenKuhfeld

Actually, your data set had the variables wrong.  This should work better.

data new;
 input Year li ui;
 Income = li; type = 'Lower Income'; output;
 income = ui; type = 'Upper Income'; output;
 datalines;
2015       1200       1360
2014       1170       1330
2013       1170       1330
2012       1140       1330
2011       1130       1320
2010       1110       1310
2009       1140       1310
2008       1120       1290
2007       1130       1300
2006       1150       1310
;

ods html body='b.html';
proc sgplot data=new;
   reg x=year y=income / group=type cli clm;
   keylegend / title=' ';
run;

proc sgplot data=new;
   reg x=year y=income / group=type cli clm nomarkers legendlabel=' ' name='a';
   scatter x=year y=income / group=type markerchar=type legendlabel=' ';
   format type $1.;
   keylegend 'a' / title=' ';
run;
ods html close;





Contributor
Posts: 34

Re: SAS Graphics - scatterplot, symbols, and confidence intervals

Posted in reply to WarrenKuhfeld
Warren:
I don't understand why when I run your code, SAS tells me there are 20
observations while there are only 10!

##- Please type your reply above this line. Simple formatting, no
attachments. -##
SAS Super FREQ
Posts: 3,753

Re: SAS Graphics - scatterplot, symbols, and confidence intervals

Posted in reply to sas_user4

If I understand what you are asking, you can use the HIGHLOW statement to create a graph that displays interval estimates. Here is an example that uses your original data:

 

data new;
 input year li ui;
 datalines;
2015       1200       1360
2014       1170       1330
2013       1170       1330
2012       1140       1330
2011       1130       1320
2010       1110       1310
2009       1140       1310
2008       1120       1290
2007       1130       1300
2006       1150       1310
 ;
run;

proc sgplot data=new;
   highlow x=year low=li high=ui / highcap=openarrow lowcap=openarrow;
run;

 

SAS Super FREQ
Posts: 295

Re: SAS Graphics - scatterplot, symbols, and confidence intervals

Rather than writing out two  income variables like you did origially, I wrote out one income variable of interest with two observations for each year.  This kind of grouped data set is often the right form for ODS Graphics, although there is often more than one way to do things.

Contributor
Posts: 34

Re: SAS Graphics - scatterplot, symbols, and confidence intervals

Posted in reply to WarrenKuhfeld

Thanks Warren. I hope you have patience but can I ask you how your code (using reg) is different than mine (x*y=z) in terms of the output?

 

SAS Super FREQ
Posts: 295

Re: SAS Graphics - scatterplot, symbols, and confidence intervals

Posted in reply to sas_user4

'x*y=z' doesn't do anything.  As before, I am guessing you are referring to  PROC GPLOT.  I have forgotten almost everything I ever knew about it and never recommend it.  As I recall, GPLOT has no mechanism for linear regression functions, but I could very well be wrong.  I was showing you how to fit regression models for each group.  Rick thought you might want something different.  Whatever you want, if it is a scatter plot, PROC SGPLOT is the procedure that you should be learning.

Contributor
Posts: 34

Re: SAS Graphics - scatterplot, symbols, and confidence intervals

Posted in reply to WarrenKuhfeld

Thanks Warren! You are correct, I use gplot:

proc gplot data=new;
title 'Scatter Plot of Year and li with ui plotted against year';
plot year*li=ui / regeqn;
run;

 

This is snapshot of the graph using gplot

 

graph.PNG';

Super User
Posts: 11,343

Re: SAS Graphics - scatterplot, symbols, and confidence intervals

Posted in reply to WarrenKuhfeld

WarrenKuhfeld wrote:

'x*y=z' doesn't do anything.  As before, I am guessing you are referring to  PROC GPLOT.  I have forgotten almost everything I ever knew about it and never recommend it.  As I recall, GPLOT has no mechanism for linear regression functions, but I could very well be wrong.  I was showing you how to fit regression models for each group.  Rick thought you might want something different.  Whatever you want, if it is a scatter plot, PROC SGPLOT is the procedure that you should be learning.


Old school graph for regression has the instructions in the SYMBOL statement with the interpolation options (which was not intuitively obvious when I learned this)

 

Symbolxx i=R with options for linear, quadratic and cubic regressions only and confidence limits of the mean or idividual values and the confidence range (50 to 99)

Contributor
Posts: 34

Re: SAS Graphics - scatterplot, symbols, and confidence intervals

Warren: Thank you so much for all the info and the detailed discussion. I
also thank Rick and Sanjay for all the feedback. THANKS!
Contributor
Posts: 34

Re: SAS Graphics - scatterplot, symbols, and confidence intervals

Rick_SAS:

Thank you, I run the code and here is the error message:

WARNING: No output destinations active.

And no graph was produced!

My question is that I need to plot lower income (Y axis) and Year (X axis) and then upper income against year in the same plot. Hope it is clearer now.

Contributor
Posts: 34

Re: SAS Graphics - scatterplot, symbols, and confidence intervals

Posted in reply to sas_user4

How about this code?

 

goptions reset = all;

symbol1 v=^ ;

symbol2 v=v;

proc sgplot data =new;

  scatter x=year y=lq ;

  scatter x=year y=uq;

run;

quit;

 

and how to produde these symbols (^) and (v)?

Thakns

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 32 replies
  • 448 views
  • 7 likes
  • 5 in conversation