BookmarkSubscribeRSS Feed
Ody
Quartz | Level 8 Ody
Quartz | Level 8

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;

6 REPLIES 6
Reeza
Super User

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


Anotherdream
Quartz | Level 8

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

Reeza
Super User

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.


Ody
Quartz | Level 8 Ody
Quartz | Level 8

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


Cynthia_sas
SAS Super FREQ

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

Ody
Quartz | Level 8 Ody
Quartz | Level 8

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 6 replies
  • 963 views
  • 0 likes
  • 4 in conversation