%scan only reads first character of macro array variable value

Accepted Solution Solved
Reply
Contributor
Posts: 23
Accepted Solution

%scan only reads first character of macro array variable value

The following code creates a macro array variable and then runs a macro that scans the array and outputs each variable in the log.   However, the output is only the first numeric value.   How can I get %scan to read each variable completely, including the characters and special characters.

 

%let sample=-1.0% -2.0% 0.0 1.0xAF 2.0xAF;

%macro test;

%do i = 1 %to 6;

%put %scan(&sample,&i);

%end;

%mend;

 

%test

 


Accepted Solutions
Solution
‎12-04-2017 09:29 AM
Super User
Super User
Posts: 9,437

Re: %scan only reads first character of macro array variable value

Some starter for you:

1) Macro has no concept of array - this is datastep component

2) Read the documentation on %scan(), in particular the delimiter. 

3) Ask yourself, if Base SAS - the programming language - provides the data constructs, processing power, and simple coding - why would you want to start trying to push this through a text replacement facility?

 

To correct your code (note I add dots after macro variables and semicolon to finish a line):

%let sample=-1.0% -2.0% 0.0 1.0xAF 2.0xAF;
%macro test;
  %do i=1 %to 6;
    %put %scan(&sample.,&i., );
  %end;
%mend;
 
%test;

The main change is I put a space as the third parameter to the %scan call.

View solution in original post


All Replies
Solution
‎12-04-2017 09:29 AM
Super User
Super User
Posts: 9,437

Re: %scan only reads first character of macro array variable value

Some starter for you:

1) Macro has no concept of array - this is datastep component

2) Read the documentation on %scan(), in particular the delimiter. 

3) Ask yourself, if Base SAS - the programming language - provides the data constructs, processing power, and simple coding - why would you want to start trying to push this through a text replacement facility?

 

To correct your code (note I add dots after macro variables and semicolon to finish a line):

%let sample=-1.0% -2.0% 0.0 1.0xAF 2.0xAF;
%macro test;
  %do i=1 %to 6;
    %put %scan(&sample.,&i., );
  %end;
%mend;
 
%test;

The main change is I put a space as the third parameter to the %scan call.

Contributor
Posts: 23

Re: %scan only reads first character of macro array variable value

[ Edited ]

Your initial code just output the entire macro value not the individual array values (I accept SAS doesn't have really have macro arrays but I've seen write ups referring to them as such).   However, it worked once I put ' ' as the delimiter.   In reference to your last point, this was an excerpt of a larger program where the array values can vary depending on data steps previous to this one.   In any case, thanks for your response.

Super User
Posts: 6,637

Re: %scan only reads first character of macro array variable value

I was surprised and happy to see that the @RW9 code worked for me, as is.  Perhaps this is a recent addition, and only works in 9.4?  Formerly, you would need to add the delimiter of:

 

%str( )

 

Note that using ' ' as the delimiter will use blanks as delimiters, but will also use single quotes as delimiters.

☑ This topic is solved.

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

Discussion stats
  • 3 replies
  • 155 views
  • 1 like
  • 3 in conversation