DATA Step, Macro, Functions and more

Utilizing Macro References in An Array

Accepted Solution Solved
Reply
Contributor
Posts: 30
Accepted Solution

Utilizing Macro References in An Array

[ Edited ]

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!

 

 


Accepted Solutions
Solution
‎01-30-2016 03:07 PM
Super User
Super User
Posts: 6,500

Re: Utilizing Macro References in An Array

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


All Replies
PROC Star
Posts: 1,231

Re: Utilizing Macro References in An Array

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.
Super User
Super User
Posts: 6,500

Re: Utilizing Macro References in An Array

[ Edited ]

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;

 

 

Trusted Advisor
Posts: 1,115

Re: Utilizing Macro References in An Array

[ Edited ]

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.

Solution
‎01-30-2016 03:07 PM
Super User
Super User
Posts: 6,500

Re: Utilizing Macro References in An Array

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;
Trusted Advisor
Posts: 1,115

Re: Utilizing Macro References in An Array

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.

☑ This topic is SOLVED.

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

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