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
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.
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.
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.
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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.