Hi, I am using PROC SGPLOT in SAS 9.4 to plot data for weekdays/non-holidays such that points are shown consecutively (ie, excluding weekends and holidays) using a DISCRETE axis. What I would like to do is show the first day of whatever my interval is (eg, first day of a month), but these will not be evenly spaced. Ideally, I would simply use a separate variable as an XAXIS label, but I don't see a way to do this. (Can this be done?)
The strategy that I've employed* is to use a dummy secondary x-axis for labels, and a leave the primary x-axis as blank. The problem I'm having is that when I use the VALUES option on then XAXIS that I want to display (which are not evenly distributed), it shows the correct text, but the XAXIS appears to space the tickmarks evenly, so that the tickmarks do not line up with the where the points should be displayed.
(*Note: I posted an earlier related question to stackexchange, in which a kind user gave the suggestion that I use a second blank xaxis and the values option, which has led me to my current problem; this user recommended that I post here.)
Below is code that illustrates this. I'll quickly describe each step:
(1) inputs raw data with a few months of date/price data
(2) creates month and week variables which are used to create labels
(3) gets a unique list of the labels to use
(4) uses the %ARRAY and %DO_OVER macros to get the VALUES I want to use in the XAXIS (I've attached these in case you're not familiar with them, and there is a description of them here)
(5) plots the data with PROC SGPLOT; for this purpose I've plotted both a SERIES and a SCATTER, which should overlap if plotted correctly; the SERIES is plotted on the hidden XAXIS, and the SCATTER is plotted on displayed XAXIS2
Running this code generates the plot below: the SERIES is plotted correctly (on the hidden axis), but the SCATTER is not correct as the tickmarks on the x-axis appear to be spread out evenly across the axis, rather than at the points specified by the VALUES option in the XAXIS2 statement, even though the labels display the correct text. For example, the first tick interval is much shorter than the latter ones, so the value for 1OCT should be moved to the left such that the point appears on the series line, but instead it appears be evenly spaced across the x-axis.
I've tried a bunch of things, like including/excluding missing values from the different variables, but I can't figure out how to get these to line up properly. Any suggestions would be greatly appreciated. Thank you!
*(1) Input raw data;
data rawdata;
informat date date10.;
format date date9.;
input Obs Date price;
datalines;
1 22SEP2015 12.36
2 23SEP2015 12.24
3 24SEP2015 12.38
4 25SEP2015 12.24
5 28SEP2015 12.38
6 29SEP2015 11.99
7 30SEP2015 12.19
8 01OCT2015 12.31
9 02OCT2015 12.19
10 05OCT2015 12.2
11 06OCT2015 12.12
12 07OCT2015 12.22
13 08OCT2015 12.3
14 09OCT2015 12.69
15 12OCT2015 12.88
16 13OCT2015 12.76
17 14OCT2015 12.78
18 15OCT2015 12.79
19 16OCT2015 13.04
20 19OCT2015 12.94
21 20OCT2015 12.88
22 21OCT2015 13.16
23 22OCT2015 13.09
24 23OCT2015 13.3
25 26OCT2015 13.36
26 27OCT2015 13.24
27 28OCT2015 13.16
28 29OCT2015 13.17
29 30OCT2015 13.11
30 02NOV2015 12.95
31 03NOV2015 13.1
32 04NOV2015 13.12
33 05NOV2015 12.95
34 06NOV2015 13
35 09NOV2015 13.25
36 10NOV2015 12.87
37 11NOV2015 13
38 12NOV2015 13.04
39 13NOV2015 12.85
40 16NOV2015 12.74
41 17NOV2015 13.03
42 18NOV2015 13.17
43 19NOV2015 13.38
44 20NOV2015 13.41
45 23NOV2015 13.53
46 24NOV2015 13.53
47 25NOV2015 13.41
48 26NOV2015 13.5
49 27NOV2015 13.47
50 30NOV2015 13.39
51 01DEC2015 13.72
;
run;
*(2) Create time intervals;
data rawdata1;
set rawdata;
year=year(date);
month=year*100+month(date);
week=year*100+week(date);
run;
*(3) Get labels to display based on selected time interval;
%let timeint=month;
data rawdata2;
set rawdata1;
by &timeint;
retain xlab;
if first.&timeint then
xlab=put(date,date5.);
run;
proc sort data=rawdata2 (keep=obs xlab) out=xlab nodupkey;
by xlab;
run;
proc sort data=xlab;
by obs;
run;
*(4) Put selected values in to macro variables using ARRAY;
options nosymbolgen nomprint;
%array(xval,data=xlab,var=obs);
%array(xlab,data=xlab,var=xlab);
%put %do_over(xval,phrase=?);
%put %do_over(xlab,phrase=?);
options mprint;
*(5) Plot series and scatter, using DO_OVER macro to specify VALUES;
proc sgplot data=rawdata2;
scatter x=obs y=price / datalabel=price;
series x=date y=price / x2axis;
xaxis values=(%do_over(xval,phrase="?")) valuesdisplay=(%do_over(xlab,phrase="?")) type=discrete discreteorder=data;
x2axis display=none type=discrete;
run;
... View more