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

I have character variables values that represent a date.  An example of a date is the following:  July 2015 is displayed as 201507. What I want to do is create a scatter plot and alter the Xaxis that shows the dates, but without altering the date, I am unable to alter the Xaxis range.  I want to create a scatter plot with a regression line using the character date.

 

Below is an example of a dataset I am using. 

 

The dataset is the average MPG in a given month for my car that I want to plot over time.  I try to plot using the character date, but I am not able to do the regression line since it is a character. 

 

Do I have to reformat the character date? Convert it to numeric?  If I convert it to numeric, my xaxis gets messed up.

 

 

Any help would be appreciated.

data mpgdate;
length chardate $10;
infile datalines;
input chardate $ avempg;
date = input(chardate,yymmn.);
return;
datalines;
201401 36
201402 24
201403 28
201404 30
201405 32
201406 33
201407 31
201408 35
201409 29
201410 28
201411 36
201412 30
201501 29
201502 27
201503 30
201504 28
201505 32
201506 31
201507 33
201508 26
201509 32
201510 34
201511 35
201512 34
;
run;
 
axis1 order=(0 to 5 by .5) minor=none;
axis2 order=(201404 to 201604 by 1);
 
 
ods listing close;
ods html file='Scatterplot.html' path='.' style=statistical;
ods graphics / reset width=600px height=400px imagename='ScatterPlot' imagefmt=gif;
title "Average MPG per Month";
 
proc sgscatter data=mpgdate;
 
plot (avempg) * (chardate) /
reg=(nogroup clm degree=2) grid ;
run;
ods html close;
ods listing;

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Convert it to a SAS date - not numeric and apply a format so it displays the way you want.

 

Format date yymmn6.; 

 

Use the date variable instead of Chardate in your proc. 

  

The axis1/axis2 apply to SAS/GRAPH procedures not SG procedures so they shouldn't be affecting your plots.

 

Is this close to what you want? I'll also move this post to the Graphing forum so some of the SAS graphing guru's can chime in.

 

data mpgdate;
informat chardate $10.;
input chardate $ avempg;
date = input(chardate,yymmn6.);
format date date9.;
datalines;
201401 36
201402 24
201403 28
201404 30
201405 32
201406 33
201407 31
201408 35
201409 29
201410 28
201411 36
201412 30
201501 29
201502 27
201503 30
201504 28
201505 32
201506 31
201507 33
201508 26
201509 32
201510 34
201511 35
201512 34
;
run;
 


title "Average MPG per Month";

proc sgscatter data=mpgdate;
 
plot (avempg) * (date) /
reg=(nogroup clm degree=2) grid ;
format date yymmn6.;
run;

 

View solution in original post

4 REPLIES 4
Reeza
Super User

Convert it to a SAS date - not numeric and apply a format so it displays the way you want.

 

Format date yymmn6.; 

 

Use the date variable instead of Chardate in your proc. 

  

The axis1/axis2 apply to SAS/GRAPH procedures not SG procedures so they shouldn't be affecting your plots.

 

Is this close to what you want? I'll also move this post to the Graphing forum so some of the SAS graphing guru's can chime in.

 

data mpgdate;
informat chardate $10.;
input chardate $ avempg;
date = input(chardate,yymmn6.);
format date date9.;
datalines;
201401 36
201402 24
201403 28
201404 30
201405 32
201406 33
201407 31
201408 35
201409 29
201410 28
201411 36
201412 30
201501 29
201502 27
201503 30
201504 28
201505 32
201506 31
201507 33
201508 26
201509 32
201510 34
201511 35
201512 34
;
run;
 


title "Average MPG per Month";

proc sgscatter data=mpgdate;
 
plot (avempg) * (date) /
reg=(nogroup clm degree=2) grid ;
format date yymmn6.;
run;

 

jprosenbaum8908
Calcite | Level 5

Thanks. As long as I can apply the regression line to a character date, then great.   But I have not been able to do that, yet.  I am not sure how to go about it yet.

 

Thanks,

 

Josh

Reeza
Super User

Did the code above not work? Why do you feel you need to use the character date in your regression?

jprosenbaum8908
Calcite | Level 5

Sorry,  Yes it works.  Thanks.

 

I forgot to change one thing in my old code.

 

Josh

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
  • 4 replies
  • 1334 views
  • 0 likes
  • 2 in conversation