DATA Step, Macro, Functions and more

Macro variable check

Accepted Solution Solved
Reply
Contributor
Posts: 71
Accepted Solution

Macro variable check

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!


Accepted Solutions
Solution
‎03-25-2013 07:05 PM
Super Contributor
Posts: 1,636

Re: Macro variable check

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


All Replies
Super Contributor
Posts: 1,636

Re: Macro variable check

Hi,

try adding:

options minoperater;

if you have sas 9.2 or higher

Contributor
Posts: 71

Re: Macro variable check

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..

Occasional Contributor
Posts: 14

Re: Macro variable check

Replace --

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

with

%If &ak in (abd2001,abd2005)

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

Contributor
Posts: 71

Re: Macro variable check

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.

Super User
Posts: 11,343

Re: Macro variable check

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.

Super Contributor
Posts: 1,636

Re: Macro variable check

Hi

try

options minoperator mindelimiter=',';

%Let ak =  abd2005;

%Macro year;

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

%do;

     %Put &ak.;

%end;

%Mend;

%year;

Contributor
Posts: 71

Re: Macro variable check

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

Solution
‎03-25-2013 07:05 PM
Super Contributor
Posts: 1,636

Re: Macro variable check

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

Contributor
Posts: 71

Re: Macro variable check

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

Super User
Posts: 5,516

Re: Macro variable check

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.

Contributor
Posts: 71

Re: Macro variable check

Posted in reply to Astounding

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.

Regular Contributor
Posts: 227

Re: Macro variable check

see also The Answer supplied by a Little Birdie:

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

Ron Fehd  in.tu.it maven

🔒 This topic is solved and locked.

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

Discussion stats
  • 12 replies
  • 444 views
  • 3 likes
  • 6 in conversation