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!
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
Hi,
try adding:
options minoperater;
if you have sas 9.2 or higher
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..
Replace --
%If &ak in %str(abd2001,abd2005)
with
%If &ak in (abd2001,abd2005)
Note - %str is used to mask commas,% etc.
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.
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.
Hi
try
options minoperator mindelimiter=',';
%Let ak = abd2005;
%Macro year;
%If &ak in %str(abd2001,abd2005) %then
%do;
%Put &ak.;
%end;
%Mend;
%year;
Linlin - I added option mindelimiter and it is still no working.
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
It works now. I had a typo in my code. TY!
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.
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.
see also The Answer supplied by a Little Birdie:
http://www.sascommunity.org/wiki/MINDELIMITER_Macro_In_Delimiter
Ron Fehd in.tu.it maven
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.