<?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 loops in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Inserting-chars-in-string-using-do-loops/m-p/889102#M351249</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/446448"&gt;@112211&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A different way of doing the same as&amp;nbsp;@Kurt Bremsers solution. There is nothing wrong with that, so this is just made to show that there are many ways in SAS to achieve the same result.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data names;
  input a $ score;
  cards;
virat 100
Charles 500 
william 250
Sachin 150
warwick 100
roger 120
taylor 152
jhonny 165
hunter 125
jackson 156
;
run;

data _null_; 
  set names (obs=1);
  call symputx('len',vlength(a)*2);
run;

data want;
  length a $&amp;amp;len;
  set names;
  a = substr(prxchange('s//*/',-1,a),2,(length(a)*2)-1);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The data _null_ step computes the length of the output variable by doubling the length of the input variable and storing the result in the macro variable len.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The macro variable is used in the next step &lt;EM&gt;before&lt;/EM&gt; the set statement to change the length of variable a. The prxchange function changes all occurrences of "nothing" to *, and the substring removes the resulting leading and trailing * to create the wanted output.&amp;nbsp;&amp;nbsp;The prxchange function can do things that makes a loop superflouos, but I don't think that makes it faster or more readable,&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 13 Aug 2023 13:35:07 GMT</pubDate>
    <dc:creator>ErikLund_Jensen</dc:creator>
    <dc:date>2023-08-13T13:35:07Z</dc:date>
    <item>
      <title>Inserting chars in string using do loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Inserting-chars-in-string-using-do-loops/m-p/889076#M351236</link>
      <description>&lt;P&gt;data name;&lt;BR /&gt;input a $ score;&lt;BR /&gt;cards;&lt;BR /&gt;virat 100&lt;BR /&gt;Charles 500&amp;nbsp;&lt;BR /&gt;william 250&lt;BR /&gt;Sachin 150&lt;BR /&gt;warwick 100&lt;BR /&gt;roger 120&lt;/P&gt;
&lt;P&gt;taylor 152&lt;BR /&gt;jhonny 165&lt;BR /&gt;hunter 125&lt;BR /&gt;jackson 156&lt;BR /&gt;;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;from using above data i need output should be following&lt;/P&gt;
&lt;P&gt;name&lt;/P&gt;
&lt;P&gt;v*i*r*a*t&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;100&lt;BR /&gt;c*h*a*r*l*e*s&amp;nbsp; &amp;nbsp; &amp;nbsp; 500&lt;BR /&gt;w*i*l*l*i*a*m&amp;nbsp; &amp;nbsp; &amp;nbsp; 250&lt;/P&gt;
&lt;P&gt;s*a*c*h*i*n&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;150&lt;BR /&gt;w*a*r*w*i*c*k&amp;nbsp; &amp;nbsp; 100&lt;BR /&gt;r*o*g*e*r&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;200&lt;BR /&gt;t*a*y*l*o*r&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 152&lt;BR /&gt;j*h*o*n*n*y&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;165&lt;BR /&gt;h*u*n*t*e*r&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 125&lt;BR /&gt;j*a*c*k*s*o*n&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;156&lt;/P&gt;</description>
      <pubDate>Tue, 15 Aug 2023 14:50:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Inserting-chars-in-string-using-do-loops/m-p/889076#M351236</guid>
      <dc:creator>112211</dc:creator>
      <dc:date>2023-08-15T14:50:40Z</dc:date>
    </item>
    <item>
      <title>Re: do loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Inserting-chars-in-string-using-do-loops/m-p/889077#M351237</link>
      <description>&lt;P&gt;The below datastep code can achieve what you want.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data name1;&lt;/P&gt;&lt;P&gt;length a_new $ 30;&lt;/P&gt;&lt;P&gt;set name;&lt;/P&gt;&lt;P&gt;do ii=1 to length(a);&lt;/P&gt;&lt;P&gt;if ii&amp;lt;length(a) then a_new=trim(a_new)||substr(a,ii,1)||"*";&lt;/P&gt;&lt;P&gt;else&amp;nbsp;a_new=trim(a_new)||substr(a,ii,1);&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;drop ii;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 13 Aug 2023 03:56:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Inserting-chars-in-string-using-do-loops/m-p/889077#M351237</guid>
      <dc:creator>Daniel_CGB</dc:creator>
      <dc:date>2023-08-13T03:56:31Z</dc:date>
    </item>
    <item>
      <title>Re: do loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Inserting-chars-in-string-using-do-loops/m-p/889083#M351239</link>
      <description>&lt;P&gt;You could copy each character of variable A into an array, then apply the CATX() function to the array to produce the new variable, with the '*' as a letter-separator.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data names;
input a $ score;
cards;
virat 100
Charles 500 
william 250
Sachin 150
warwick 100
roger 120
taylor 152
jhonny 165
hunter 125
jackson 156
;

data want (drop=_:);
  set names;
  array _letter {8} $1;
  length anew $15 ;
  do _n_=1 to length(a);
    _letter{_n_}=char(a,_n_);
  end;
  anew=catx('*',of _letter{*});
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 13 Aug 2023 05:40:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Inserting-chars-in-string-using-do-loops/m-p/889083#M351239</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2023-08-13T05:40:17Z</dc:date>
    </item>
    <item>
      <title>Re: do loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Inserting-chars-in-string-using-do-loops/m-p/889086#M351242</link>
      <description>&lt;P&gt;Was the change in case for Charles and Sachin (the only two in the example that start with capital letters) to c*h*a*r*l*e*s&amp;nbsp; and&amp;nbsp; s*a*c*h*i*n , now with lower case, actually intentional or just typos?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note your example Name variable with have a length of 8 characters. None of those resulting values will fit in 8 characters so you need to create a new variable to holder the longer values.&lt;/P&gt;</description>
      <pubDate>Sun, 13 Aug 2023 06:52:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Inserting-chars-in-string-using-do-loops/m-p/889086#M351242</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-08-13T06:52:53Z</dc:date>
    </item>
    <item>
      <title>Re: do loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Inserting-chars-in-string-using-do-loops/m-p/889087#M351243</link>
      <description>typo mistake</description>
      <pubDate>Sun, 13 Aug 2023 07:08:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Inserting-chars-in-string-using-do-loops/m-p/889087#M351243</guid>
      <dc:creator>112211</dc:creator>
      <dc:date>2023-08-13T07:08:15Z</dc:date>
    </item>
    <item>
      <title>Re: do loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Inserting-chars-in-string-using-do-loops/m-p/889089#M351245</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
select (2 * length) into :l from dictionary.columns
where libname = "WORK" snd memname = "NAME" and upcase(name) = "A";
quit;

data want;
length a $&amp;amp;l.;
set name (rename=(a=_a)),
do i = 1 to length(_a);
  a = catx("*",a,substr(_a,i,1));
end;
drop i _a;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Edit: forgot to add the multiplication in the first step. Corrected now.&lt;/P&gt;</description>
      <pubDate>Sun, 13 Aug 2023 13:59:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Inserting-chars-in-string-using-do-loops/m-p/889089#M351245</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-08-13T13:59:30Z</dc:date>
    </item>
    <item>
      <title>Re: do loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Inserting-chars-in-string-using-do-loops/m-p/889102#M351249</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/446448"&gt;@112211&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A different way of doing the same as&amp;nbsp;@Kurt Bremsers solution. There is nothing wrong with that, so this is just made to show that there are many ways in SAS to achieve the same result.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data names;
  input a $ score;
  cards;
virat 100
Charles 500 
william 250
Sachin 150
warwick 100
roger 120
taylor 152
jhonny 165
hunter 125
jackson 156
;
run;

data _null_; 
  set names (obs=1);
  call symputx('len',vlength(a)*2);
run;

data want;
  length a $&amp;amp;len;
  set names;
  a = substr(prxchange('s//*/',-1,a),2,(length(a)*2)-1);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The data _null_ step computes the length of the output variable by doubling the length of the input variable and storing the result in the macro variable len.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The macro variable is used in the next step &lt;EM&gt;before&lt;/EM&gt; the set statement to change the length of variable a. The prxchange function changes all occurrences of "nothing" to *, and the substring removes the resulting leading and trailing * to create the wanted output.&amp;nbsp;&amp;nbsp;The prxchange function can do things that makes a loop superflouos, but I don't think that makes it faster or more readable,&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 13 Aug 2023 13:35:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Inserting-chars-in-string-using-do-loops/m-p/889102#M351249</guid>
      <dc:creator>ErikLund_Jensen</dc:creator>
      <dc:date>2023-08-13T13:35:07Z</dc:date>
    </item>
    <item>
      <title>Re: do loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Inserting-chars-in-string-using-do-loops/m-p/889103#M351250</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12887"&gt;@ErikLund_Jensen&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/446448"&gt;@112211&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A different way of doing the same as&amp;nbsp;@Kurt Bremsers solution. There is nothing wrong with that, so this is just made to show that there are many ways in SAS to achieve the same result.&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Yup.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And if one wants not only to avoid any DO loop, but also generate discussion, you can utilize APP (address/peek/poke) functions:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data names;
  input a $ score;
  cards;
virat 100
Charles 500 
william 250
Sachin 150
warwick 100
roger 120
taylor 152
jhonny 165
hunter 125
jackson 156
run;
%let origvarlength=8;
data want (drop=_:);
  set names;
  array _letter {&amp;amp;origvarlength} $1;
  length new_a $%eval(2*&amp;amp;origvarlength -1) ;
  call pokelong(a,addrlong(_letter1));
  new_a=catx('*',of _letter{*});
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But the APP functions are effectively being deprecated by SAS, as they are not available in the heavily-promoted CAS.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am not recommending using the above, but for the curious, you can learn more in these papers, both by Paul Dorfman&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;&lt;A href="https://support.sas.com/resources/papers/proceedings09/010-2009.pdf" target="_self"&gt;From Obscurity to Utility: ADDR, PEEK, POKE as DATA Step Programming Tools&lt;/A&gt;&amp;nbsp;&lt;/LI&gt;
&lt;LI&gt;&lt;A title="Direct Memory Data Management Using the APP Tools" href="https://support.sas.com/resources/papers/proceedings14/1510-2014.pdf" target="_self"&gt;Direct Memory Data Management Using the APP Tools&lt;/A&gt;&amp;nbsp;&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 13 Aug 2023 14:50:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Inserting-chars-in-string-using-do-loops/m-p/889103#M351250</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2023-08-13T14:50:50Z</dc:date>
    </item>
    <item>
      <title>Re: do loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Inserting-chars-in-string-using-do-loops/m-p/889106#M351252</link>
      <description>&lt;P&gt;Using trick from this thread:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://communities.sas.com/t5/SAS-Programming/How-to-delimit-large-dataset-28-Million-rows-into-700-variables/m-p/486946#M287206" target="_blank"&gt;https://communities.sas.com/t5/SAS-Programming/How-to-delimit-large-dataset-28-Million-rows-into-700-variables/m-p/486946#M287206&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;proposed by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;and summarised by &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13868"&gt;@AhmedAl_Attar&lt;/a&gt;&amp;nbsp;you can do it like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data names;
input a :$20. score;
cards;
virat 100
Charles 500 
william 250
Sachin 150
warwick 100
roger 120
taylor 152
jhonny 165
hunter 125
jackson 156
;

filename d TEMP;
data _null_;
  file d;
  put;
run;

data want (drop=L:);
  set names;
  infile d;  
  input @1 @;
  _infile_ = a;
  input @1 (L1-L20) ($1.) @@;   /* 20 - length of a or less */

  anew=catx('*',of L:);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;All the best&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Sun, 13 Aug 2023 15:20:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Inserting-chars-in-string-using-do-loops/m-p/889106#M351252</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2023-08-13T15:20:04Z</dc:date>
    </item>
    <item>
      <title>Re: do loops</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Inserting-chars-in-string-using-do-loops/m-p/889107#M351253</link>
      <description>&lt;P&gt;BTW. If you decide to work with multilanguage data (in multi byte encoding like UTF-8) you should probably start using the K...() functions:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data names;
  input a :$20. score;
cards;
ŻÓŁĆ 123
空手道 456
;

data want (drop=i);
  set names;
  length aNew $ 200;
  
  do i = 1 to klength(a);
   aNew=catx('*', aNew, ksubstr(a,i,1));
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Sun, 13 Aug 2023 15:42:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Inserting-chars-in-string-using-do-loops/m-p/889107#M351253</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2023-08-13T15:42:58Z</dc:date>
    </item>
  </channel>
</rss>

