BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
sas_user4
Obsidian | Level 7

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!

1 ACCEPTED SOLUTION

Accepted Solutions
WarrenKuhfeld
Rhodochrosite | Level 12

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

32 REPLIES 32
WarrenKuhfeld
Rhodochrosite | Level 12

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;
sas_user4
Obsidian | Level 7
Thanks Warren. Is this code correct:
x*year=income
Is this correct?


##- Please type your reply above this line. Simple formatting, no
attachments. -##
WarrenKuhfeld
Rhodochrosite | Level 12

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;
WarrenKuhfeld
Rhodochrosite | Level 12

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;





sas_user4
Obsidian | Level 7
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. -##
Rick_SAS
SAS Super FREQ

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;

 

WarrenKuhfeld
Rhodochrosite | Level 12

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.

sas_user4
Obsidian | Level 7

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?

 

WarrenKuhfeld
Rhodochrosite | Level 12

'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.

sas_user4
Obsidian | Level 7

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';

ballardw
Super User

@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)

sas_user4
Obsidian | Level 7
Warren: Thank you so much for all the info and the detailed discussion. I
also thank Rick and Sanjay for all the feedback. THANKS!
sas_user4
Obsidian | Level 7

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.

sas_user4
Obsidian | Level 7

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 32 replies
  • 3699 views
  • 7 likes
  • 5 in conversation