BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
meckarthik
Quartz | Level 8

Hi, need help 

currently got a program that should stop execute if it is Friday after 4 pm 

Below macro get todays datetime 

proc format;
picture dtStamp (round)
low - high = '%Y-%0m-%0d %0H:%0M' (datatype=datetime);
run;

%let dttime = %sysfunc(datetime(), dtStamp.);

%put &dttime;

 

Below is the code i tried to run but getting error .

%macro controlit ;

%if &dttime <=6 and timepart(datetime())>'16:00't %then %do ;
stop ;
run ;
%end ;
%mend;
%controlit;

 

Any help, please 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

This can never work:

%if &dttime <=6 and timepart(datetime())>'16:00't %then %do ;

In macro statements, you always need %sysfunc() to use data step functions. So you need

%sysfunc(timepart(%sysfunc(datetime())))

You also get an ERROR because the formatted datetime value cannot be compared to a number. Mind that a datetime value less than 6 is every point in time before 1960-01-01T00:00:06. Datetimes are counts of seconds from midnight on that day.

 

To achieve a conditional stop, you don't need macro code:

data _null_;
set sashelp.class;
put name=;
if weekday(today()) = 6 and time() > '16:00't then stop;
run;

Log:

 73         data _null_;
 74         set sashelp.class;
 75         put name=;
 76         if weekday(today()) = 6 and time() > '16:00't then stop;
 77         run;
 
 Name=Alfred
 Name=Alice
 Name=Barbara
 Name=Carol
 Name=Henry
 Name=James
 Name=Jane
 Name=Janet
 Name=Jeffrey
 Name=John
 Name=Joyce
 Name=Judy
 Name=Louise
 Name=Mary
 Name=Philip
 Name=Robert
 Name=Ronald
 Name=Thomas
 Name=William

I changed it to work for today (a Tuesday):

data _null_;
set sashelp.class;
put name=;
if weekday(today()) = 3 and time() > '16:00't then stop;
run;

and got this log:

 73         data _null_;
 74         set sashelp.class;
 75         put name=;
 76         if weekday(today()) = 3 and time() > '16:00't then stop;
 77         run;
 
 Name=Alfred
 NOTE: There were 1 observations read from the data set SASHELP.CLASS.
 NOTE:  Verwendet wurde: DATA statement - (Gesamtverarbeitungszeit):
       real time           0.01 seconds
       cpu time            0.00 seconds

View solution in original post

5 REPLIES 5
PaigeMiller
Diamond | Level 26

@meckarthik wrote:

Hi, need help 

currently got a program that should stop execute if it is Friday after 4 pm 

Below macro get todays datetime 

proc format;
picture dtStamp (round)
low - high = '%Y-%0m-%0d %0H:%0M' (datatype=datetime);
run;

%let dttime = %sysfunc(datetime(), dtStamp.);

%put &dttime;

 

Below is the code i tried to run but getting error .

%macro controlit ;

%if &dttime <=6 and timepart(datetime())>'16:00't %then %do ;
stop ;
run ;
%end ;
%mend;
%controlit;

 

Any help, please 


I answered this here: https://communities.sas.com/t5/New-SAS-User/stop-processing-code-if-a-certain-condition-is-met/m-p/5...

--
Paige Miller
meckarthik
Quartz | Level 8

thank you! 

ballardw
Super User

@meckarthik wrote:

Hi, need help 

currently got a program that should stop execute if it is Friday after 4 pm 

Below macro get todays datetime 

proc format;
picture dtStamp (round)
low - high = '%Y-%0m-%0d %0H:%0M' (datatype=datetime);
run;

%let dttime = %sysfunc(datetime(), dtStamp.);

%put &dttime;

 

Below is the code i tried to run but getting error .

%macro controlit ;

%if &dttime <=6 and timepart(datetime())>'16:00't %then %do ;
stop ;
run ;
%end ;
%mend;
%controlit;

 

Any help, please 


Since your macro includes a RUN statement I have to assume (because you did not show how your are actually calling the macro) that it is in a data step. Don't bother, just put that code equivalent in the data step. There are some serious issues related to data step compilation and macro resolutions. And your macro is likely not executing when you think it is.

 

Second, what values do your created &dttime actually take? Look at them. Then compare those "values" to 6. Very carefully.

 

If you have run this code and gotten an error you should post the log with the code and error.

 

With macro's it is best to set the system option MPRINT when testing to get a better look at the code actually generated by the macro. Read the LOG carefully.

Kurt_Bremser
Super User

This can never work:

%if &dttime <=6 and timepart(datetime())>'16:00't %then %do ;

In macro statements, you always need %sysfunc() to use data step functions. So you need

%sysfunc(timepart(%sysfunc(datetime())))

You also get an ERROR because the formatted datetime value cannot be compared to a number. Mind that a datetime value less than 6 is every point in time before 1960-01-01T00:00:06. Datetimes are counts of seconds from midnight on that day.

 

To achieve a conditional stop, you don't need macro code:

data _null_;
set sashelp.class;
put name=;
if weekday(today()) = 6 and time() > '16:00't then stop;
run;

Log:

 73         data _null_;
 74         set sashelp.class;
 75         put name=;
 76         if weekday(today()) = 6 and time() > '16:00't then stop;
 77         run;
 
 Name=Alfred
 Name=Alice
 Name=Barbara
 Name=Carol
 Name=Henry
 Name=James
 Name=Jane
 Name=Janet
 Name=Jeffrey
 Name=John
 Name=Joyce
 Name=Judy
 Name=Louise
 Name=Mary
 Name=Philip
 Name=Robert
 Name=Ronald
 Name=Thomas
 Name=William

I changed it to work for today (a Tuesday):

data _null_;
set sashelp.class;
put name=;
if weekday(today()) = 3 and time() > '16:00't then stop;
run;

and got this log:

 73         data _null_;
 74         set sashelp.class;
 75         put name=;
 76         if weekday(today()) = 3 and time() > '16:00't then stop;
 77         run;
 
 Name=Alfred
 NOTE: There were 1 observations read from the data set SASHELP.CLASS.
 NOTE:  Verwendet wurde: DATA statement - (Gesamtverarbeitungszeit):
       real time           0.01 seconds
       cpu time            0.00 seconds

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 540 views
  • 0 likes
  • 4 in conversation