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.
BASUG is hosting free webinars Next up: Jane Eslinger presenting PROC REPORT and the ODS EXCEL destination on Mar 27 at noon ET. Register now at the Boston Area SAS Users Group event page: 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-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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