<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: String to split into words using macro. in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/String-to-split-into-words-using-macro/m-p/49640#M10319</link>
    <description>Hey Learner,&lt;BR /&gt;
The macro is very simple. You just need to understand how the word breaking be done to assign that word to some variable. &lt;BR /&gt;
&lt;BR /&gt;
Here is the code :: &lt;BR /&gt;
&lt;BR /&gt;
%macro splitter(string=);&lt;BR /&gt;
%let cnt=1;&lt;BR /&gt;
%let num1=%scan(&amp;amp;string,&amp;amp;cnt,%str( ));&lt;BR /&gt;
	%do %while(&amp;amp;num1 ne %str( ));&lt;BR /&gt;
	%global word&amp;amp;cnt;&lt;BR /&gt;
		%let word&amp;amp;cnt=&amp;amp;num1;&lt;BR /&gt;
		%let cnt=%eval(&amp;amp;cnt+1);&lt;BR /&gt;
		%let num1=%scan(&amp;amp;string,&amp;amp;cnt,%str( ));&lt;BR /&gt;
	%end;&lt;BR /&gt;
%mend splitter;&lt;BR /&gt;
%splitter(string=base macros sql advance)&lt;BR /&gt;
%put _global_;&lt;BR /&gt;
&lt;B&gt;&lt;/B&gt;</description>
    <pubDate>Sat, 27 Jun 2009 18:22:39 GMT</pubDate>
    <dc:creator>SushilNayak</dc:creator>
    <dc:date>2009-06-27T18:22:39Z</dc:date>
    <item>
      <title>String to split into words using macro.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/String-to-split-into-words-using-macro/m-p/49639#M10318</link>
      <description>Hi, &lt;BR /&gt;
&lt;BR /&gt;
Can any one help me in solving this. &lt;BR /&gt;
&lt;BR /&gt;
I have a string which consists of 4 words.I have to create  macro to split the string into 4 words to create  each word into a global macro variable and the same to be displayed as  individual macro variable.&lt;BR /&gt;
&lt;BR /&gt;
For ex. string=base macros sql advance. &lt;BR /&gt;
i want each word to be an individual  global macro variable like &lt;BR /&gt;
word1=base word2=macros word3=sql etc. &lt;BR /&gt;
and the same to be displayed  as variable=value in log window.&lt;BR /&gt;
&lt;BR /&gt;
How do u write a macro for  this and to be  displayed .</description>
      <pubDate>Sat, 27 Jun 2009 17:37:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/String-to-split-into-words-using-macro/m-p/49639#M10318</guid>
      <dc:creator>keen_sas</dc:creator>
      <dc:date>2009-06-27T17:37:59Z</dc:date>
    </item>
    <item>
      <title>Re: String to split into words using macro.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/String-to-split-into-words-using-macro/m-p/49640#M10319</link>
      <description>Hey Learner,&lt;BR /&gt;
The macro is very simple. You just need to understand how the word breaking be done to assign that word to some variable. &lt;BR /&gt;
&lt;BR /&gt;
Here is the code :: &lt;BR /&gt;
&lt;BR /&gt;
%macro splitter(string=);&lt;BR /&gt;
%let cnt=1;&lt;BR /&gt;
%let num1=%scan(&amp;amp;string,&amp;amp;cnt,%str( ));&lt;BR /&gt;
	%do %while(&amp;amp;num1 ne %str( ));&lt;BR /&gt;
	%global word&amp;amp;cnt;&lt;BR /&gt;
		%let word&amp;amp;cnt=&amp;amp;num1;&lt;BR /&gt;
		%let cnt=%eval(&amp;amp;cnt+1);&lt;BR /&gt;
		%let num1=%scan(&amp;amp;string,&amp;amp;cnt,%str( ));&lt;BR /&gt;
	%end;&lt;BR /&gt;
%mend splitter;&lt;BR /&gt;
%splitter(string=base macros sql advance)&lt;BR /&gt;
%put _global_;&lt;BR /&gt;
&lt;B&gt;&lt;/B&gt;</description>
      <pubDate>Sat, 27 Jun 2009 18:22:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/String-to-split-into-words-using-macro/m-p/49640#M10319</guid>
      <dc:creator>SushilNayak</dc:creator>
      <dc:date>2009-06-27T18:22:39Z</dc:date>
    </item>
    <item>
      <title>Re: String to split into words using macro.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/String-to-split-into-words-using-macro/m-p/49641#M10320</link>
      <description>Minor embellishment to code from Sushil Nayak taking advantage of COUNTW function (new with SAS 9), and to echo resolved macro var string back to the SASLOG -- also added optional macro invocation parameters for flexibility:&lt;BR /&gt;
&lt;BR /&gt;
%macro splitter(string=,wordpfx=WORD,dlm=%str( ));&lt;BR /&gt;
%do cnt=1 %to %sysfunc(countw(&amp;amp;string,&amp;amp;dlm));&lt;BR /&gt;
  %global &amp;amp;wordpfx&amp;amp;cnt;&lt;BR /&gt;
  %let &amp;amp;wordpfx&amp;amp;cnt = %scan(&amp;amp;string,&amp;amp;cnt,%str(&amp;amp;dlm));&lt;BR /&gt;
  %* echo macro var result to log window;&lt;BR /&gt;
  %put &amp;amp;wordpfx&amp;amp;cnt=&amp;amp;&amp;amp;&amp;amp;wordpfx&amp;amp;cnt;&lt;BR /&gt;
%end;&lt;BR /&gt;
%mend splitter;&lt;BR /&gt;
%splitter(string=base\macros\sql\advance,wordpfx=xxx,dlm=\);&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.</description>
      <pubDate>Sat, 27 Jun 2009 18:51:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/String-to-split-into-words-using-macro/m-p/49641#M10320</guid>
      <dc:creator>sbb</dc:creator>
      <dc:date>2009-06-27T18:51:16Z</dc:date>
    </item>
    <item>
      <title>Re: String to split into words using macro.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/String-to-split-into-words-using-macro/m-p/49642#M10321</link>
      <description>Learner,&lt;BR /&gt;
&lt;BR /&gt;
Just in case you'd prefer not to leave SAS base you could use the following. It generates the macro variables and displays their values in the log. I haven't fully tested it but it should be good for any number of words in the string. Of course you can replace the TRIM/LEFT functions with the appropriate version 9 function.&lt;BR /&gt;
&lt;BR /&gt;
data a;&lt;BR /&gt;
 string = 'base macros sql advance';&lt;BR /&gt;
 i=1;&lt;BR /&gt;
 DO UNTIL(SCAN(string,i)='');&lt;BR /&gt;
  call symput('VAR'!!put(i,3. -L),SCAN(string,i));&lt;BR /&gt;
  i+1;&lt;BR /&gt;
 END;&lt;BR /&gt;
 DO j = 1 to (i-1);&lt;BR /&gt;
  x    = symget('var'!!put(j,3.-l));&lt;BR /&gt;
  putv = 'var'!!TRIM(put(j,3.-l))!!'='!!TRIM(LEFT(x));&lt;BR /&gt;
  put putv;&lt;BR /&gt;
 END;&lt;BR /&gt;
RUN;&lt;BR /&gt;
&lt;BR /&gt;
Regards,&lt;BR /&gt;
&lt;BR /&gt;
BPD</description>
      <pubDate>Mon, 29 Jun 2009 12:12:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/String-to-split-into-words-using-macro/m-p/49642#M10321</guid>
      <dc:creator>BPD</dc:creator>
      <dc:date>2009-06-29T12:12:03Z</dc:date>
    </item>
  </channel>
</rss>

