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

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!

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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;

View solution in original post

5 REPLIES 5
Quentin
Super User
Can you say more about *how* SAS freaks out? Are you getting macro variable not resolved messages? What values do the macro vars have?Maybe turn on options MPRINT and call the macro, then add that log.
The Boston Area SAS Users Group is hosting free webinars!
Next up: Joe Madden & Joseph Henry present Putting Power into the Hands of the Programmer with SAS Viya Workbench on Wednesday Nov 6.
Register now at https://www.basug.org/events.
Tom
Super User Tom
Super User

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;

 

 

FreelanceReinh
Jade | Level 19

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.

Tom
Super User Tom
Super User

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;
FreelanceReinh
Jade | Level 19

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: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 1218 views
  • 3 likes
  • 4 in conversation