DATA Step, Macro, Functions and more

Macros and Dates

Reply
Frequent Contributor
Posts: 97

Macros and Dates

I am trying to write a macro that checks a table every 60 seconds to see if there is an entry for today's date. If not, it sleeps 60 seconds, if found, it starts a process. I think I have two problems: not sure if I am using Call Symput correctly, and the &sysdate is character, and I am trying to compare to a date (numberic) value. Any help would be greatly appreciated!

%MACRO AUTO();
%LET WAIT = 'TRUE';
%DO %WHILE (&WAIT = 'TRUE');
%SLEEP(60);
DATA _NULL_;
SET FILES.CHECK;
IF DATE = "&SYSDATE" THEN CALL SYMPUT('WAIT', 'FALSE');
RUN;
%END;
/*PROCESS*/
%MEND AUTO;
Christopher Johnson
www.codeitmagazine.com
Super Contributor
Super Contributor
Posts: 3,174

Re: Macros and Dates

There is no %SLEEP macro, unless you have defined it, possibly to use a DATA step with SLEEP function? You may want to explore the use of %SYSFUNC and invoking a CALL function in that manner without a DATA step.

Also the &SYSDATE value (in double-quotes) must be identified as a SAS DATE constant by adding the "D" suffix to the quoted string.


Scott Barry
SBBWorks, Inc.
Frequent Contributor
Posts: 97

Re: Macros and Dates

Thank you so much! I have a sleep macro, but I had completely forgotten to use the d to make sysdate a data constant. Works great now. I included the working code below in case it helps anyone else.

%MACRO AUTO();
%LET WAIT = 'TRUE';
%DO %WHILE (&WAIT = 'TRUE');
%SLEEP(2);
DATA _NULL_;
SET FILES.CHECK;
IF DATE = "&SYSDATE"D THEN CALL SYMPUT('WAIT', 'FALSE');
RUN;
%END;
/*PROCESS*/
%PUT "IT WORKED!";
%MEND AUTO;

%MACRO SLEEP(SECONDS);
DATA _NULL_;
Var1 = SLEEP(&SECONDS);
RUN;
%MEND SLEEP;
Christopher Johnson
www.codeitmagazine.com
Super Contributor
Super Contributor
Posts: 3,174

Re: Macros and Dates

If appropriate, the %SLEEP macro can be eliminated by using:

%LET X = %SYSFUNC(SLEEP(60));

Also, if you only need to detect the presence of at least one obs for the current date, consider using a WHERE and also have a DO/END loop to invoke the SYMPUT, along with a STOP; statement -- given what you have shown for a DATA step invocation, there is really no need to pass the entire file, right? Just a thought for SAS programming optimization.

Scott Barry
SBBWorks, Inc.
Frequent Contributor
Posts: 97

Re: Macros and Dates

Great tips. I replaced the sleep function, and the where statement really helped.

Thanks.
Christopher Johnson
www.codeitmagazine.com
Frequent Contributor
Posts: 97

Re: Macros and Dates

I have one more question if you don't mind. I am calling the macro with the following parameters, and it works great:

%LET CURR = "&SYSDATE"D - 1;
%LET PREV = "&SYSDATE"D - 2;

How do I get the date one month prior (i.e. 6/19/09 and 5/19/09), since the number of days to subtract will vary?
Christopher Johnson
www.codeitmagazine.com
Super Contributor
Super Contributor
Posts: 3,174

Re: Macros and Dates

If you intend to do it macro-based, you will need to work with %SYSFUNC and look at the DATE-related functions (normally used in a DATA step but accessible in macro language when using SYSFUNC. Then construct a new macro variable, again using SYSFUNC, and the MDY function.

Suggest in the future you consider creating a new post for a new question or discussion topic.

Scott Barry
SBBWorks, Inc.
Valued Guide
Posts: 2,177

Re: Macros and Dates

the INTNX() function has a SAMEDAY parameter (in place of begin/end/middle)

%put %sysfunc( intnx( month, "21Jun2009"d, -1, sameday ),date9 ) ;
should report 21May2009 in the log
(works for me)
PeterC
Frequent Contributor
Posts: 80

Re: Macros and Dates

Peter

Very nice to know about SAMEDAY parameter in intnx function.

Thanks.

Super Contributor
Posts: 474

Re: Macros and Dates

Also, you should consider working with the lock table features.

If any process tries to write something, while you checking (reading) the table, or you try to check the table while its being written to, you'll get an error (unless it's a SAS/SHARE library or, your SO is able to do the lock handling, like z/OS).

See the online documentation:
http://support.sas.com/documentation/cdl/en/shrref/59595/HTML/default/a000203947.htm

Cheers from Portugal.

Daniel Santos @ www.cgd.pt
Frequent Contributor
Posts: 97

Re: Macros and Dates

Posted in reply to DanielSantos
Thanks. I had just run into that problem!
Christopher Johnson
www.codeitmagazine.com
Ask a Question
Discussion stats
  • 10 replies
  • 1068 views
  • 0 likes
  • 5 in conversation