Hello,
I have defeind the refline for specific date but it was coming before the date rather than specified.
Below is my output, the refline date is 11MAR2020 , but it was displaye before Mar2020.
Below is my code.
data prefinal;
set indata;
if fasfl='Y';
cutoff=cov19pdt;
adt = FUPEDT;
if randdt > cutoff then do;
adt2=adt;
randdt2=randdt;
before='Y';
end;
else if adt < cutoff then do;
adt1=adt;
randdt1=randdt;
after='Y';
end;
else do;
randdt1=randdt;
adt1=cutoff;
randdt2=cutoff;
adt2=adt;
both='Y';
end;
format randdt randdt1 adt randdt2 adt2 cutoff date9. ;
keep usubjid randdt1 adt1 randdt2 adt2 cutoff randdt adt fupedt;
run;
proc sql noprint ;
create table xaxisdt as
select min(randdt) as xstdt format date9.
,max(adt) as xendt format date9.
,max(cutoff) as cutoffdt format date9.
from prefinal;
quit;
data xaxisdt;
set xaxisdt;
stdtn=intnx('month',xstdt,-1,'s');
stdtc=put(stdtn,date9.);
endtn=intnx('month',xendt,1,'s');
endtc=put(endtn,date9.);
keep stdtc endtc cutoffdt;
run;
proc sql noprint;
select stdtc,endtc ,cutoffdt into: xstdt, :xendt, :cutoffdt
from xaxisdt;
quit;
proc sgplot data=final;
highlow Y=N low=RANDDT1 high=ADT1/lineattrs=(thickness=1.5 pattern=solid color=grey) name='C' legendlabel='Follow-up Days';
highlow Y=N low=RANDDT2 high=ADT2/lineattrs=(thickness=1.5 pattern=solid color=charcoal) name='C' legendlabel='Follow-up Days';
yaxis label="Subjects (N = %trim(&n99))" values=(0 1000 2000 3000 4000 5000 6000 6500);
xaxis label="Study Calendar" interval=quarter VALUESFORMAT=MONYY7.;
refline "&cutoffdt."d/ axis=x lineattrs=(thickness=3 pattern=dash color=black) label="&cutoffdt. (COVID-19 Pandemic Date)";
inset "%trim(&nbe)% Of follow-up days prior to the start of COVID-19 pandemic" / position=left textattrs=(color=black size=9);
inset "%trim(&ndu)% Of follow-up days after or on the start of COVID-19 pandemic" / position=right textattrs=(color=black size=9);
run;
Also please let me know how we can wrap inset text.
Thank you,
Rajasekahar B
Hi,
I think the problem is that your plot has a linear axis rather than a time axis. To fix it, make sure randdt1 randdt2 addt1 addt2 all have date formats attached to them in work.final (you could add a format statement inside the SGPLOT). This should trigger SGPLOT to use a time axis.
One warning sign in your picture is that the month names are not the start of a quarter (they should be Jan, Apr, Jul, Oct). The easiest way to confirm this problem is to change the xaxis to valuesformat=date9. It should show you that the day values for each month are not the first or the last of the month, because it's a linear axis I think the start point is driven by the data. I'm surprised that SGPLOT doesn't throw at least a NOTE to say that you are using a time interval for a linear axis. I thought you could fix this by adding type=time to the axis statement, but SAS then throws a note that although you have requested a time axis, the axis will be changed to linear because the values being plotted are not time values.
So in order to get a proper time axis, it looks like the values need to be formatted as dates (or date-times or times) so SGPLOT knows it is plotting time values. Then SGPLOT will give you a time axis rather than a linear axis.
Sample code below. It's always helpful to make a tiny test case when exploring SAS questions, so that you can narrow down the problem.
data have ;
do randdt1="01Jan2020"d to "01Jan2021"d by 10 ;
randdt2=randdt1+100 ;
N++1 ;
output ;
end ;
run ;
title1 "This plot has a linear axis rather than a time axis because the variables are not formatted as dates" ;
proc sgplot data=have ;
highlow y=N low=randdt1 high=randdt2;
refline "11Mar2020"d /axis=x ;
xaxis interval=quarter valuesformat=mmddyy10. ;
run ;
title1 ;
title1 "This plot has a time axis because randdt1 and randdt2 have a date format attached" ;
proc sgplot data=have ;
highlow y=N low=randdt1 high=randdt2;
refline "11Mar2020"d /axis=x ;
xaxis interval=quarter valuesformat=mmddyy10. ;
format randdt1 randdt2 date9. ;
run ;
title1 ;
title1 "This plot has a time axis because randdt1 and randdt2 have a date format attached" ;
proc sgplot data=have ;
highlow y=N low=randdt1 high=randdt2;
refline "11Mar2020"d /axis=x ;
xaxis interval=quarter valuesformat=monyy7. ;
format randdt1 randdt2 date9. ;
run ;
title1 ;
cutoff maccro variable resolve to 11Mar2020 and date formate in data set is date9.
Thank you,
Rajasekhar Reddy.
Mar2020 tick-mark on X axis corresponds to 31Mar2020.
Hi,
I think the problem is that your plot has a linear axis rather than a time axis. To fix it, make sure randdt1 randdt2 addt1 addt2 all have date formats attached to them in work.final (you could add a format statement inside the SGPLOT). This should trigger SGPLOT to use a time axis.
One warning sign in your picture is that the month names are not the start of a quarter (they should be Jan, Apr, Jul, Oct). The easiest way to confirm this problem is to change the xaxis to valuesformat=date9. It should show you that the day values for each month are not the first or the last of the month, because it's a linear axis I think the start point is driven by the data. I'm surprised that SGPLOT doesn't throw at least a NOTE to say that you are using a time interval for a linear axis. I thought you could fix this by adding type=time to the axis statement, but SAS then throws a note that although you have requested a time axis, the axis will be changed to linear because the values being plotted are not time values.
So in order to get a proper time axis, it looks like the values need to be formatted as dates (or date-times or times) so SGPLOT knows it is plotting time values. Then SGPLOT will give you a time axis rather than a linear axis.
Sample code below. It's always helpful to make a tiny test case when exploring SAS questions, so that you can narrow down the problem.
data have ;
do randdt1="01Jan2020"d to "01Jan2021"d by 10 ;
randdt2=randdt1+100 ;
N++1 ;
output ;
end ;
run ;
title1 "This plot has a linear axis rather than a time axis because the variables are not formatted as dates" ;
proc sgplot data=have ;
highlow y=N low=randdt1 high=randdt2;
refline "11Mar2020"d /axis=x ;
xaxis interval=quarter valuesformat=mmddyy10. ;
run ;
title1 ;
title1 "This plot has a time axis because randdt1 and randdt2 have a date format attached" ;
proc sgplot data=have ;
highlow y=N low=randdt1 high=randdt2;
refline "11Mar2020"d /axis=x ;
xaxis interval=quarter valuesformat=mmddyy10. ;
format randdt1 randdt2 date9. ;
run ;
title1 ;
title1 "This plot has a time axis because randdt1 and randdt2 have a date format attached" ;
proc sgplot data=have ;
highlow y=N low=randdt1 high=randdt2;
refline "11Mar2020"d /axis=x ;
xaxis interval=quarter valuesformat=monyy7. ;
format randdt1 randdt2 date9. ;
run ;
title1 ;
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
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.