<?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: Fill down multiple rows in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Fill-down-multiple-rows/m-p/684619#M207498</link>
    <description>If the values of any variables in the last.id are missing, then fill down (row 5 per id) from row above (row 4 per id). I have accomplished this with the following code:&lt;BR /&gt;data want;&lt;BR /&gt;update have (obs=0) have;&lt;BR /&gt;by id;&lt;BR /&gt;output;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;Now, I need to delete the values that filled down from row 4. Row 4 now contains the missing values. Then I would like to fill down (row 4) from row above (row 3). Then delete filled down values from row 3. Fill down from row 2. Delete row 2 filled down value. Then fill down from row 1. In the data want, row 1 contains the missing values.&lt;BR /&gt;</description>
    <pubDate>Thu, 17 Sep 2020 14:21:14 GMT</pubDate>
    <dc:creator>eabc0351</dc:creator>
    <dc:date>2020-09-17T14:21:14Z</dc:date>
    <item>
      <title>Fill down multiple rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Fill-down-multiple-rows/m-p/684612#M207493</link>
      <description>&lt;P&gt;Hi all. I have the following data:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id day1 day2 day3 day4;
datalines;
1 -23 -22 -21 -20
1 -19 -18 -17 -16
1 -15 -14 -13 -12
1 -11 -10 -9 -8 
1 -7 -6 . . 
2 -20 -19 -18 -17 
2 -16 -15 -14 -13
2 -12 -11 -9 -8
2 -7 -6 -5 -4
2 -3 -2 -1 .  
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I would like the following data:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
input id day1 day2 day3 day4;
datalines;
1 -23 -22 . .
1 -19 -18 -21 -20
1 -15 -14 -17 -16
1 -11 -10 -13 -12
1 -7 -6 -9 -8
2 -20 -19 -18 . 
2 -16 -15 -14 -17 
2 -12 -11 -9 -13
2 -7 -6 -5 -8
2 -3 -2 -1 -4
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Each ID has 5 observations only. I am looking to do this for all the variables in the table, not just the 4 that are listed here. Thank you for your assistance!&lt;/P&gt;</description>
      <pubDate>Thu, 17 Sep 2020 14:06:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Fill-down-multiple-rows/m-p/684612#M207493</guid>
      <dc:creator>eabc0351</dc:creator>
      <dc:date>2020-09-17T14:06:22Z</dc:date>
    </item>
    <item>
      <title>Re: Fill down multiple rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Fill-down-multiple-rows/m-p/684616#M207496</link>
      <description>&lt;P&gt;Please explain the logic that lets you go from data set HAVE to data set WANT.&lt;/P&gt;</description>
      <pubDate>Thu, 17 Sep 2020 14:15:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Fill-down-multiple-rows/m-p/684616#M207496</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-09-17T14:15:02Z</dc:date>
    </item>
    <item>
      <title>Re: Fill down multiple rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Fill-down-multiple-rows/m-p/684618#M207497</link>
      <description>&lt;P&gt;Why does ID 1 have two variables "moved" and ID 2 only 1 (apparently). Do different ID values have different rules? If so then you need to explain the rules for each ID.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For "all variables", what are the differing rules involving which variables for which ids?&lt;/P&gt;</description>
      <pubDate>Thu, 17 Sep 2020 14:20:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Fill-down-multiple-rows/m-p/684618#M207497</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-09-17T14:20:58Z</dc:date>
    </item>
    <item>
      <title>Re: Fill down multiple rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Fill-down-multiple-rows/m-p/684619#M207498</link>
      <description>If the values of any variables in the last.id are missing, then fill down (row 5 per id) from row above (row 4 per id). I have accomplished this with the following code:&lt;BR /&gt;data want;&lt;BR /&gt;update have (obs=0) have;&lt;BR /&gt;by id;&lt;BR /&gt;output;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;Now, I need to delete the values that filled down from row 4. Row 4 now contains the missing values. Then I would like to fill down (row 4) from row above (row 3). Then delete filled down values from row 3. Fill down from row 2. Delete row 2 filled down value. Then fill down from row 1. In the data want, row 1 contains the missing values.&lt;BR /&gt;</description>
      <pubDate>Thu, 17 Sep 2020 14:21:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Fill-down-multiple-rows/m-p/684619#M207498</guid>
      <dc:creator>eabc0351</dc:creator>
      <dc:date>2020-09-17T14:21:14Z</dc:date>
    </item>
    <item>
      <title>Re: Fill down multiple rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Fill-down-multiple-rows/m-p/684631#M207505</link>
      <description>There are not different rules for each ID. I have 7 variables to indicate day (day1-day7). For the last.id, each person could have at least one missing and up to 7 missing values for these day variables. For example:&lt;BR /&gt;ID=1 might have missing values for day4-day7 in last.id. But ID=2 might have missing values for day2-day7.&lt;BR /&gt;Then there are corresponding varaibles to the days that describe peoples drug use on those days. For instance:&lt;BR /&gt;ID=1 would have a 1 or 0 for druguse1-druguse3 but missing values for druguse4-druguse7 in last.id, corresponding to the missing values for the days.&lt;BR /&gt;ID=2 the missing values in last.id would be druguse2-druguse7.&lt;BR /&gt;Because there are so many variables, I was hoping to fill down to row 5 from row above (row 4) if variables are missing using this code:&lt;BR /&gt;data want;&lt;BR /&gt;update have (obs=0) have;&lt;BR /&gt;by id;&lt;BR /&gt;output;&lt;BR /&gt;run;&lt;BR /&gt;Then I was thinking it may be possible to delete all the values that filled down from row 4. Then fill down from row 3 to rown 4, delete values from row that that went down... and so on. The missing values would end up in first.id instead of last.id.&lt;BR /&gt;If this logic will not work, then please let me know, and I can find another way.&lt;BR /&gt;</description>
      <pubDate>Thu, 17 Sep 2020 14:33:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Fill-down-multiple-rows/m-p/684631#M207505</guid>
      <dc:creator>eabc0351</dc:creator>
      <dc:date>2020-09-17T14:33:42Z</dc:date>
    </item>
    <item>
      <title>Re: Fill down multiple rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Fill-down-multiple-rows/m-p/684632#M207506</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input id day1 day2 day3 day4;
datalines;
1 -23 -22 -21 -20
1 -19 -18 -17 -16
1 -15 -14 -13 -12
1 -11 -10 -9 -8 
1 -7 -6 . 5 
2 -20 -19 -18 -17 
2 -16 -15 -14 -13
2 -12 -11 -9 -8
2 -7 -6 -5 -4
2 -3 -2 -1 .  
;
run;


data want;
 do until(last.id);
  set have;
  by id;
  array d day:;
  array t(4) _temporary_;/*Expand the array subscript to the actual you have*/
 end;
 do over d;
  if d=. then t(_i_)=_i_;
 end;
 do until(last.id);
  set have;
  by id;
  do over d;
   d=ifn(_i_ in t ,lag(d),d);
   if _i_ in t and first.id then d=.;
  end;
  output;
 end;
 call missing(of t(*));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;EDITED:&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/148180"&gt;@eabc0351&lt;/a&gt;&amp;nbsp; To include "&lt;EM&gt;&lt;FONT face="book antiqua,palatino"&gt;and not first.id"---&amp;gt;&amp;nbsp;d=ifn(_i_ in t ,lag(d),d);&lt;BR /&gt;if _i_ in t and first.id then d=.;&lt;/FONT&gt;&lt;/EM&gt;&lt;STRONG&gt;&lt;EM&gt;&amp;nbsp;&amp;nbsp;&lt;/EM&gt;&lt;/STRONG&gt;Please take notice!&lt;/P&gt;</description>
      <pubDate>Thu, 17 Sep 2020 15:42:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Fill-down-multiple-rows/m-p/684632#M207506</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-09-17T15:42:03Z</dc:date>
    </item>
    <item>
      <title>Re: Fill down multiple rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Fill-down-multiple-rows/m-p/684640#M207512</link>
      <description>&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;, thank you. That worked for the day variable. I would like to expand this code for all the drug variables now. For example, I have the following groups in addition to day1-day7:&lt;BR /&gt;alc1-alc7&lt;BR /&gt;can1-can7&lt;BR /&gt;coc1-coc7&lt;BR /&gt;her1-her7... and so on. There are about 20 groups of these drug varaibles.&lt;BR /&gt;I would like to do the same thing we did for day for the remainder of the variables.&lt;BR /&gt;How would I expand this array to incorporate all of those groups?&lt;BR /&gt;</description>
      <pubDate>Thu, 17 Sep 2020 14:47:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Fill-down-multiple-rows/m-p/684640#M207512</guid>
      <dc:creator>eabc0351</dc:creator>
      <dc:date>2020-09-17T14:47:23Z</dc:date>
    </item>
    <item>
      <title>Re: Fill down multiple rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Fill-down-multiple-rows/m-p/684651#M207515</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/148180"&gt;@eabc0351&lt;/a&gt;&amp;nbsp; So you have 20 groups with 7 variables in each group..Is that correct?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If yes, you have 140 variables in total.&lt;/P&gt;
&lt;P&gt;Group the 140 variables carefully(&lt;EM&gt;&lt;STRONG&gt;for the array index to evaluate&lt;/STRONG&gt;&lt;/EM&gt;)&lt;U&gt;&lt;STRONG&gt; left to right&lt;/STRONG&gt;&lt;/U&gt; in one array like-;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;array day1-day7 alc1-alc7; etc&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;and initialize&amp;nbsp; the other temporary array with 140 elements like&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt; array t(140) _temporary_;&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please try the above suggestion. I am sure you can whilst we folks are&lt;EM&gt;&lt;STRONG&gt; lazy&lt;/STRONG&gt; &lt;/EM&gt;to type;&lt;/P&gt;</description>
      <pubDate>Thu, 17 Sep 2020 15:01:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Fill-down-multiple-rows/m-p/684651#M207515</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-09-17T15:01:27Z</dc:date>
    </item>
    <item>
      <title>Re: Fill down multiple rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Fill-down-multiple-rows/m-p/684652#M207516</link>
      <description>It worked. Thanks so much!</description>
      <pubDate>Thu, 17 Sep 2020 15:05:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Fill-down-multiple-rows/m-p/684652#M207516</guid>
      <dc:creator>eabc0351</dc:creator>
      <dc:date>2020-09-17T15:05:06Z</dc:date>
    </item>
    <item>
      <title>Re: Fill down multiple rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Fill-down-multiple-rows/m-p/684655#M207518</link>
      <description>&lt;P&gt;I'll attribute my misinterpretation of the objective to &lt;STRIKE&gt;laziness&lt;/STRIKE&gt; forgetting the nature of asynchronous communication -&amp;nbsp; I only saw the initial topic introduction and made the wrong assumption. While I was multitasking and generating the response (struck out below), clarification happened and was neglected by me.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You already have a great solution by &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt; , but in the spirit of my earlier reply here is a corrected approach in the same style as my first.&amp;nbsp; It's main difference from the accepted solution is that it uses a single SET statement with HAVE repeated as the target, vs having two SETs each targetting HAVE once.&amp;nbsp; You can see the structural differences that implies:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id day1 day2 day3 day4;
datalines;
1 -23 -22 -21 -20
1 -19 -18 -17 -16
1 -15 -14 -13 -12
1 -11 -10 -9 -8
1 -7 -6 . .
2 -20 -19 -18 -17
2 -16 -15 -14 -13
2 -12 -11 -9 -8
2 -7 -6 -5 -4
2 -3 -2 -1 .
run;

data want (drop=_:);
  set have (in=firstpass) 
      have (in=secondpass);
  by id;

  array d _numeric_;
  array  _endmiss    _endmiss1-_endmiss5;
  retain _endmiss:   _start_secondpass;

  _start_secondpass=dif(secondpass);
  do over d;
    if firstpass=1 then _endmiss=ifn(d=.,1,0);
	d=ifn(secondpass and _endmiss=1,lag(d),d);
	if _start_secondpass=1 and _endmiss=1 then d=.;
  end;
  if secondpass;
run;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The SET with two HAVEs with a BY statement reads each ID group twice.&amp;nbsp; Use the first pass to establish the _ENDMISS flags.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The DIF function is X-lag(X), so _start_secondpass=1 only when the second pass of each id group starts.&amp;nbsp; The allows lagged responses to be replace by missing at the start of each ID group output.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Make sure the ARRAY statement for _ENDMISS is long enough to accommodate all the variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Erroneous response struct out below:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRIKE&gt;You want to carry-forward by one observation a subset of variables, within ID groups.&amp;nbsp; The tools for this are the array statement and the lag function:&lt;/STRIKE&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;STRIKE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id day1 day2 day3 day4;
datalines;
1 -23 -22 -21 -20
1 -19 -18 -17 -16
1 -15 -14 -13 -12
1 -11 -10 -9 -8 
1 -7 -6 . . 
2 -20 -19 -18 -17 
2 -16 -15 -14 -13
2 -12 -11 -9 -8
2 -7 -6 -5 -4
2 -3 -2 -1 .  
run;

data want;
  set have;
  by id;
  array d day3-day4;
  do over d;
    d=ifn(first.id,.,lag(d));
  end;
run;
&lt;/CODE&gt;&lt;/STRIKE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;STRIKE&gt;If all the carry-forward variables are numeric, just include them in the array statement.&lt;/STRIKE&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 17 Sep 2020 19:47:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Fill-down-multiple-rows/m-p/684655#M207518</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-09-17T19:47:13Z</dc:date>
    </item>
    <item>
      <title>Re: Fill down multiple rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Fill-down-multiple-rows/m-p/684782#M207580</link>
      <description>&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;. Thank you for reviewing that earlier response and correcting it. I will give this solution a try as well. Thanks so much!</description>
      <pubDate>Thu, 17 Sep 2020 19:58:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Fill-down-multiple-rows/m-p/684782#M207580</guid>
      <dc:creator>eabc0351</dc:creator>
      <dc:date>2020-09-17T19:58:26Z</dc:date>
    </item>
  </channel>
</rss>

