<?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: error extracting numeric from a text in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/error-extracting-numeric-from-a-text/m-p/477637#M123059</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data w;
k="[123/121/324/345]";
k1=compress(k,'[]');
grp=0;
do _n_=1 to countw(k1);
num=scan(k1,_n_,'/');
grp+1;
output;
end;
drop k1;
run;

proc transpose data=w out=want prefix=year;
by k;
var num;
id grp;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 12 Jul 2018 17:45:08 GMT</pubDate>
    <dc:creator>novinosrin</dc:creator>
    <dc:date>2018-07-12T17:45:08Z</dc:date>
    <item>
      <title>error extracting numeric from a text</title>
      <link>https://communities.sas.com/t5/SAS-Programming/error-extracting-numeric-from-a-text/m-p/477614#M123044</link>
      <description>&lt;P&gt;Dear friends at SAS community&lt;/P&gt;
&lt;P&gt;I have a dataset that has 4000 observations.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In this dataset, I have a&amp;nbsp;variable var1 which contains text data like this"[123/121/324/345]". &amp;nbsp;I need to extract the numbers as separate numeric variables. &amp;nbsp;For any observation, there can be up to 30 numbers in var1 that I need to extract:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;so I am trying to extract new1=123, new2=121, new3=324 and new4=345. &amp;nbsp; I tried the following code:&lt;/P&gt;
&lt;P&gt;new1=scan(compress(var1, &lt;SPAN&gt;'[]'&lt;/SPAN&gt;),&lt;SPAN&gt;&lt;STRONG&gt;1&lt;/STRONG&gt;&lt;/SPAN&gt;,&lt;SPAN&gt;'/'&lt;/SPAN&gt;);&lt;/P&gt;
&lt;P&gt;new2=scan(compress(var1, &lt;SPAN&gt;'[]'&lt;/SPAN&gt;),&lt;SPAN&gt;&lt;STRONG&gt;2&lt;/STRONG&gt;&lt;/SPAN&gt;,&lt;SPAN&gt;'/'&lt;/SPAN&gt;);&lt;/P&gt;
&lt;P&gt;new3=scan(compress(var1, &lt;SPAN&gt;'[]'&lt;/SPAN&gt;),&lt;SPAN&gt;&lt;STRONG&gt;3&lt;/STRONG&gt;&lt;/SPAN&gt;,&lt;SPAN&gt;'/'&lt;/SPAN&gt;);&lt;/P&gt;
&lt;P&gt;new4=scan(compress(var1, &lt;SPAN&gt;'[]'&lt;/SPAN&gt;),&lt;SPAN&gt;&lt;STRONG&gt;4&lt;/STRONG&gt;&lt;/SPAN&gt;,&lt;SPAN&gt;'/'&lt;/SPAN&gt;);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;but the results do not seem correct. &amp;nbsp;The last number is missing and never extracted. &amp;nbsp;Also, when an observation only has one number in var1, e.g.&amp;nbsp;var1="[123]", new1 is missing rather than 123 that I want to have. &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;so for var1="&lt;SPAN&gt;[123/121/324/345]"; I got new1=123, new2=121, new3=324, new4=. when I want new4=345&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;for var1="[123]", I got new1=. when I want new1=123&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;any thoughts of a solution?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;thanks&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Raymond&lt;/P&gt;</description>
      <pubDate>Thu, 12 Jul 2018 17:01:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/error-extracting-numeric-from-a-text/m-p/477614#M123044</guid>
      <dc:creator>rykwong</dc:creator>
      <dc:date>2018-07-12T17:01:59Z</dc:date>
    </item>
    <item>
      <title>Re: error extracting numeric from a text</title>
      <link>https://communities.sas.com/t5/SAS-Programming/error-extracting-numeric-from-a-text/m-p/477619#M123048</link>
      <description>&lt;P&gt;data w;&lt;BR /&gt;k="[123/121/324/345]";&lt;BR /&gt;k1=compress(k,'[]');&lt;BR /&gt;do _n_=1 to countw(k1);&lt;BR /&gt;num=scan(k1,_n_,'/');&lt;BR /&gt;output;&lt;BR /&gt;end;&lt;BR /&gt;drop k1;&lt;BR /&gt;run;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data w;
k="[123/121/324/345]";
k1=compress(k,'[]');
do _n_=1 to countw(k1);
num=scan(k1,_n_,'/');
output;
end;
drop k1;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 12 Jul 2018 17:06:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/error-extracting-numeric-from-a-text/m-p/477619#M123048</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-07-12T17:06:26Z</dc:date>
    </item>
    <item>
      <title>Re: error extracting numeric from a text</title>
      <link>https://communities.sas.com/t5/SAS-Programming/error-extracting-numeric-from-a-text/m-p/477634#M123056</link>
      <description>&lt;P&gt;dear novinosrin,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;thanks so much for your help. &amp;nbsp;one of such "var1" variables is cardiac_nuclear_tests_year so I ran the code below&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token procnames"&gt;data&lt;/SPAN&gt; w&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
k1&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token function"&gt;compress&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(cardiac_nuclear_tests_year&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;&lt;SPAN class="token string"&gt;'[]'&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
do _n_&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;1&lt;/SPAN&gt; to &lt;SPAN class="token function"&gt;countw&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;k1&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
num&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token function"&gt;scan&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;k1&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;_n_&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;&lt;SPAN class="token string"&gt;'/'&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
output&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
end&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token keyword"&gt;drop&lt;/SPAN&gt; k1&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token procnames"&gt;run&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I am still having problems, because this code puts num in a long format, instead of as separate variables year1, year2....year5. &amp;nbsp;Also other than cardiac_nuclear_tests_year, I have 10 other similar variables to extract from. &amp;nbsp; Any other thought? &amp;nbsp;many thanks&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 12 Jul 2018 17:41:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/error-extracting-numeric-from-a-text/m-p/477634#M123056</guid>
      <dc:creator>rykwong</dc:creator>
      <dc:date>2018-07-12T17:41:32Z</dc:date>
    </item>
    <item>
      <title>Re: error extracting numeric from a text</title>
      <link>https://communities.sas.com/t5/SAS-Programming/error-extracting-numeric-from-a-text/m-p/477637#M123059</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data w;
k="[123/121/324/345]";
k1=compress(k,'[]');
grp=0;
do _n_=1 to countw(k1);
num=scan(k1,_n_,'/');
grp+1;
output;
end;
drop k1;
run;

proc transpose data=w out=want prefix=year;
by k;
var num;
id grp;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 12 Jul 2018 17:45:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/error-extracting-numeric-from-a-text/m-p/477637#M123059</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-07-12T17:45:08Z</dc:date>
    </item>
    <item>
      <title>Re: error extracting numeric from a text</title>
      <link>https://communities.sas.com/t5/SAS-Programming/error-extracting-numeric-from-a-text/m-p/477639#M123061</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16220"&gt;@rykwong&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;dear novinosrin,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;thanks so much for your help. &amp;nbsp;one of such "var1" variables is cardiac_nuclear_tests_year so I ran the code below&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;&lt;SPAN class="token procnames"&gt;data&lt;/SPAN&gt; w&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt; k1&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token function"&gt;compress&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(cardiac_nuclear_tests_year&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;&lt;SPAN class="token string"&gt;'[]'&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt; do _n_&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;1&lt;/SPAN&gt; to &lt;SPAN class="token function"&gt;countw&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;k1&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt; num&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token function"&gt;scan&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;k1&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;_n_&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;&lt;SPAN class="token string"&gt;'/'&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt; output&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt; end&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt; &lt;SPAN class="token keyword"&gt;drop&lt;/SPAN&gt; k1&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt; &lt;SPAN class="token procnames"&gt;run&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/LI-CODE&gt;
&lt;P&gt;I am still having problems, because this code puts num in a long format, instead of as separate variables year1, year2....year5. &amp;nbsp;Also other than cardiac_nuclear_tests_year, I have 10 other similar variables to extract from. &amp;nbsp; Any other thought? &amp;nbsp;many thanks&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Perhaps:&lt;/P&gt;
&lt;PRE&gt;data w;
   set have;
   k1=compress(cardiac_nuclear_tests_year,'[]');
   array num{30};
   do i=1 to countw(k1);
      num[i]=scan(k1,i,'/');
   end;
   drop k1 i;
run;&lt;/PRE&gt;
&lt;P&gt;I consider creating a variable _n_ to be suboptimal as SAS has an automatic variable _n_ that indicates the iteration of the data step and could 1) be confusing in code and 2) mess up the count if needed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 12 Jul 2018 17:48:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/error-extracting-numeric-from-a-text/m-p/477639#M123061</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-07-12T17:48:33Z</dc:date>
    </item>
  </channel>
</rss>

