DATA Step, Macro, Functions and more

date comparison condition in a macro

Accepted Solution Solved
Reply
Contributor
Posts: 52
Accepted Solution

date comparison condition in a macro

I am trying to create a macro bibgen, that sets the macro variable bib to a, if the date stichdatum is earlier then 01Jan2012, else to b.

Stichdatum is a date macro variable that is created by the code

--------------------------------------------------------

%LET &

%LET Stichdatum = %sysfunc(INPUTN(&stichtag, B8601DA.));

--------------------------------------------------------

Here is my macro, however it does not work. It always returns b, whereas it should return b in this case! How can I fix it?

--------------------------------------------------------

/* Makro zur automatischen Selektion der richtigen Bibliothek wg. Historisierung */

%Macro bibgen();

%GLOBAL bib;

%LET stichdatum2 = %SYSFUNC(putn(&stichdatum, date9.));

%PUT stichdatum=&stichdatum;

%PUT stichdatum2=&stichdatum2;

%IF &stichdatum < '01Jan2012'd

      %THEN %LET bib =a;

      %ELSE %LET bib =b;

%PUT &bib;

%mend bibGen;


Accepted Solutions
Solution
‎05-16-2012 06:26 AM
Occasional Contributor
Posts: 9

Re: date comparison condition in a macro

Rather than writing the date as '01Jan2012'd, that will just be interpereated as text? Use this instead...

%sysfunc(putn('01Jan2012'd,5.))

View solution in original post


All Replies
Contributor
Posts: 52

Re: date comparison condition in a macro

The first line must be

%LET &stichtag=20111230;


Solution
‎05-16-2012 06:26 AM
Occasional Contributor
Posts: 9

Re: date comparison condition in a macro

Rather than writing the date as '01Jan2012'd, that will just be interpereated as text? Use this instead...

%sysfunc(putn('01Jan2012'd,5.))

Contributor
Posts: 52

Re: date comparison condition in a macro

Thank you! It works. However, just that I understand the reason: I always thought that SAS would evaluate the condition as sas code and not as macro code.

So would the condition be different in an regular IF-THEN-Clause? i.e.I could write '01Jan2012'd in a regular IF-THEN-Clause, coundn't I?

Super User
Super User
Posts: 6,499

Re: date comparison condition in a macro

Use %SYSEVALF() to let the macro code evaluate the date comparisons.

%IF %sysevalf(&stichdatum < '01Jan2012'd)

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 3144 views
  • 1 like
  • 3 in conversation