Sample 26153: Create a separate macro variable for each 'word' in a string
The sample code on the Full Code tab uses macro logic to parse a string into separate macro variables.
Note: See SAS Note 24736, "Scanning for words in a string", for a non-macro approach using the SCAN function in a DATA step.
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
/* Code below uses a blank as the delimiter */
/* The %STR below would need to be changed if another delimiter */
/* is to be used */
/* For example: %let var&i=%qscan(%superq(string),&i,%str(,)); */
/* This would treat a comma as the delimiter */
%macro test(string);
%let word_cnt=%sysfunc(countw(%superq(string)));
%do i = 1 %to &word_cnt;
%let var&i=%qscan(%superq(string),&i,%str( ));
%put &&var&i;
%end;
%mend test;
%test(a b c d e)
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
Output written to log
a
b
c
d
e
This sample uses macro logic to parse a string into separate macro variables using a blank as the delimiter.
Type: | Sample |
Topic: | SAS Reference ==> Macro
|
Date Modified: | 2012-03-09 09:35:24 |
Date Created: | 2007-04-21 03:03:00 |
Operating System and Release Information
SAS System | Base SAS | All | n/a | n/a |