DATA Step, Macro, Functions and more

Macro to split a string into variables

Reply
Contributor msg
Contributor
Posts: 21

Macro to split a string into variables

Hi all,

I have a request to create variables in a dataset depends on the number of words a particular string has. I came with this code, but its not working.

For. Ex. The string "name" has 5 words, so my dataset one should have 5 variables starting with word1 till word5 containing the corresponding words.

%macro split (name=);
data one;

%let i=1;
j='x';
%do %while (j ne ' ');
word&i=%scan(&name,&i,' ');
j=word&i.;
output;
%let i=%eval (&i. + 1);
j=%scan(&name,&i,' ');
%end;

run;
%mend split;

%split (name='sas is an analytical language');

What may be the error in this code?
Super Contributor
Super Contributor
Posts: 3,174

Re: Macro to split a string into variables

You are mixing DATA step processing with SAS macro processing. A macro is normally resolved at compilation time. Your macro construct should be using all DATA step code (SAS variables, not SAS macro variables). Also, you will need to address the consideration of SAS variable length as you parse your input SAS variable -- defining a SAS LENGTH statement for your temporary variable - shown as "j". Lastly, your code generates one or more observations, depending on the input variable contents -- so you are actually generating an observation for each parsed sub-variable field/value.

Better to define what you want to actually accomplish showing both INPUT data and the desired OUTPUT data -- then others on the forum can provide feedback.

Scott Barry
SBBWorks, Inc.
Contributor msg
Contributor
Posts: 21

Re: Macro to split a string into variables

Okay.

The input is a string "sas is an analytical language".

The output should be 5 variables (dynamically) such as

Word1 Word2 Word3 Word4 Word5
--------- ------------ ----------- ----------- --------------
sas is an analytical language

Hope my request is clear.
Super Contributor
Super Contributor
Posts: 3,174

Re: Macro to split a string into variables

You can still use macro language logic but with SAS DATA step functions to parse your input variable, creating one or more "parsed data" variables, very similar to what you have shown in your code. Suggest you give it a try (coding a SAS macro to work within a DATA step), test it, and then come back to the forum, if you have unexpected results. Again, you will need to consider the SAS LENGTH attribute for each new variable - also you will have some challenges determining how many new SAS variables to declare, if you do not know the number of sub-string components that are present in your INPUT variable. Possibly you can look to use a SAS CALL SYMPUT when you have completed the parsing logic, setting a macro variable identifying number of words in your input variable. Investigate using the COUNTW function to determine your DO/END loop for the parsing process.

Scott Barry
SBBWorks, Inc.
Super Contributor
Posts: 281

Re: Macro to split a string into variables

%let string=sas is an analytical language;
%let nwords=%word_count2(&string);

The word_count2 macro is given at http://support.sas.com/kb/26/152.html
then you write a loop

inside the loop

%let word&i = %scan(&string,&i,%str( ));
Super Contributor
Super Contributor
Posts: 3,174

Re: Macro to split a string into variables

As I read the details (beyond the SUBJECT), the OP posted a question about parsing a SAS dataset variable, not a SAS macro variable.

Scott Barry
SBBWorks, Inc.
PROC Star
Posts: 1,760

Re: Macro to split a string into variables

Msg, think of the macro language as a code generator.

The OP posted a question about parsing a macro variable into SAS dataset variables as I understand it.
[pre]
%macro split (name=);
data one;
%do i=1 %to %sysfunc(countw(&name));
word&i = "%scan(&name,&i)";
%end;
run;
%mend split;
%split (name=sas is an analytical language)
[/pre]
or even all the logic in the data step:
[pre]
%macro split (name=);
%let countw=%sysfunc(countw(&name));
data one(drop=i);
array word(&countw) $16;
do i=1 to &countw;
word[ i] = scan("&name",i);
end;
run;
%mend split;

%split (name=sas is an analytical language)
Super Contributor
Super Contributor
Posts: 3,174

Re: Macro to split a string into variables

I believe that we are working with output SAS variables; I noticed an OUTPUT statement in the original post which persuaded my feedback. Maybe the OP needs to reconsider the basis of the post to contribute some different code -- better yet, start a new thread, when there is a problem / question on the topic point?

Scott Barry
SBBWorks, Inc.
Contributor msg
Contributor
Posts: 21

Re: Macro to split a string into variables

Thank you all for your suggestions. I will take a look and try today.

Mine is SAS 8.2 and doesn't support COUNTW function.
Super Contributor
Posts: 474

Re: Macro to split a string into variables

OK then, you should use tho "old fashion way" word parsing.

Just do the following modification to Chris code:

%macro split (name=);
data one;
%let I=1;
%do %until(%scan(&name,&I) eq );
word&i = "%scan(&name,&I)";
%let I=%eval(&I+1);
%end;
run;
%mend split;
%split (name=sas is an analytical language);

Cheers from Portugal.

Daniel Santos @ www.cgd.pt.
Contributor msg
Contributor
Posts: 21

Re: Macro to split a string into variables

Thanks all for ur explanations. I got confused with the intreaction between macro and the data step. Now, it's fine. Thanks again.
Ask a Question
Discussion stats
  • 10 replies
  • 6263 views
  • 0 likes
  • 5 in conversation