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

Hello there !

I cannot seem to fix a broken scatter plot. For an unknown reason, the points on the graph are going up (instead of up and down).

 

This is the code I am running

proc sgplot data=cardata;
scatter x=year y=age_adjusted_rate;
run;

 

Screen Shot 2020-05-21 at 17.46.26.png

Any ideas why?

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Do this:

data cardata;
infile reffile dlm=',' dsd truncover;
input
  Sex :$15.
  Race :$15.
  State :$20.
  Ethnicity :$10.
  Age_Group :$15.
  First_Year
  Last_Year
  Cause_of_Death :$50.
  Year
  Deaths
  Population
  Crude Rate
  Age_Adjusted_Rate
;
run;

to read the numbers as numbers.

View solution in original post

17 REPLIES 17
Reeza
Super User

Show your log. 

Is there a format applied to the library?

 

What happens if you plot the following:

proc sgplot data=sashelp.class;
scatter x=height y=weight;
run;

@AbhilashS wrote:

Hello there !

I cannot seem to fix a broken scatter plot. For an unknown reason, the points on the graph are going up (instead of up and down).

 

This is the code I am running

proc sgplot data=cardata;
scatter x=year y=age_adjusted_rate;
run;

 

Screen Shot 2020-05-21 at 17.46.26.png

Any ideas why?


 

AbhilashS
Fluorite | Level 6

Hi Reeza

Here is the log from the code I ran

Screen Shot 2020-05-21 at 18.08.55.png

 

I ran the code you suggested and the plot worked. What does that tell you?

Screen Shot 2020-05-21 at 18.09.31.png

Reeza
Super User

It tells me the issue is not with SAS but with your data, so that's when you know to check the data types and formats. 

 

You can convert a character variable to a numeric as follows though you should really back up and ensure you read it in properly from the start. 

 

data temp;
set have;

age_adjusted_rate_num = input(age_adjusted_rate, 12.1);

run;

proc sgplot data=temp;
scatter x=year y=age_adjusted_rate_num;
run;
Rick_SAS
SAS Super FREQ

Perhaps your Y variable is probably character instead of numeric? That can happen if you (mis)read the data from an Excel spreadsheet.

 

To check, run


proc contents data=cardata;

run;

 

and look at the type of the Age_Adjusted_Rate variable.

AbhilashS
Fluorite | Level 6

Hi Rick

Yes you are right. It is a CHARACTER. Is there a way to change them to NUMERIC?

 

Screen Shot 2020-05-21 at 18.16.51.png

 

AbhilashS
Fluorite | Level 6
Hi Kurt
It is a CSV file downloaded from a public database.
Thank you for those articles. I am going through them now.
Kurt_Bremser
Super User

Then you should give us an example of the csv file. Either copy/paste a few lines into a window opened with the </> button (open the file with an editor, not with Excel!), or attach it to a post.

AbhilashS
Fluorite | Level 6
FILENAME REFFILE '/folders/myfolders/sas_datasets/results.csv';

PROC IMPORT DATAFILE=REFFILE
	DBMS=CSV
	OUT=WORK.cardata
	replace;
RUN;

Thanks Kurt.

I have attached both the CSV file I am using and the code I ran.

Kurt_Bremser
Super User

Do this:

data cardata;
infile reffile dlm=',' dsd truncover;
input
  Sex :$15.
  Race :$15.
  State :$20.
  Ethnicity :$10.
  Age_Group :$15.
  First_Year
  Last_Year
  Cause_of_Death :$50.
  Year
  Deaths
  Population
  Crude Rate
  Age_Adjusted_Rate
;
run;

to read the numbers as numbers.

kristinsainani
Calcite | Level 5

A simple trick to change this to a numeric variable is to add zero:

data NEW;

set OLD;

rate2=age_adjusted_rate+0;

run;

ballardw
Super User

Exactly the same as https://communities.sas.com/t5/Graphics-Programming/Change-Scatter-Plot-Axis-Values/m-p/648398#M1990...

 

This appears with two different people posting such to be homework. Bring to the instructor's attention that the data file is poorly structured CSV for Proc Import to read. If all of the values are quoted then Proc Import will think the data is character: Example rows.

"Sex","Race","State","Ethnicity","Age Group","First Year","Last Year","Cause of Death","Year","Deaths","Population","Crude Rate","Age-Adjusted Rate"
"Both Sexes","All Races","United States","Both","All Ages","1999","2016","Unintentional MV Traffic, Occupant","1999","18326","279040238","6.57","6.55966083710955"
"Both Sexes","All Races","United States","Both","All Ages","1999","2016","Unintentional MV Traffic, Occupant","2000","18649","282171936","6.61","6.59229940420289"
AbhilashS
Fluorite | Level 6

Hi Ballardw

Thank you for pointing this out. This is actually a self-guided statistics course with this data specifically being generated from the CDC website. There is no live instructor but I will try and email your points to the course creator. It has been a challenge as the tutorials do not anticipate problems like the one I have had so I am quite thankful to the help I am getting. 

AbhilashS
Fluorite | Level 6

Thank you Reeza and KurtB for your patience and help.

Eureka !

Screen Shot 2020-05-22 at 09.52.44.png

 

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!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 17 replies
  • 1768 views
  • 5 likes
  • 6 in conversation