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;
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;
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;
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
Did the code above not work? Why do you feel you need to use the character date in your regression?
Sorry, Yes it works. Thanks.
I forgot to change one thing in my old code.
Josh
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.