<?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 value with macro and loop in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Create-new-value-with-macro-and-loop/m-p/614291#M179528</link>
    <description>&lt;P&gt;Really thanks. It worked.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, how did 'k' in array worked?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;With 'do over j', j did loop with A, B, C.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But there was no 'do over' with k.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It would be bothering, I would really appreciate your explanation.&lt;/P&gt;</description>
    <pubDate>Sun, 29 Dec 2019 16:40:57 GMT</pubDate>
    <dc:creator>km0927</dc:creator>
    <dc:date>2019-12-29T16:40:57Z</dc:date>
    <item>
      <title>Create new value with macro and loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-new-value-with-macro-and-loop/m-p/614288#M179525</link>
      <description>&lt;P&gt;Hello, I'm new to use do loop with let macro.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to create several new variables, with if conditions on pre-existing variables.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Without using macro, the code will be this. A, B, C .. is old variable, _A, _B, _C ... is new variable.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
by id;
if first.id then _A=A;
if A=1 then _A=A;
else do;
retain _A.;
end;
run;

data want;
set have;
by id;
if first.id then _B=B;
if B=1 then _B=B;
else do;
retain _B.;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;... and so on.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;With searching, I found it is better to use let function and do loop. However, I'm new to this. I created the code below (only A, B, C), However, it wasn't working.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%Let var = A B C ;
%Let _var = _A _B _C ;

data want;
set have;

do i = 1 to 3;
by id;
if first.id then scan(&amp;amp;_var.,i,' ')=scan(&amp;amp;var.,i,' ');
if scan(&amp;amp;var.,i,' ')=1 then scan(&amp;amp;_var.,i,' ')=scan(&amp;amp;var.,i,' ')
else do; retain scan(&amp;amp;_var.,i,' ');
end;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;With log statement, there was problem in scan. It can't read 'A B C'. I hope somebody help me fixing this problem.&lt;/P&gt;</description>
      <pubDate>Sun, 29 Dec 2019 16:12:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-new-value-with-macro-and-loop/m-p/614288#M179525</guid>
      <dc:creator>km0927</dc:creator>
      <dc:date>2019-12-29T16:12:52Z</dc:date>
    </item>
    <item>
      <title>Re: Create new value with macro and loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-new-value-with-macro-and-loop/m-p/614289#M179526</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%Let var = A B C ;
%Let _var = _A _B _C ;

data want;
set have;
by id;
array j &amp;amp;var;
array k &amp;amp;_var;
retain k;
do over j;
 if first.id or j=1 then k=j;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/233577"&gt;@km0927&lt;/a&gt;&amp;nbsp; What you need is not macro i'm afraid rather an ARRAY. Please observe the grouping of var and _var in two sets of arrays and the loop that traverses through the elements of the arrays. The implicit array DO OVER makes it a very convenient syntax.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 29 Dec 2019 16:30:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-new-value-with-macro-and-loop/m-p/614289#M179526</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-12-29T16:30:59Z</dc:date>
    </item>
    <item>
      <title>Re: Create new value with macro and loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-new-value-with-macro-and-loop/m-p/614290#M179527</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/233577"&gt;@km0927&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You should typically use an array in this case.&lt;/P&gt;
&lt;P&gt;An array allow you to perform the same manipulation on a large range of variable.&lt;/P&gt;
&lt;P&gt;- First, you declare the array in an ARRAY statement -&amp;gt; ARRAY &amp;lt;name of the array&amp;gt; (*) &amp;lt;$ only if character variables&amp;gt; &amp;lt;your variables&amp;gt;;&lt;/P&gt;
&lt;P&gt;- Then you can loop through all the variables of the array by calling them by their name inside the array : for example, variable A is the first variable in the array "new" so you can call it old(1); variable B is equivalent to old(B); ...&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
	set have;
	by id;
	
	array old (*) A B C;
	array new (*) _A _B _C;
	
	do i=1 to dim(old);
		if first.id or old(i) = 1 then new(i)=old(i);
		else do;
			retain new;
		end;
	end;
	
	drop i;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 29 Dec 2019 16:31:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-new-value-with-macro-and-loop/m-p/614290#M179527</guid>
      <dc:creator>ed_sas_member</dc:creator>
      <dc:date>2019-12-29T16:31:23Z</dc:date>
    </item>
    <item>
      <title>Re: Create new value with macro and loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-new-value-with-macro-and-loop/m-p/614291#M179528</link>
      <description>&lt;P&gt;Really thanks. It worked.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, how did 'k' in array worked?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;With 'do over j', j did loop with A, B, C.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But there was no 'do over' with k.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It would be bothering, I would really appreciate your explanation.&lt;/P&gt;</description>
      <pubDate>Sun, 29 Dec 2019 16:40:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-new-value-with-macro-and-loop/m-p/614291#M179528</guid>
      <dc:creator>km0927</dc:creator>
      <dc:date>2019-12-29T16:40:57Z</dc:date>
    </item>
    <item>
      <title>Re: Create new value with macro and loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-new-value-with-macro-and-loop/m-p/614292#M179529</link>
      <description>&lt;P&gt;Sure&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/233577"&gt;@km0927&lt;/a&gt;&amp;nbsp; &amp;nbsp;the DO OVER array is an implicit array and hence there is no need to explicitly specify an index variable like &lt;EM&gt;do i=1 to n;&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Though implicit there is an index variable created in the PDV by the name _I_ , however this variable is not written to the output dataset. So every time the DO OVER loop executes, _I_ is incremented by a value of 1. In essence, what happens, when _I_ is 1 , the array reference is j(_I_) or k(_I_) for the reason both J and K are nonscalar items that points to an array that are collection of variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Since J and K array are grouped with same number of items like in your instance 3 elements, it is easy to reference and loop over one array J or K as _I_ increment can correspondingly be referred for elements in K or the other array.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hope that helps.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;PS Recommended reading IMPLICIT SAS DO OVER ARRAYS online&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;&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;y&lt;/P&gt;</description>
      <pubDate>Sun, 29 Dec 2019 16:53:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-new-value-with-macro-and-loop/m-p/614292#M179529</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-12-29T16:53:44Z</dc:date>
    </item>
  </channel>
</rss>

