BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
vicky07
Quartz | Level 8

Hi,

I am fairly new to Macro and I am struggling with the below simple code.

%Let ak =  abd2001;

%Macro year;

%If &ak = %str(abd2001) %then

%do;

     %Put &ak.;

%end;

%Mend;

%year;

The above code works fine,but i want to run the same macro for a different macro variable, let's say abd2005 .

%Let ak =  abd2005;

%Macro year;

%If &ak in %str(abd2001,abd2005) %then

%do;

     %Put &ak.;

%end;

%Mend;

%year;

I am getting error now. Like i said I am new to macro and just learning. Any help is much appreciated.

Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
Linlin
Lapis Lazuli | Level 10

Hi,

It works for me. below is my log file.

1    options minoperator mindelimiter=',';

2    %Let ak =  abd2005;

3    %Macro year;

4    %If &ak in %str(abd2001,abd2005) %then

5    %do;

6         %Put &ak.;

7    %end;

8    %Mend;

9    %year;

abd2005

View solution in original post

12 REPLIES 12
Linlin
Lapis Lazuli | Level 10

Hi,

try adding:

options minoperater;

if you have sas 9.2 or higher

vicky07
Quartz | Level 8

Linlin - Added the option you suggested and i am not getting any error this time but the macro condition is saying False, even though the parameter that i passed is abd2005..

Ankitsas
Calcite | Level 5

Replace --

%If &ak in %str(abd2001,abd2005)

with

%If &ak in (abd2001,abd2005)

Note - %str is used to mask commas,% etc.

vicky07
Quartz | Level 8

Ankitsas - I need the %str as some of the variables that input has commas. But on a side note, I tried without the %str and it still didn't work.

ballardw
Super User

Are you saying that values of AK will have commas? If so you probably want to use a mindelimiter other than comma because otherwise the IN operator will examine each comma delimited string to the value of AK and never find a match.

Example if AK = abd2001,abd2005 and you are using IN (abd2001, abd2005) then AK is compared to abd2001 and is not match, then AK is compared to abd2005 and is not a match, so no match.

You will need to use a charcter for the delimiter that will not ever appear in AK to have this work as intended.

Linlin
Lapis Lazuli | Level 10

Hi

try

options minoperator mindelimiter=',';

%Let ak =  abd2005;

%Macro year;

%If &ak in %str(abd2001,abd2005) %then

%do;

     %Put &ak.;

%end;

%Mend;

%year;

vicky07
Quartz | Level 8

Linlin - I added option mindelimiter and it is still no working.

Linlin
Lapis Lazuli | Level 10

Hi,

It works for me. below is my log file.

1    options minoperator mindelimiter=',';

2    %Let ak =  abd2005;

3    %Macro year;

4    %If &ak in %str(abd2001,abd2005) %then

5    %do;

6         %Put &ak.;

7    %end;

8    %Mend;

9    %year;

abd2005

vicky07
Quartz | Level 8

It works now. I had a typo in my code. TY!

Astounding
PROC Star

As you have seen, the IN operator in macro language is not as simple as the DATA step IN operator.  Given that you are new to macro language, I would recommend starting somewhere else.  Fow now, you could just spell out the values:

%if &ak=adb2001 or &ak=adb2005 %then %do;

Even that simple-looking syntax can become complicated, depending on the range of values that &AK can take on.

vicky07
Quartz | Level 8

Astounding,

It worked, thx, but the thing is I have more than 20 range of values that ak can take(just mentioned 2 for illustration purpose) and wondering if there is a more efficient way of doing this rather than listing all 20 variable with a OR in between.

Ron_MacroMaven
Lapis Lazuli | Level 10

see also The Answer supplied by a Little Birdie:

http://www.sascommunity.org/wiki/MINDELIMITER_Macro_In_Delimiter

Ron Fehd  in.tu.it maven

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
  • 12 replies
  • 1468 views
  • 3 likes
  • 6 in conversation