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

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

 

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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

3 REPLIES 3
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

Batman
Quartz | Level 8

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.

Astounding
PROC Star

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.

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
  • 3 replies
  • 846 views
  • 1 like
  • 3 in conversation