<?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: Recursively filling rows in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Recursively-filling-rows/m-p/832537#M329071</link>
    <description>&lt;P&gt;Here you are&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/19879"&gt;@Quentin&lt;/a&gt;&amp;nbsp;. But it's obviuosly wrong:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data my_try;
	set have;
 	retain var;
	var = lag(var)*2;
 run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 09 Sep 2022 16:50:17 GMT</pubDate>
    <dc:creator>chris2377</dc:creator>
    <dc:date>2022-09-09T16:50:17Z</dc:date>
    <item>
      <title>Recursively filling rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Recursively-filling-rows/m-p/832534#M329069</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;Say I have a folllowing dataset:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	do i = 1 to 10;
		id = i;
		output;
	end;
	drop i;
run;
data have;
	set have;
	if id = 1 then var = 10;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;BR /&gt;Now I want to fill empty rows in a recursive way, so that value of each row equals to the value of previous row multiplied by two (so the values are 20, 40, 80 etc)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I've tried to use retain statement in different ways but failed to achieve what I want.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any help, please?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Best&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 09 Sep 2022 16:45:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Recursively-filling-rows/m-p/832534#M329069</guid>
      <dc:creator>chris2377</dc:creator>
      <dc:date>2022-09-09T16:45:08Z</dc:date>
    </item>
    <item>
      <title>Re: Recursively filling rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Recursively-filling-rows/m-p/832536#M329070</link>
      <description>&lt;P&gt;Please show the code you have tried, and describe whether you got errors or unexpected results.&amp;nbsp; It will help others help you.&lt;/P&gt;</description>
      <pubDate>Fri, 09 Sep 2022 16:48:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Recursively-filling-rows/m-p/832536#M329070</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2022-09-09T16:48:31Z</dc:date>
    </item>
    <item>
      <title>Re: Recursively filling rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Recursively-filling-rows/m-p/832537#M329071</link>
      <description>&lt;P&gt;Here you are&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/19879"&gt;@Quentin&lt;/a&gt;&amp;nbsp;. But it's obviuosly wrong:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data my_try;
	set have;
 	retain var;
	var = lag(var)*2;
 run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 09 Sep 2022 16:50:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Recursively-filling-rows/m-p/832537#M329071</guid>
      <dc:creator>chris2377</dc:creator>
      <dc:date>2022-09-09T16:50:17Z</dc:date>
    </item>
    <item>
      <title>Re: Recursively filling rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Recursively-filling-rows/m-p/832538#M329072</link>
      <description>&lt;P&gt;But close.&amp;nbsp; Because you are retaining, you don't need lag.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Consider something like:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  do i = 1 to 10;
    id = i;
    output;
  end;
  drop i;
run;

data want;
  set have;&lt;BR /&gt;
  retain var;&lt;BR /&gt;
  if id = 1 then var = 10;
  else var = var*2;

  put (id var)(=) ;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 09 Sep 2022 16:54:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Recursively-filling-rows/m-p/832538#M329072</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2022-09-09T16:54:32Z</dc:date>
    </item>
    <item>
      <title>Re: Recursively filling rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Recursively-filling-rows/m-p/832539#M329073</link>
      <description>&lt;P&gt;Since RETAIN is one of the most likely ways to accomplish this you should show what you tried and explain where it failed.&lt;/P&gt;
&lt;P&gt;Note RETAIN gets reset to a value if it is already in the data set. So if you attempted to Retain Var from the last 'Have' set it would get reset to missing for all except the first observation.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I don't see any "empty rows" to "fill" with anything. You have created a data set with missing values for a specific Variable.&lt;/P&gt;
&lt;P&gt;Perhaps this is what you are looking for.&lt;/P&gt;
&lt;PRE&gt;data want;
   set have;
   retain tempvar;
   if var then tempvar=var;
   else do;
      var=2*tempvar;
      tempvar=var;
   end;
   drop tempvar;
run;&lt;/PRE&gt;
&lt;P&gt;This will reset to the Var value when not missing, if there are any.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/24842"&gt;@chris2377&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;Say I have a folllowing dataset:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	do i = 1 to 10;
		id = i;
		output;
	end;
	drop i;
run;
data have;
	set have;
	if id = 1 then var = 10;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;BR /&gt;Now I want to fill empty rows in a recursive way, so that value of each row equals to the value of previous row multiplied by two (so the values are 20, 40, 80 etc)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I've tried to use retain statement in different ways but failed to achieve what I want.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any help, please?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Best&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;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 09 Sep 2022 16:57:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Recursively-filling-rows/m-p/832539#M329073</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-09-09T16:57:12Z</dc:date>
    </item>
    <item>
      <title>Re: Recursively filling rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Recursively-filling-rows/m-p/832565#M329078</link>
      <description>&lt;P&gt;My understanding of recursion is a function that calls itself.&amp;nbsp; &amp;nbsp;Thus a macro or proc fcmp would be ideal&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc fcmp outlib=work.funcs.myfunc;
function bytwo(n);
/* if n=1 then x=n; else x = n * 2; */&lt;BR /&gt;  x=ifn(n=1,1,(n * 2));
return(x);
endsub;
quit;

options cmplib=work.funcs;

* Recursion example ; 
 
data allinone;
    do i = 1 to 10;	
       var=bytwo(i);
		output;
	end;
run;
proc print;
run; 
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="ghosh_0-1662750144521.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/75101iA712F420FC912DDD/image-size/medium?v=v2&amp;amp;px=400" role="button" title="ghosh_0-1662750144521.png" alt="ghosh_0-1662750144521.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 10 Sep 2022 03:14:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Recursively-filling-rows/m-p/832565#M329078</guid>
      <dc:creator>ghosh</dc:creator>
      <dc:date>2022-09-10T03:14:18Z</dc:date>
    </item>
    <item>
      <title>Re: Recursively filling rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Recursively-filling-rows/m-p/832599#M329099</link>
      <description>&lt;P&gt;Use the RETAIN statement for two purposes: (1) to carry over values to subsequent observations, and (2) to initialize VAR to the desired value of 10 for the first observation.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then output BEFORE doubling var to carry over to the next observation:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  do id = 1 to 10;
    output;
  end;
run;

data want;
  set have;
  retain var 10;
  output;
  var=2*var;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 10 Sep 2022 01:17:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Recursively-filling-rows/m-p/832599#M329099</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2022-09-10T01:17:28Z</dc:date>
    </item>
    <item>
      <title>Re: Recursively filling rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Recursively-filling-rows/m-p/832847#M329230</link>
      <description>&lt;P&gt;Thanks&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;,&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/78622"&gt;@ghosh&lt;/a&gt;&amp;nbsp;,&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;&amp;nbsp;for your suggestions as well&lt;/P&gt;</description>
      <pubDate>Mon, 12 Sep 2022 10:00:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Recursively-filling-rows/m-p/832847#M329230</guid>
      <dc:creator>chris2377</dc:creator>
      <dc:date>2022-09-12T10:00:39Z</dc:date>
    </item>
  </channel>
</rss>

