<?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: Macro to split a string into variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-split-a-string-into-variables/m-p/60352#M13068</link>
    <description>As I read the details (beyond the SUBJECT), the OP posted a question about parsing a SAS dataset variable, not a SAS macro variable.&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.</description>
    <pubDate>Thu, 30 Jul 2009 17:55:34 GMT</pubDate>
    <dc:creator>sbb</dc:creator>
    <dc:date>2009-07-30T17:55:34Z</dc:date>
    <item>
      <title>Macro to split a string into variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-split-a-string-into-variables/m-p/60347#M13063</link>
      <description>Hi all,&lt;BR /&gt;
&lt;BR /&gt;
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.&lt;BR /&gt;
&lt;BR /&gt;
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.&lt;BR /&gt;
&lt;BR /&gt;
%macro split (name=);&lt;BR /&gt;
data one;&lt;BR /&gt;
&lt;BR /&gt;
%let i=1;&lt;BR /&gt;
j='x';&lt;BR /&gt;
  %do %while (j ne ' ');&lt;BR /&gt;
	word&amp;amp;i=%scan(&amp;amp;name,&amp;amp;i,' ');&lt;BR /&gt;
	j=word&amp;amp;i.;&lt;BR /&gt;
	output;&lt;BR /&gt;
   %let i=%eval (&amp;amp;i. + 1);&lt;BR /&gt;
   j=%scan(&amp;amp;name,&amp;amp;i,' ');&lt;BR /&gt;
   %end;&lt;BR /&gt;
&lt;BR /&gt;
run;&lt;BR /&gt;
%mend split;&lt;BR /&gt;
&lt;BR /&gt;
%split (name='sas is an analytical language');&lt;BR /&gt;
&lt;BR /&gt;
What may be the error in this code?</description>
      <pubDate>Thu, 30 Jul 2009 10:25:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-split-a-string-into-variables/m-p/60347#M13063</guid>
      <dc:creator>msg</dc:creator>
      <dc:date>2009-07-30T10:25:28Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to split a string into variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-split-a-string-into-variables/m-p/60348#M13064</link>
      <description>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.&lt;BR /&gt;
&lt;BR /&gt;
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.&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.</description>
      <pubDate>Thu, 30 Jul 2009 12:48:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-split-a-string-into-variables/m-p/60348#M13064</guid>
      <dc:creator>sbb</dc:creator>
      <dc:date>2009-07-30T12:48:14Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to split a string into variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-split-a-string-into-variables/m-p/60349#M13065</link>
      <description>Okay.&lt;BR /&gt;
&lt;BR /&gt;
The input is a string "sas is an analytical language".&lt;BR /&gt;
&lt;BR /&gt;
The output should be  5 variables (dynamically) such as &lt;BR /&gt;
&lt;BR /&gt;
Word1       Word2         Word3           Word4         Word5&lt;BR /&gt;
---------      ------------       -----------         -----------       --------------&lt;BR /&gt;
sas              is               an             analytical     language&lt;BR /&gt;
&lt;BR /&gt;
Hope my request is clear.</description>
      <pubDate>Thu, 30 Jul 2009 13:19:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-split-a-string-into-variables/m-p/60349#M13065</guid>
      <dc:creator>msg</dc:creator>
      <dc:date>2009-07-30T13:19:58Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to split a string into variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-split-a-string-into-variables/m-p/60350#M13066</link>
      <description>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.&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.</description>
      <pubDate>Thu, 30 Jul 2009 15:55:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-split-a-string-into-variables/m-p/60350#M13066</guid>
      <dc:creator>sbb</dc:creator>
      <dc:date>2009-07-30T15:55:12Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to split a string into variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-split-a-string-into-variables/m-p/60351#M13067</link>
      <description>%let string=sas is an analytical language;&lt;BR /&gt;
%let nwords=%word_count2(&amp;amp;string);&lt;BR /&gt;
&lt;BR /&gt;
The word_count2 macro is given at &lt;A href="http://support.sas.com/kb/26/152.html" target="_blank"&gt;http://support.sas.com/kb/26/152.html&lt;/A&gt; &lt;BR /&gt;
then you write a loop&lt;BR /&gt;
&lt;BR /&gt;
inside the loop&lt;BR /&gt;
&lt;BR /&gt;
%let word&amp;amp;i = %scan(&amp;amp;string,&amp;amp;i,%str( ));</description>
      <pubDate>Thu, 30 Jul 2009 17:24:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-split-a-string-into-variables/m-p/60351#M13067</guid>
      <dc:creator>Paige</dc:creator>
      <dc:date>2009-07-30T17:24:34Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to split a string into variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-split-a-string-into-variables/m-p/60352#M13068</link>
      <description>As I read the details (beyond the SUBJECT), the OP posted a question about parsing a SAS dataset variable, not a SAS macro variable.&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.</description>
      <pubDate>Thu, 30 Jul 2009 17:55:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-split-a-string-into-variables/m-p/60352#M13068</guid>
      <dc:creator>sbb</dc:creator>
      <dc:date>2009-07-30T17:55:34Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to split a string into variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-split-a-string-into-variables/m-p/60353#M13069</link>
      <description>Msg, think of the macro language as a code generator.&lt;BR /&gt;
&lt;BR /&gt;
The OP posted a question about parsing a macro variable into SAS dataset variables as I understand it.&lt;BR /&gt;
[pre]&lt;BR /&gt;
%macro split (name=);&lt;BR /&gt;
  data one;&lt;BR /&gt;
    %do i=1 %to %sysfunc(countw(&amp;amp;name));&lt;BR /&gt;
      word&amp;amp;i = "%scan(&amp;amp;name,&amp;amp;i)"; &lt;BR /&gt;
    %end;&lt;BR /&gt;
  run;&lt;BR /&gt;
%mend split;&lt;BR /&gt;
%split (name=sas is an analytical language)&lt;BR /&gt;
[/pre]&lt;BR /&gt;
or even all the logic in the data step:&lt;BR /&gt;
[pre]&lt;BR /&gt;
%macro split (name=);  &lt;BR /&gt;
  %let countw=%sysfunc(countw(&amp;amp;name));&lt;BR /&gt;
  data one(drop=i);    &lt;BR /&gt;
    array word(&amp;amp;countw) $16;&lt;BR /&gt;
    do i=1 to &amp;amp;countw;    &lt;BR /&gt;
      word[ i] = scan("&amp;amp;name",i);  &lt;BR /&gt;
    end; &lt;BR /&gt;
  run;&lt;BR /&gt;
%mend split;&lt;BR /&gt;
&lt;BR /&gt;
%split (name=sas is an analytical language)</description>
      <pubDate>Thu, 30 Jul 2009 23:07:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-split-a-string-into-variables/m-p/60353#M13069</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2009-07-30T23:07:56Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to split a string into variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-split-a-string-into-variables/m-p/60354#M13070</link>
      <description>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?&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.</description>
      <pubDate>Fri, 31 Jul 2009 00:52:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-split-a-string-into-variables/m-p/60354#M13070</guid>
      <dc:creator>sbb</dc:creator>
      <dc:date>2009-07-31T00:52:44Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to split a string into variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-split-a-string-into-variables/m-p/60355#M13071</link>
      <description>Thank you all for your suggestions. I will take a look and try today. &lt;BR /&gt;
&lt;BR /&gt;
Mine is SAS 8.2 and doesn't support COUNTW function.</description>
      <pubDate>Fri, 31 Jul 2009 05:30:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-split-a-string-into-variables/m-p/60355#M13071</guid>
      <dc:creator>msg</dc:creator>
      <dc:date>2009-07-31T05:30:35Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to split a string into variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-split-a-string-into-variables/m-p/60356#M13072</link>
      <description>OK then, you should use tho "old fashion way" word parsing.&lt;BR /&gt;
&lt;BR /&gt;
Just do the following modification to Chris code:&lt;BR /&gt;
&lt;BR /&gt;
%macro split (name=);&lt;BR /&gt;
  data one;&lt;BR /&gt;
   %let I=1;&lt;BR /&gt;
    %do %until(%scan(&amp;amp;name,&amp;amp;I) eq );&lt;BR /&gt;
      word&amp;amp;i = "%scan(&amp;amp;name,&amp;amp;I)"; &lt;BR /&gt;
     %let I=%eval(&amp;amp;I+1);&lt;BR /&gt;
    %end;&lt;BR /&gt;
  run;&lt;BR /&gt;
%mend split;&lt;BR /&gt;
%split (name=sas is an analytical language);&lt;BR /&gt;
&lt;BR /&gt;
Cheers from Portugal.&lt;BR /&gt;
&lt;BR /&gt;
Daniel Santos @ &lt;A href="http://www.cgd.pt" target="_blank"&gt;www.cgd.pt&lt;/A&gt;.</description>
      <pubDate>Fri, 31 Jul 2009 07:27:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-split-a-string-into-variables/m-p/60356#M13072</guid>
      <dc:creator>DanielSantos</dc:creator>
      <dc:date>2009-07-31T07:27:19Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to split a string into variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-split-a-string-into-variables/m-p/60357#M13073</link>
      <description>Thanks all for ur explanations. I got confused with the intreaction between macro and the data step. Now, it's fine. Thanks again.</description>
      <pubDate>Fri, 31 Jul 2009 08:43:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-split-a-string-into-variables/m-p/60357#M13073</guid>
      <dc:creator>msg</dc:creator>
      <dc:date>2009-07-31T08:43:48Z</dc:date>
    </item>
  </channel>
</rss>

