<?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: Using arrays to parse text strings in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Using-arrays-to-parse-text-strings/m-p/365585#M86842</link>
    <description>&lt;P&gt;Macro language example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data HAVE;
  retain RED_T1   1 RED_T2   2 RED_T3   4 
         BLUE_T1  1 BLUE_T2  3 BLUE_T3  6 
         WHITE_T1 1 WHITE_T2 4 WHITE_T3 8 
         GREEN_T1 1 GREEN_T2 4 GREEN_T3 9 ;
run;

%macro diffs;
%local names name namenb time;
%let names=RED BLUE WHITE GREEN;
data WANT;
  set HAVE;
  %do namenb=1 %to %sysfunc(countw(&amp;amp;names,%str( )));
    %let name=%scan(&amp;amp;names,&amp;amp;namenb,%str( ));
    %do time=1 %to 2;
      &amp;amp;name._D%eval(&amp;amp;time+1)=&amp;amp;name._T%eval(&amp;amp;time+1)-&amp;amp;name._T&amp;amp;time;
    %end;
  %end;
run;      
%mend;
%diffs;
 
proc print noobs; 
  var  RED_D2-RED_D3  BLUE_D2-BLUE_D3  WHITE_D2-WHITE_D3  GREEN_D2-GREEN_D3;
run;


&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;DIV class="branch"&gt;
&lt;DIV&gt;
&lt;DIV align="center"&gt;
&lt;TABLE class="table" summary="Procedure Print: Data Set WORK.WANT" frame="box" rules="all" cellpadding="5" cellspacing="0"&gt;
&lt;THEAD&gt;
&lt;TR&gt;
&lt;TH class="r header" scope="col"&gt;RED_D2&lt;/TH&gt;
&lt;TH class="r header" scope="col"&gt;RED_D3&lt;/TH&gt;
&lt;TH class="r header" scope="col"&gt;BLUE_D2&lt;/TH&gt;
&lt;TH class="r header" scope="col"&gt;BLUE_D3&lt;/TH&gt;
&lt;TH class="r header" scope="col"&gt;WHITE_D2&lt;/TH&gt;
&lt;TH class="r header" scope="col"&gt;WHITE_D3&lt;/TH&gt;
&lt;TH class="r header" scope="col"&gt;GREEN_D2&lt;/TH&gt;
&lt;TH class="r header" scope="col"&gt;GREEN_D3&lt;/TH&gt;
&lt;/TR&gt;
&lt;/THEAD&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;1&lt;/TD&gt;
&lt;TD class="r data"&gt;2&lt;/TD&gt;
&lt;TD class="r data"&gt;2&lt;/TD&gt;
&lt;TD class="r data"&gt;3&lt;/TD&gt;
&lt;TD class="r data"&gt;3&lt;/TD&gt;
&lt;TD class="r data"&gt;4&lt;/TD&gt;
&lt;TD class="r data"&gt;3&lt;/TD&gt;
&lt;TD class="r data"&gt;5&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;</description>
    <pubDate>Fri, 09 Jun 2017 02:49:45 GMT</pubDate>
    <dc:creator>ChrisNZ</dc:creator>
    <dc:date>2017-06-09T02:49:45Z</dc:date>
    <item>
      <title>Using arrays to parse text strings</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-arrays-to-parse-text-strings/m-p/365572#M86840</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a dataset with lots of variables that capture the numbers of each type of product that a customer has at different points in time. The time components of all the variables are labelled in the same fashion but the text referring to the product differs.&lt;/P&gt;&lt;P&gt;E.g. red_t1, red_t2, red_t3,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; blue_t1, blue_t2, blue_t3,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; white_t1, white_t2, white_t3&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to create a series of difference variables and use some sort of array to parse the product names to make the code more efficient. Ideally, I'm thinking something like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data new;
set old;

array prod[3] red blue white;

do i = 1 to 3;

        prod[i]_diff2=prod[i]_t2-prod[i]_t1;
        prod[i]_diff3=prod[i]_t3-prod[i]_t2;

end;

run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, I haven't been able to figure out a way to use arrays to parse text rather than refer to an actual variable.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help would be greatly appreciated! Thank-you.&lt;/P&gt;</description>
      <pubDate>Fri, 09 Jun 2017 02:01:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-arrays-to-parse-text-strings/m-p/365572#M86840</guid>
      <dc:creator>_Rachael_</dc:creator>
      <dc:date>2017-06-09T02:01:12Z</dc:date>
    </item>
    <item>
      <title>Re: Using arrays to parse text strings</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-arrays-to-parse-text-strings/m-p/365580#M86841</link>
      <description>&lt;P&gt;LIke this?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data HAVE;
  retain RED_T1   4 RED_T2   2 RED_T3   1
         BLUE_T1  6 BLUE_T2  3 BLUE_T3  1
         WHITE_T1 8 WHITE_T2 4 WHITE_T3 1
         GREEN_T1 9 GREEN_T2 4 GREEN_T3 1;
run;
data WANT;
  set HAVE;
  array COUNTS[*] RED_T1-RED_T3  BLUE_T1-BLUE_T3  WHITE_T1-WHITE_T3  GREEN_T1-GREEN_T3;
  array DIFFS[*]  RED_D2-RED_D3  BLUE_D2-BLUE_D3  WHITE_D2-WHITE_D3  GREEN_D2-GREEN_D3;
  do PRODNB=0 to 3;
    do TIME=1 to 2;
      DIFFS[PRODNB*2+TIME]=COUNTS[PRODNB*3+TIME]-COUNTS[PRODNB*3+TIME+1];
    end;
  end;
run;      
    
proc print noobs; 
  var  RED_D2-RED_D3  BLUE_D2-BLUE_D3  WHITE_D2-WHITE_D3  GREEN_D2-GREEN_D3;
run;


&lt;/CODE&gt;&lt;/PRE&gt;
&lt;DIV class="branch"&gt;
&lt;DIV&gt;
&lt;DIV align="center"&gt;
&lt;TABLE class="table" summary="Procedure Print: Data Set WORK.WANT" frame="box" rules="all" cellpadding="5" cellspacing="0"&gt;
&lt;THEAD&gt;
&lt;TR&gt;
&lt;TH class="r header" scope="col"&gt;RED_D2&lt;/TH&gt;
&lt;TH class="r header" scope="col"&gt;RED_D3&lt;/TH&gt;
&lt;TH class="r header" scope="col"&gt;BLUE_D2&lt;/TH&gt;
&lt;TH class="r header" scope="col"&gt;BLUE_D3&lt;/TH&gt;
&lt;TH class="r header" scope="col"&gt;WHITE_D2&lt;/TH&gt;
&lt;TH class="r header" scope="col"&gt;WHITE_D3&lt;/TH&gt;
&lt;TH class="r header" scope="col"&gt;GREEN_D2&lt;/TH&gt;
&lt;TH class="r header" scope="col"&gt;GREEN_D3&lt;/TH&gt;
&lt;/TR&gt;
&lt;/THEAD&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;2&lt;/TD&gt;
&lt;TD class="r data"&gt;1&lt;/TD&gt;
&lt;TD class="r data"&gt;3&lt;/TD&gt;
&lt;TD class="r data"&gt;2&lt;/TD&gt;
&lt;TD class="r data"&gt;4&lt;/TD&gt;
&lt;TD class="r data"&gt;3&lt;/TD&gt;
&lt;TD class="r data"&gt;5&lt;/TD&gt;
&lt;TD class="r data"&gt;3&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;DIV class="branch"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV class="branch"&gt;If you want the names to be more abstracted then you'll need to use the macro language.&lt;/DIV&gt;</description>
      <pubDate>Fri, 09 Jun 2017 02:33:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-arrays-to-parse-text-strings/m-p/365580#M86841</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2017-06-09T02:33:25Z</dc:date>
    </item>
    <item>
      <title>Re: Using arrays to parse text strings</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-arrays-to-parse-text-strings/m-p/365585#M86842</link>
      <description>&lt;P&gt;Macro language example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data HAVE;
  retain RED_T1   1 RED_T2   2 RED_T3   4 
         BLUE_T1  1 BLUE_T2  3 BLUE_T3  6 
         WHITE_T1 1 WHITE_T2 4 WHITE_T3 8 
         GREEN_T1 1 GREEN_T2 4 GREEN_T3 9 ;
run;

%macro diffs;
%local names name namenb time;
%let names=RED BLUE WHITE GREEN;
data WANT;
  set HAVE;
  %do namenb=1 %to %sysfunc(countw(&amp;amp;names,%str( )));
    %let name=%scan(&amp;amp;names,&amp;amp;namenb,%str( ));
    %do time=1 %to 2;
      &amp;amp;name._D%eval(&amp;amp;time+1)=&amp;amp;name._T%eval(&amp;amp;time+1)-&amp;amp;name._T&amp;amp;time;
    %end;
  %end;
run;      
%mend;
%diffs;
 
proc print noobs; 
  var  RED_D2-RED_D3  BLUE_D2-BLUE_D3  WHITE_D2-WHITE_D3  GREEN_D2-GREEN_D3;
run;


&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;DIV class="branch"&gt;
&lt;DIV&gt;
&lt;DIV align="center"&gt;
&lt;TABLE class="table" summary="Procedure Print: Data Set WORK.WANT" frame="box" rules="all" cellpadding="5" cellspacing="0"&gt;
&lt;THEAD&gt;
&lt;TR&gt;
&lt;TH class="r header" scope="col"&gt;RED_D2&lt;/TH&gt;
&lt;TH class="r header" scope="col"&gt;RED_D3&lt;/TH&gt;
&lt;TH class="r header" scope="col"&gt;BLUE_D2&lt;/TH&gt;
&lt;TH class="r header" scope="col"&gt;BLUE_D3&lt;/TH&gt;
&lt;TH class="r header" scope="col"&gt;WHITE_D2&lt;/TH&gt;
&lt;TH class="r header" scope="col"&gt;WHITE_D3&lt;/TH&gt;
&lt;TH class="r header" scope="col"&gt;GREEN_D2&lt;/TH&gt;
&lt;TH class="r header" scope="col"&gt;GREEN_D3&lt;/TH&gt;
&lt;/TR&gt;
&lt;/THEAD&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;1&lt;/TD&gt;
&lt;TD class="r data"&gt;2&lt;/TD&gt;
&lt;TD class="r data"&gt;2&lt;/TD&gt;
&lt;TD class="r data"&gt;3&lt;/TD&gt;
&lt;TD class="r data"&gt;3&lt;/TD&gt;
&lt;TD class="r data"&gt;4&lt;/TD&gt;
&lt;TD class="r data"&gt;3&lt;/TD&gt;
&lt;TD class="r data"&gt;5&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;</description>
      <pubDate>Fri, 09 Jun 2017 02:49:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-arrays-to-parse-text-strings/m-p/365585#M86842</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2017-06-09T02:49:45Z</dc:date>
    </item>
    <item>
      <title>Re: Using arrays to parse text strings</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-arrays-to-parse-text-strings/m-p/365596#M86846</link>
      <description>&lt;P&gt;Thanks very much for your help Chris.&lt;/P&gt;&lt;P&gt;The time periods are not expressed quite so elegantly as in my example, so I definitely want the added flexibility of the macro solution.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It is a shame that this requires it's own data step though. It would be great if SAS had a way of programming it as simply as I'd hoped. In the meantime, thanks, I'll be sharing this solution with my colleagues and I'm sure it will get a great work out!&lt;/P&gt;</description>
      <pubDate>Fri, 09 Jun 2017 04:16:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-arrays-to-parse-text-strings/m-p/365596#M86846</guid>
      <dc:creator>_Rachael_</dc:creator>
      <dc:date>2017-06-09T04:16:15Z</dc:date>
    </item>
    <item>
      <title>Re: Using arrays to parse text strings</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-arrays-to-parse-text-strings/m-p/365598#M86848</link>
      <description>&lt;P&gt;No worries. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;gt; this requires its own data step&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;I doesn't:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data WANT;
  set HAVE;

 &amp;lt;more code&amp;gt;

%macro diffs;
%local names name namenb time;
%let names=RED BLUE WHITE GREEN;
  %do namenb=1 %to %sysfunc(countw(&amp;amp;names,%str( )));
    %let name=%scan(&amp;amp;names,&amp;amp;namenb,%str( ));
    %do time=1 %to 2;
      &amp;amp;name._D%eval(&amp;amp;time+1)=&amp;amp;name._T%eval(&amp;amp;time+1)-&amp;amp;name._T&amp;amp;time;
    %end;
  %end;
%mend;
%diffs;

&amp;lt;more code&amp;gt;

run; 
 &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 09 Jun 2017 04:22:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-arrays-to-parse-text-strings/m-p/365598#M86848</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2017-06-09T04:22:57Z</dc:date>
    </item>
    <item>
      <title>Re: Using arrays to parse text strings</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-arrays-to-parse-text-strings/m-p/365599#M86849</link>
      <description>&lt;P&gt;Thanks Chris, you're right, I can do that. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 09 Jun 2017 04:46:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-arrays-to-parse-text-strings/m-p/365599#M86849</guid>
      <dc:creator>_Rachael_</dc:creator>
      <dc:date>2017-06-09T04:46:27Z</dc:date>
    </item>
  </channel>
</rss>

