DATA Step, Macro, Functions and more

only update if last day of month - comparinge dates

Reply
Regular Contributor
Posts: 236

only update if last day of month - comparinge dates

data _null_;
format LastDayOfMonth date.;
LastDayOfMonth = intnx('month',today(),1)-1;
call symput ('LastDayOfMonth',LastDayOfMonth);
run;
%put &lastdayofmonth; (THIS IS A NUMBER)

%macro updateL3;
%if today() eq &lastdayofmonth %then %do;
...
%end;
%mend;
%updateL3;

ERROR: Required operator not found in expression: today() eq %eval(&lastdayofmonth)
ERROR: The macro UPDATEL3 will stop executing.
Occasional Contributor
Posts: 7

Re: only update if last day of month - comparinge dates

Answer: Use %sysfunc(today()) when using today() in macro.


%macro update;

data _null_;
format LastDayOfMonth date.;
LastDayOfMonth = intnx('month',today(),1)-1;
call symput ('LastDayOfMonth',LastDayOfMonth);
run;
%put DEBUG:Smiley Sad&SYSMACRONAME)::Last day of month is &lastdayofmonth;
%put DEBUG:Smiley Sad&SYSMACRONAME)::Today is %sysfunc(today());

%if %sysfunc(today()) eq &lastdayofmonth %then %do;
%put DEBUG:Smiley Sad&SYSMACRONAME)::updated;
%end;
%else %do;
%put DEBUG:Smiley Sad&SYSMACRONAME)::Not updated;
%end;

%mend update;

/* ---- Call update ---- */
%update;

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Log output:
-----------------
DEBUG:Smiley SadUPDATE)::Last day of month is 18778
DEBUG:Smiley SadUPDATE)::Today is 18767
DEBUG:Smiley SadUPDATE)::Not updated
Regular Contributor
Posts: 236

Re: only update if last day of month - comparinge dates

Posted in reply to ChendhilKumar
ok thanks! have to review my sas macro i guess Smiley Happy
Super Contributor
Super Contributor
Posts: 3,174

Re: only update if last day of month - comparinge dates

You can use CALL EXECUTE to conditionally invoke a SAS code-piece/program/macro from within your DATA step, thus avoiding the "date-related' conditional macro logic.

Scott Barry
SBBWorks, Inc.
Regular Contributor
Posts: 241

Re: only update if last day of month - comparinge dates

I find that, in macros, you need to be aware of if your date is in the numeric or in the literal format, which necessitates at least a pair of conversion macros.

   %*-- two utilities --*;
   %macro date2num(date, informat=anydtdte.);
     %*-- strip quotations and postfix d from date literal if any. --*;
     %*-- quotations are intentionally doubled to prevent unmatched error --*;
     %let date=%sysfunc(prxchange(s/[''""]d?//i,-1,&date));
     %sysfunc(inputn(&date,&informat))
   %mend  date2num;
   %macro num2date(num, format=date10., literal=1);
     %local n;
     %let n = %sysfunc(putn(&num,&format));
     %if &literal %then "&n"d; %else &n;
   %mend  num2date;

   %*-- main work --*;
   %macro today();
     %num2date(%sysfunc(today()))
   %mend  today;
   %macro LDoM(date=%today());
     %sysfunc(intnx(mon,%date2num(&date),0,e))
   %mend  LDoM;
   %macro IsLDoM(date=%today());
     %sysevalf(&date = %LDoM(date=&date))
   %mend IsLDoM;

   %*-- check --*;
   %macro test(date=);
      %local not;
      %let not = %qsysfunc(ifc(%IsLDoM(date=&date),,%str(not )));
      &date is &not.the last day of the month.
   %mend  test;
   %put NOTE: %test(date="20may2011"d);
   %put NOTE: %test(date="31may2011"d);
   %*-- on log
   NOTE: "20may2011"d is not the last day of the month.
   NOTE: "31may2011"d is the last day of the month.
   --*;

Ask a Question
Discussion stats
  • 4 replies
  • 1280 views
  • 0 likes
  • 4 in conversation