I would like to draw a graph that shows intraday price changes in several days. Each day trading hour is from 9am to 4pm. I have a date variable and a time variable. If I use these variables then there would be gaps between the days as there is no data between 4pm and 9 am the next day. One quick way I could overcome this is to use the order number _N_ but then I need to format the time axis to show the date and hour correctly. Is there a different and proper way to do this? thanks
I addressed the "gap" issue in this blog article for holidays. You can likely use the idea for your case. I do not know of any format you can user to do this automatically.
https://blogs.sas.com/content/graphicallyspeaking/2017/09/27/stock-chart/
If a single axis is not a requirement, another way would be to use SGPANEL with PANELBY=day, and put the cells in a row. Then, all the gaps between 4pm and 9am should go away.
So I tried your suggested method but it gives an empty graph. my code is:
proc sgplot data=trade_summary2 noborder;
title "Intraday Effective Spread";
series x=datetime y=ES;
xaxis label= "Date" type=discrete discreteorder=data;
run;
I also changed type option to Time (type=TIME) but the gaps remain.
datetime is a datetime variable
I'm not sure if it would be worth the work depending on how many days you display at a time but the RANGES option could be used to create breaks. Use the date and time to create a datetime variable, use the desired format such as datetime10. and specify the ranges for each day which could be built from your data.
The break indicators might not be wanted but would at least indicate nothing is supposed to be in those intervals.
I'd probably use the _n_ method and build a user defined format to show the dates the way you want. You can use CNTLIN to create the table that maps the _n_ to the date.
So I tried your suggestion and almost get there. my code is:
data datetimefmt;
set trade_summary2;
keep fmtname n datetime;
rename n=start datetime=label;
fmtname="datetimefmt";
run;
However, the display value is a long number, not in a datetime format that i need (i.e. 10Apr2018 09:30:15). What am I missing?
@somebody wrote:
So I tried your suggestion and almost get there. my code is:
data datetimefmt; set trade_summary2; keep fmtname n datetime; rename n=start datetime=label; fmtname="datetimefmt"; run;
However, the display value is a long number, not in a datetime format that i need (i.e. 10Apr2018 09:30:15). What am I missing?
Most likely you want to use something like Label = put(datetime, datetime12.); to show values to hours.
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 25. 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.