<?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: Do loop in data step in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-in-data-step/m-p/303469#M64471</link>
    <description>&lt;P&gt;You messed up the order of your arguments in the CATX function, the delimiter is the first argument.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's your code, corrected, and an alternate method using CALL&amp;nbsp;SCAN to find the 3rd delimiter and then the SUBSTR/TRANSLATE function to do your conversion, and store it in a variable called &lt;STRONG&gt;&lt;EM&gt;want.&lt;/EM&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.sums_processed;
	_name_ = "A_B_1_2_3";

	First_value = scan(_NAME_,1,'_');
	Second_value = scan(_NAME_,2,'_');
		
	no_words = countw(_NAME_,'_');

	category = scan(_NAME_,3,'_');
		
	do i = 4 to (countw(_NAME_,'_'));
	   category = catx('.',category, scan(_NAME_,i,'_'));
	   
	end;
	
	call scan(_name_,3,pos,len,'_');
	want = translate(substr(_name_, pos), ".", "_");		

run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sun, 09 Oct 2016 19:50:36 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2016-10-09T19:50:36Z</dc:date>
    <item>
      <title>Do loop in data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-in-data-step/m-p/303460#M64466</link>
      <description>Hi,&lt;BR /&gt;&lt;BR /&gt;I have in a column values concatenated with underscores that I need to split in different columns. -Always the first 2 substrings represent same info and I use scan to read it. The problem is with the remaining info, which has variable length and I need to keep it concatenated.&lt;BR /&gt;&lt;BR /&gt;For example the input string is A_B_1_2. I know that a A and B are fixed but 1_2 could be also 1_2_3_4 so it has different length. The desired outcome is to have 1.2 or 1.2.3.4 (so replace also _ with dots).&lt;BR /&gt;&lt;BR /&gt;I tried in a data step to use a do loop and start from position 3 to maximum (which I find with countw function with the separator _) and use the scan with the index from the loop as position to concatenate each instance of the scan to the previous ones but this for some reason returns only either the 3rd or the last instance of the string. For sure I am doing something wrong but I have no clue what ...&lt;BR /&gt;&lt;BR /&gt;Could you please give me some ideas &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;BR /&gt;&lt;BR /&gt;Thanks</description>
      <pubDate>Sun, 09 Oct 2016 18:09:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-in-data-step/m-p/303460#M64466</guid>
      <dc:creator>_SAS_</dc:creator>
      <dc:date>2016-10-09T18:09:57Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop in data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-in-data-step/m-p/303462#M64467</link>
      <description>&lt;P&gt;SUBSTR() + TRANSLATE()&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For substr you only need the start of the string.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Translate will convert the underscores to periods. I'll leave that one up to you.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;X=substr(string, 5);&lt;/P&gt;</description>
      <pubDate>Sun, 09 Oct 2016 18:31:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-in-data-step/m-p/303462#M64467</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-10-09T18:31:45Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop in data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-in-data-step/m-p/303467#M64470</link>
      <description>&lt;P&gt;Hi and thank you for the quick reply.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The problem is to find the start position for the last chunk. The "A" and the "B" are also variable lenghts. They can be any number of characters. Also there is the possibility that A = B such that when I search for the position of the string with find I am not sure if I receive the first instance or the second one ...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I&amp;nbsp;don't understand if why the do&amp;nbsp;code doesn't work ... Considering my previous example with A_B_1_2_3, the first value should be A, the second should be B and then I need to have 1.2.3 ... However the code below returns "...1" What do I do wrong?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.sums_processed (compress=yes);
	set work.sums_prev;

	First_value = scan(_NAME_,1,'_');
	Second_value = scan(_NAME_,2,'_');
		
	no_words = countw(_NAME_,'_');

	category = scan(_NAME_,3,'_');
		
	do i = 4 to (countw(_NAME_,'_'));
	   category = catx(category,'.',scan(_NAME_,i,'_'));
	end;		

run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Thank you&lt;/P&gt;</description>
      <pubDate>Sun, 09 Oct 2016 19:18:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-in-data-step/m-p/303467#M64470</guid>
      <dc:creator>_SAS_</dc:creator>
      <dc:date>2016-10-09T19:18:18Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop in data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-in-data-step/m-p/303469#M64471</link>
      <description>&lt;P&gt;You messed up the order of your arguments in the CATX function, the delimiter is the first argument.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's your code, corrected, and an alternate method using CALL&amp;nbsp;SCAN to find the 3rd delimiter and then the SUBSTR/TRANSLATE function to do your conversion, and store it in a variable called &lt;STRONG&gt;&lt;EM&gt;want.&lt;/EM&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.sums_processed;
	_name_ = "A_B_1_2_3";

	First_value = scan(_NAME_,1,'_');
	Second_value = scan(_NAME_,2,'_');
		
	no_words = countw(_NAME_,'_');

	category = scan(_NAME_,3,'_');
		
	do i = 4 to (countw(_NAME_,'_'));
	   category = catx('.',category, scan(_NAME_,i,'_'));
	   
	end;
	
	call scan(_name_,3,pos,len,'_');
	want = translate(substr(_name_, pos), ".", "_");		

run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 09 Oct 2016 19:50:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-in-data-step/m-p/303469#M64471</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-10-09T19:50:36Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop in data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-in-data-step/m-p/303473#M64474</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza﻿&lt;/a&gt;&amp;nbsp;gave you a nice hint to a short code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;The end of the first two strings is the begining of the third string:&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; help&amp;nbsp;= scan(_NAME_,3,'_');&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; category_start = index(_NAME_, trim(&lt;SPAN&gt;help)&amp;nbsp;&lt;/SPAN&gt;); &amp;nbsp;/* equal to length of A_B_ */&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;A_B = substr(_NAME_,1,category_start - 1);&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;category = translate(substr(_NAME_, category_start), '.', '_');&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As to your code:&amp;nbsp;add &lt;STRONG&gt;length&lt;/STRONG&gt; statemnt for strings you get. Assign &lt;STRONG&gt;max&lt;/STRONG&gt; length expected&lt;/P&gt;&lt;P&gt;especially to category, otherwize it will be &lt;STRONG&gt;truncated&lt;/STRONG&gt; to its first assignment length !&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;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 09 Oct 2016 20:09:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-in-data-step/m-p/303473#M64474</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2016-10-09T20:09:47Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop in data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-in-data-step/m-p/303485#M64477</link>
      <description>&lt;P&gt;For completeness sake, you can also use regular expression matching to extract the substrings:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data sums_processed;
if not prxId then prxId + prxParse("/(.+?)_(.+?)_(.*)/");
_name_ = "AB_CD_DE_F_1_2_3";
if prxMatch(prxId, _name_) then do;
    first_value = prxPosn(prxId, 1, _name_);
    second_value = prxPosn(prxId, 2, _name_);
    category = translate(prxPosn(prxId, 3, _name_), ".", "_");
    end;
drop prxId;
run;

proc print; run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 10 Oct 2016 03:06:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-in-data-step/m-p/303485#M64477</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2016-10-10T03:06:11Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop in data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-in-data-step/m-p/303499#M64482</link>
      <description>&lt;P&gt;Thank you all for the great help!&lt;/P&gt;</description>
      <pubDate>Mon, 10 Oct 2016 05:31:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-in-data-step/m-p/303499#M64482</guid>
      <dc:creator>_SAS_</dc:creator>
      <dc:date>2016-10-10T05:31:56Z</dc:date>
    </item>
  </channel>
</rss>

