DATA Step, Macro, Functions and more

Evaluate multiple dates via Macro

Reply
Frequent Contributor
Frequent Contributor
Posts: 83

Evaluate multiple dates via Macro

Hello all,


First time posting, frequent user of the advice found here.


I have a macro that runs all sorts of different programs based on days of the week and various dates. I'm trying to include another section that runs code based on certain dates. In the first example, the macro condition of &rptdte = &J1 is true and the code runs. However, in the second example, when I try to include other dates as part of &J1 it evaluates too false.

What is the correct method for evaluating multiple dates in this instance?

I appreciate your thoughts. Thanks!

EXAMPLE 1

%let A1=100313;

%let J1=3OCT2013;

%let rptdte=%sysfunc(inputn(&A1, mmddyy6.), date9.);

%put &rptdte;

options mprint mlogic;

%macro C3_Reports();

%let X = %sysfunc(weekday(%sysfunc(inputn(&a1, mmddyy6.))));

/* Code part 3  */

%if &rptdte = &J1 %then %do;

%include '\\W_gap_fs\Data\Grp\Code part 3.sas';

%end;

%mend;

%C3_Reports;

EXAMPLE 2

%let A1=100313;

%let J1 = (27DEC2012,24JAN2013,21FEB2013,21MAR2013,18APR2013,16MAY2013,13JUN2013,11JUL2013,08AUG2013,05SEP2013,03OCT2013,31OCT2013,28NOV2013);

%let rptdte=%sysfunc(inputn(&A1, mmddyy6.), date9.);

%put &rptdte;

options mprint mlogic;

%macro C3_Reports();

%let X = %sysfunc(weekday(%sysfunc(inputn(&a1, mmddyy6.))));

/* Code part 3  */

%if &rptdte = &J1 %then %do;

%include '\\W_gap_fs\Data\GrpCode part 3.sas';

%end;

%mend;

%C3_Reports;

Super User
Posts: 19,822

Re: Evaluate multiple dates via Macro

Are you looking for IN instead of = perhaps? If so also look up the minoperator when using the in operator in a macro.


Super Contributor
Posts: 418

Re: Evaluate multiple dates via Macro

you can't specify a = clause if the value that you are trying to compare is a string and you only have a substring in it.

Basically, you have

If 03OCT2013  = ...16MAY2013,13JUN2013,11JUL2013,08AUG2013,05SEP2013,03OCT2013,31OCT2013,28NOV2013)

Which it does not.

CHange your code to use a find clause. THerefore

%if find(&varb,&vara) > 1 %then %do;...

Where varb is your long string and vara is the date you are looking for.

However i'd change your entire macro code to just use the numeric values of dates (instead of the dates themselves) and then use an in clause instead... that's just me however

Super User
Posts: 19,822

Re: Evaluate multiple dates via Macro

Posted in reply to Anotherdream

I agree, using actual SAS dates is better.

One reason, that can be a pain to debug is a case mismatch.

For example in your current syntax 01Jan2013 is NOT EQUAL to 01JAN2013.


Frequent Contributor
Frequent Contributor
Posts: 83

Re: Evaluate multiple dates via Macro

Thanks for the feedback.

I would love to evaluate SAS dates instead of strings but there are other parts to this macro that require me to use strings

The solution seems obvious now. I changed it too evaluate the following:  %if %sysfunc(find(&j1, &rptdte))>0 %then %do;


Thanks for the extra set of eyes


SAS Super FREQ
Posts: 8,868

Re: Evaluate multiple dates via Macro

Hi, just a comment:

You said: "I would love to evaluate SAS dates instead of strings" -- the fundamental nature of the SAS Macro Facility is to work with strings. The Macro Facility is a text-based facility. It exists to resolve macro variable references and generate code for you. By the time your code gets to the compiler, there are NO, nada, zilch &macvar or %macpgm references left in the program. All of the macro "triggers" with & and % have been resolved, and code has been typed. So when you are working with SAS macro variables in a comparison, you are only dealing with text strings. So when you are using %IF, you are comparing strings.

cynthia

Frequent Contributor
Frequent Contributor
Posts: 83

Re: Evaluate multiple dates via Macro

Posted in reply to Cynthia_sas

That's a great point to remember and one that makes the solution even more obvious too me.Thanks. I'm still fairly new to SAS programming so keeping all of the details straight is tricky. Smiley Happy

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