DATA Step, Macro, Functions and more

SAS reading a time variable as a character during a do-loop?

Reply
N/A
Posts: 0

SAS reading a time variable as a character during a do-loop?

As far as I know, SAS times should be numeric, so I should be able to loop through the day in 15-minute intervals by converting the minutes to seconds. When I try to do that, though, I get the following error:

ERROR: A character operand was found in the %EVAL function or %IF
condition where a numeric operand is required. The condition
was: &start_time
ERROR: The %FROM value of the %DO TIME loop is invalid.
ERROR: A character operand was found in the %EVAL function or %IF
condition where a numeric operand is required. The condition
was: &end_time by &interval_seconds
ERROR: The %TO value of the %DO TIME loop is invalid.
ERROR: The macro TAQDATA will stop executing.

Code:

%let start_time = '9:30:00't; /* starting time*/
%let end_time = '16:00:00't; /* ending time (per Andersen)*/
%let interval_seconds = (15 * 60); /* interval length in seconds, note that total daily trading time should be divisible by interval length*/
%let no_intervals = ((&end_time-&start_time)/ &interval_seconds ) ; /*number of intervals based on start/end time and length of intervals*/

%do year = &start_year %to &end_year;
%do month = 1 %to 12;
%do time = &start_time %to &end_time by &interval_seconds;
%if (&year = 1996 or (&year = 2004 and &month>= 7)) %then %let fdatevar = datef;
%else %let fdatevar = fdate;
%let currentyear = %sysevalf(%substr(&year,3,2)); /*last two digits of year*/
%if %length(&month) = 1 %then %let currentmonth = 0&month;
%else %let currentmonth = &month; /* month in MM format*/
%let ctime=&time;
%let monthyearint = &currentyear&currentmonth&cinterval;
%if not(&year = &start_year and &month < &start_month) and not(&year = &end_year and &month > &end_month) and not (&ctime> &end_time) %then %
;

Any ideas greatly appreciated.
Super Contributor
Super Contributor
Posts: 3,174

Re: SAS reading a time variable as a character during a do-loop?

Posted in reply to deleted_user
A SAS constant for TIME as used in a DATA step is not equivalent to a time-formatted character string with a trailing "t". This value is still treated as a character string. If you need to process a macro using a time value, you will need to convert the character string to a SAS macro variable with a number that represents the equivalent time amount.

This data conversion involves a %SYSFUNC macro function and an INPUT function to read the time-formatted macro variable. For example, the SAS macro code below assigns a SAS number representing the current date (as days since 1/1/1960), then in a yyyy-mm-dd formatted value, as a character-string:

%let today = %sysfunc(today());
%put today is: &today;
%let todaymdy = %sysfunc(putn(&today,yymmdd10.));
%put today is: &todaymdy;


I suggest you reply to the forum and explain what you are trying to accomplish, rather than attempting to try to debug your macro logic and coding.

Scott Barry
SBBWorks, Inc.
Regular Contributor
Posts: 191

Re: SAS reading a time variable as a character during a do-loop?

Posted in reply to deleted_user
You have to resolve date and time to numeric.
This might work:

data _null_;
call symput('START_TIME','9:30:00't);
call symput('END_TIME','16:00:00't);
call symput('INTERVAL_SECONDS',(15*60));
call symput('NO_INTERVALS',((&end_time-&start_time)/ &interval_seconds ));
run;

You also need to use %BY instead of by in the macro loop....;-)
%do time = &START_TIME %to &END_TIME %by &INTERVAL_SECONDS;
//Fredrik
Ask a Question
Discussion stats
  • 2 replies
  • 177 views
  • 0 likes
  • 3 in conversation