In the macro this is what I have:
DATA %SCAN(&DATA,&i);
SET %SCAN(&DATA,&i);
ARRAY %SCAN(&DATA,&i)[*] &&List_Social;
ARRAY BIN_%SCAN(&DATA,&i)[*] BIN_%SCAN(&DATA,&i)1 - BIN_%SCAN(&DATA,&i)&&Count%SCAN(&DATA,&i);
DO i = 1 TO &&Count%SCAN(&DATA,&i);
IF %SCAN(&DATA,&i)(i) = '' THEN BIN_%SCAN(&DATA,&i)(i) = 0;
ELSE BIN_%SCAN(&DATA,&i)(i) = 1;
END;
RUN;
Outside of the macro the code resolves to this:
DATA Social;
SET Social;
ARRAY Social[&CountSocial] &List_Social;
ARRAY BIN_Social[&CountSocial] BIN_Social1 - BIN_Social&CountSocial;
DO i = 1 TO &CountSocial;
IF Social(i) = '' THEN BIN_Social(i) = 0;
ELSE BIN_Social(i) = 1;
END;
RUN;
When it runs in the macro, SAS freaks out when it gets to the '&' symbols and does not treat them as macro variables. Any help is appreciated.
Thanks!
That's probably it. Again it is easier to just use another variable and break the step into pieces. Has the added benifit when the value is needed in multiple places of making the code easier to read.
%let countvar=Count%SCAN(&DATA,&i);
%let count=&&&countvar;
I suspect that it tokenizer is splitting BIN_%.... into two tokens and making your generated SAS code invalid.
Usually I just assign the generated word to macro variable and then use the vlaue of the macro variable. But you could also try wrapping it inside of a %UNQUOTE() function so that SAS knows to treat it as one word.
So for example you could generate the BIN_.... name into a macro variable called BIN and then use it as the name for your ARRAY. You could also simplify the logic of your DO loop and convert your IF/THEN/ELSE to just an assignment statement.
%let bin=BIN_%SCAN(&DATA,&i);
%let nbins=&&Count%SCAN(&DATA,&i);
ARRAY &BIN [&nbins] ;
DO i = 1 TO dim(&bin);
&bin(i) = NOT (%SCAN(&DATA,&i)(i) = '') ;
END;
Hi @Katie,
I think, the primary issue is the construct
&&Count%SCAN(&DATA,&i)
Please try and replace both occurrences of it by
%unquote(%superq(Count%SCAN(&DATA,&i)))
Actually, in the second occurrence (the "TO" value of the DO loop) you could omit the %UNQUOTE(), but it does no harm anyway.
Edit:
Explanation: The idea is that after %SCAN(&DATA,&i) resolves to something like Social, %SUPERQ will return the content of macro variable CountSocial (this macro variable must exist in your macro, though). In the first occurrence of the problematic construct, the value contained in CountSocial, say 17, needs to be prefixed with BIN_%SCAN(&DATA,&i), i.e. BIN_Social. This requires unquoting, because otherwise an invisible character (from macro quoting) would hang between "BIN_Social" and "17" and the data step compiler wouldn't like that.
That's probably it. Again it is easier to just use another variable and break the step into pieces. Has the added benifit when the value is needed in multiple places of making the code easier to read.
%let countvar=Count%SCAN(&DATA,&i);
%let count=&&&countvar;
Yes, sure. I would have coded that macro differently, too. With my suggestion I tried just to fix the issue without changing too much of the existing code.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.