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

I've look at several guides across the internet and can't figure this out so I'm posting here.

 

I'm creating a basic scatter plot, however the ticks are discrete rather than continuous, and I can find no way to change this.

 

The first set of code produces the graph below: 

proc sgplot data=accidents noautolegend;
scatter x=year y=age_adjusted_rate  /markerattrs=(color=red symbol=circlefilled);
label age_adjusted_rate="Car Accident Death Rate";

run;

SGPlot15.png

As you can see, SAS is automatically using the discrete values from the data for both the X and Y axes rather than continuous integers. Also why would SAS put the Y axis in descending order?!?

 

I changed the code to the following:

proc sgplot data=accidents noautolegend;
scatter x=year y=age_adjusted_rate  /markerattrs=(color=red symbol=circlefilled);
label age_adjusted_rate="Car Accident Death Rate";

xaxis grid values=(1999 to 2016 by 5);
yaxis grid values=(2 to 8 by 1);
run;

Unfortunately while this changed my axes to the values I want, SAS is still using discrete values. This code produces the graph below. 

SGPlot21.png

How do I fix this? I tried playing around in ODS Graphic Designer forever and couldn't find any options. 

1 ACCEPTED SOLUTION

Accepted Solutions
Rick_SAS
SAS Super FREQ

Any chance that you read the data from an excel spreadsheet and the data are actually strings instead of numbers? Run

 

proc contents data=accidents;

run;

 

and look at the Type column in the table "Alphabetic List of Variables and Attributes."

View solution in original post

6 REPLIES 6
ballardw
Super User

What format is assigned to your age_adjusted_rate variable? By default SAS will attempt to use that format if it will fit and the axis has space, so if you have a format like F15.12 that could explain the decimals since you apparently have few records involved.

 

When odd things happen, i.e. you ask questions about them, it is best to copy the procedure code plus any comments from LOG and paste the result into a code box opened with the forum's </> icon to preserve formatting.

 

You might not believe how many times we find people posting code that they say didn't do what was wanted that will not even run. With the LOG we can see if SAS complained about something.

Best if the data is not sensitive is to provide a data step that duplicates the data you are plotting.

Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the <> icon or attached as text to show exactly what you have and that we can test code against.

Only include the variables you are using for the graph.

Rick_SAS
SAS Super FREQ

Any chance that you read the data from an excel spreadsheet and the data are actually strings instead of numbers? Run

 

proc contents data=accidents;

run;

 

and look at the Type column in the table "Alphabetic List of Variables and Attributes."

ballardw
Super User

I think @Rick_SAS has a bit part of it.

 

It seems that when the Yaxis variable is character the order it is encountered determines which goes at the first axis value.

A small example of data a code showing this behavior:

data example;
   input x y $;
datalines;
3  3.45
1  1.234
2  2.111
;

proc sgplot data=example;
   scatter x=x y=y;
run;

proc sort data=example;
   by y;
run;

proc sgplot data=example;
   scatter x=x y=y;
run;

The likely solution will be either reread your data from the source or use a data step and the input function to create a numeric value from current value.

 

 

culliso3
Fluorite | Level 6

Thank you! This was exactly the issue. A line of data was provided in the source data for "overall" which used a value of "." under Age_Adjusted_Rate. This caused SAS to recognize that variable type as character rather than number. Deleting this line of data fixed my issue.

 

culliso3
Fluorite | Level 6

You were exactly right. For some reason the "Age_Adjusted_Rate"  variable type is character rather than number. I downloaded a similar data set from the same source and ran the proc contents and got that the variable is number. When running the scatter plot for this data it worked perfectly. 

 

Opening the excel spreadsheet I found the website source includes a line of "overall" data. Deleting this line and re-running allowed SAS to recognize the variables as number rather than character. 

 

Thank you so much for your help!

JeffMeyers
Barite | Level 11

This might be a silly question, but what happens when you put TYPE=LINEAR into your XAXIS and YAXIS statements?  This might tell you if your variables are character by mistake.  If they are then you need to convert them to numeric to use a continuous axis.

 

Any Example of this:

 

data blah;
set accidents;
   age_adjusted_rate_n=input(age_adjusted_rate,12.);
   year_n=input(year,4.);
run;

Then use the numeric versions in your graph instead.

 

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 6 replies
  • 4728 views
  • 4 likes
  • 4 in conversation