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!
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;
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;
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;
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;
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;
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.
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?
'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.
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
';
@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)
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.
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
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.