Hello SAS community, I've built some code in data steps that I am now going to have to repeat on many other data sources, so I'm changing the code over to %macro in order to easily apply it many times. However I've run into an issue. First i will show the original code snippet, then I will show a toy example I am playing with to find out exactly what is causing the problem: %do j=1 %to &d;
%if %substr(array{j},1,5) in ("str1", "str2", "str3") %then %do;
if var1 > 3 then var2="no";
%end;
%end; THis produces error4: "Macro function %substr has too many arguments. The excess arguments will be ignored. A character operand was found in the %EVAL function or %IF condition where a number operand is required. The condition was: j} Argument 2 to macro function %substr is not a number. Operand missing for IN operator to argument to %EVAL function" So one of the first things I realised when converting code to macros is that the %dos and %ifs need to have percent prefix to avoid issues, then i found out that substr() is not valid in macros, so you ahve to use either %substr or %sysfunc(substr()). Then I learned that "IN" is also not valid, unless you include options to allow it before code. GIven these lessons, I have tried to now use a toy example just to test out the general functionality of declaring array elements and checking substr of the value of them using "in" operand. Here's my test code: data test;
input id $ answer1 $ answer2 $;
datalines;
1 yes maybe
2 no maybe
3 maybe maybe
;
run;
options minoperator mindelimiter=',';
%macro substringtest();
data test2;
set test;
array answer{2};
%do i=1 %to 2;
%if %substr(answer{i},1,1) in ("y", "n") %then %do;
certain&i=1;
%end;
%else %do;
certain&i=0;
%end;
%end;
run;
%mend;
%substringtest; When running this, I get no mlogic issues, but the values for obs 1 and 2 "certain1" should be "1", but they are all 0 for certain1 and certain2. I'm now a little confused regarding a few things 1) Why is the substr argument workign in test code but no the real code. 2) Why isn't the test code producing proper variable outputs reflecting correct logic. 3) Is there some easier way to get around using do blocks? I have other macros I converted to which uses substr and arrays and in just fine, but because i have further conditions after, i have some parts that are in "do" blocks. Maybe there's a workaround to just not do this? Thanks ahead of time.
... View more