SGPLOT Procedure BLOCK Statement shows the following image with blocks around dots—for example, the observation for Janet is located in the middle of the second red box.
I use both SERIES and BLOCK as follows—as a working example, 12 observations from January to December 2020 are displayed. A dummy variable equals one only in February and zero otherwise. The outcome, unlike the plot above, draws the February block after the second dot.
As in the first, I want the block in the middle rather than on the right of the dot. In this case, how can I relocate the block? The following is the MWE code.
data i;
do i=1 to 12;
t=mdy(i,1,2020);
x+rannor(1);
y=i=2;
output;
end;
run;
data j;
input id :$ value fillcolor :$ filltransparency @@;
cards;
i 0 black 1 i 1 black 0.9
;
ods results=off;
ods listing gpath="!userprofile\desktop";
ods graphics/reset;
proc sgplot data=i dattrmap=j;
series x=t y=x;
block x=t block=y/attrid=i nooutline novalues;
xaxis valuesformat=yymmddn6.;
quit;
ods graphics/reset;
ods listing gpath=none;
ods results=on;
Add the points that you want to use as the boundary of the block region. Then create a binary variable that indicates whether t is in the region that you want to shade. For example, the following program uses 15Jan and 15Feb as boundary values:
%let Left = mdy(1,15,2020);
%let Right = mdy(2,15,2020);
data i;
do i=1 to 12;
t=mdy(i,1,2020);
x+rannor(1);
output;
end;
run;
/* add points at boundary of block */
data Block;
t = &Left; output;
t = &Right; output;
run;
/* merge boundary values and then sort */
data All;
merge i Block;
by t;
BlockID = (t>=&Left & t<&Right);
format t DATE9.;
run;
proc sort data=All; by t; run;
data j;
input id :$ value fillcolor :$ filltransparency @@;
cards;
i 0 black 1 i 1 black 0.9
;
proc sgplot data=All dattrmap=j;
series x=t y=x;
block x=t block=blockID /attrid=i nooutline novalues;
xaxis valuesformat=yymmddn9.;
quit;
You could make your variable a string:
T = put(mdy(I,1,2020),yymmdd.);
Add the points that you want to use as the boundary of the block region. Then create a binary variable that indicates whether t is in the region that you want to shade. For example, the following program uses 15Jan and 15Feb as boundary values:
%let Left = mdy(1,15,2020);
%let Right = mdy(2,15,2020);
data i;
do i=1 to 12;
t=mdy(i,1,2020);
x+rannor(1);
output;
end;
run;
/* add points at boundary of block */
data Block;
t = &Left; output;
t = &Right; output;
run;
/* merge boundary values and then sort */
data All;
merge i Block;
by t;
BlockID = (t>=&Left & t<&Right);
format t DATE9.;
run;
proc sort data=All; by t; run;
data j;
input id :$ value fillcolor :$ filltransparency @@;
cards;
i 0 black 1 i 1 black 0.9
;
proc sgplot data=All dattrmap=j;
series x=t y=x;
block x=t block=blockID /attrid=i nooutline novalues;
xaxis valuesformat=yymmddn9.;
quit;
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 16. Read more here about why you should contribute and what is in it for you!
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.