<?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: create new variables from substring in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/create-new-variables-from-substring/m-p/258829#M57404</link>
    <description>&lt;P&gt;You're in a data step, don't use macro functions.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Create an array to hold your new variables.&lt;/P&gt;
&lt;P&gt;Use the scan function to populate the variables.&lt;/P&gt;</description>
    <pubDate>Thu, 24 Mar 2016 14:30:14 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2016-03-24T14:30:14Z</dc:date>
    <item>
      <title>create new variables from substring</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/create-new-variables-from-substring/m-p/258817#M57400</link>
      <description>&lt;P&gt;Hello all,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a SAS data set has a variable names 'string' its value looks like:&amp;nbsp; "a1|a2|a3|a4",&amp;nbsp;"a1|a2",&amp;nbsp; "a1",&amp;nbsp;&amp;nbsp; "a1|a2|a3"&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;(some obs has less '|' and some has more "|")&amp;nbsp;;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to create new variables&amp;nbsp; x1=a1,x2=a2,x3=a3........ but it does not work,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;please help.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks!&lt;/P&gt;
&lt;P&gt;Here is my code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data have;&lt;/P&gt;
&lt;P&gt;string=&amp;nbsp;"a1|a2|a3|a4"; output;&lt;/P&gt;
&lt;P&gt;string=&amp;nbsp;"a1|a2"; output;&lt;/P&gt;
&lt;P&gt;string=&amp;nbsp;"a1|a2|a3"; output;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="2"&gt;&lt;STRONG&gt;data&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; want;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;length&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;&lt;FONT face="Courier New" size="2"&gt; x1 x2 x3 x4 x5 $&lt;/FONT&gt;&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="2"&gt;&lt;FONT color="#008080" face="Courier New" size="2"&gt;&lt;FONT color="#008080" face="Courier New" size="2"&gt;200&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="2"&gt;&lt;FONT face="Courier New" size="2"&gt; ;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;set&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;&lt;FONT face="Courier New" size="2"&gt; have;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;do&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;&lt;FONT face="Courier New" size="2"&gt; i=&lt;/FONT&gt;&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="2"&gt;&lt;FONT color="#008080" face="Courier New" size="2"&gt;&lt;FONT color="#008080" face="Courier New" size="2"&gt;1&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/STRONG&gt; &lt;FONT color="#0000ff" face="Courier New" size="2"&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;to &lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;countw(string,&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="2"&gt;"|"&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;)&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="2"&gt;&lt;FONT face="Courier New" size="2"&gt;;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;/*call&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;&lt;FONT face="Courier New" size="2"&gt; symput(&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="2"&gt;&lt;FONT color="#800080" face="Courier New" size="2"&gt;&lt;FONT color="#800080" face="Courier New" size="2"&gt;'x'&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;&lt;FONT face="Courier New" size="2"&gt;||left(_i_), scan(string,i,&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="2"&gt;&lt;FONT color="#800080" face="Courier New" size="2"&gt;&lt;FONT color="#800080" face="Courier New" size="2"&gt;"|"&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;&lt;FONT face="Courier New" size="2"&gt;));*/&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;x%trim(%left(i))=&lt;FONT face="Courier New" size="2"&gt;scan(string,i,&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="2"&gt;&lt;FONT color="#800080" face="Courier New" size="2"&gt;&lt;FONT color="#800080" face="Courier New" size="2"&gt;"|"&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;&lt;FONT face="Courier New" size="2"&gt;)&lt;/FONT&gt;&lt;/FONT&gt;;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;end&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;&lt;FONT face="Courier New" size="2"&gt;;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="2"&gt;&lt;FONT color="#000080" face="Courier New" size="2"&gt;&lt;FONT color="#000080" face="Courier New" size="2"&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;&lt;FONT face="Courier New" size="2"&gt;;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 24 Mar 2016 14:22:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/create-new-variables-from-substring/m-p/258817#M57400</guid>
      <dc:creator>GeorgeSAS</dc:creator>
      <dc:date>2016-03-24T14:22:41Z</dc:date>
    </item>
    <item>
      <title>Re: create new variables from substring</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/create-new-variables-from-substring/m-p/258825#M57402</link>
      <description>&lt;P&gt;You're picturing that macro language can work on DATA step variables, but that's not really the case.&amp;nbsp; For example, try:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%let x = %left(i);&lt;/P&gt;
&lt;P&gt;%put *&amp;amp;x*;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You'll see that %LEFT is left-hand justifying the character "i", and has nothing to do with the value of a DATA step variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Try setting up an array:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;array x {5} $200;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then inside the DO loop you can use:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;x{i} = scan(string, i, "|");&lt;/P&gt;</description>
      <pubDate>Thu, 24 Mar 2016 14:28:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/create-new-variables-from-substring/m-p/258825#M57402</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-03-24T14:28:48Z</dc:date>
    </item>
    <item>
      <title>Re: create new variables from substring</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/create-new-variables-from-substring/m-p/258826#M57403</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;One solution:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data want;&lt;BR /&gt;&lt;BR /&gt;length x1 x2 x3 x4 x5 $200 ;&lt;BR /&gt;&lt;BR /&gt;set have;&lt;BR /&gt;&lt;BR /&gt;array x{*} x1 x2 x3 x4 x5;&lt;BR /&gt;&lt;BR /&gt;do i=1 to dim(x) while (missing(scan(string,i,"|"))=0);&lt;BR /&gt;&lt;BR /&gt;x{i}=scan(string,i,"|");&lt;BR /&gt;&lt;BR /&gt;end;&lt;BR /&gt;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Thu, 24 Mar 2016 14:28:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/create-new-variables-from-substring/m-p/258826#M57403</guid>
      <dc:creator>Loko</dc:creator>
      <dc:date>2016-03-24T14:28:55Z</dc:date>
    </item>
    <item>
      <title>Re: create new variables from substring</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/create-new-variables-from-substring/m-p/258829#M57404</link>
      <description>&lt;P&gt;You're in a data step, don't use macro functions.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Create an array to hold your new variables.&lt;/P&gt;
&lt;P&gt;Use the scan function to populate the variables.&lt;/P&gt;</description>
      <pubDate>Thu, 24 Mar 2016 14:30:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/create-new-variables-from-substring/m-p/258829#M57404</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-03-24T14:30:14Z</dc:date>
    </item>
    <item>
      <title>Re: create new variables from substring</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/create-new-variables-from-substring/m-p/258860#M57405</link>
      <description>&lt;P&gt;You already have some good answers, below example has been presented many time by &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15410"&gt;@data_null__&lt;/a&gt;:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
	infile cards dsd truncover  dlm='|';

	if _n_=1 then
		input @@;
	set have;
	_infile_=string;
	length x1-x5 $200;
	input @1 x1-x5 @@;
	cards;
neccesary evil
;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 24 Mar 2016 15:41:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/create-new-variables-from-substring/m-p/258860#M57405</guid>
      <dc:creator>Haikuo</dc:creator>
      <dc:date>2016-03-24T15:41:47Z</dc:date>
    </item>
    <item>
      <title>Re: create new variables from substring</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/create-new-variables-from-substring/m-p/259007#M57412</link>
      <description>No. The first I saw this mixed code is Tom, not John King(data _null_) .</description>
      <pubDate>Fri, 25 Mar 2016 01:46:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/create-new-variables-from-substring/m-p/259007#M57412</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-03-25T01:46:57Z</dc:date>
    </item>
  </channel>
</rss>

