<?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: How to populate variables using subsequent observations in SAS Data Management</title>
    <link>https://communities.sas.com/t5/SAS-Data-Management/How-to-populate-variables-using-subsequent-observations/m-p/556979#M17055</link>
    <description>&lt;P&gt;You essentially are asking how to do a reverse Last Observation Carried Forward (LOCF) operation.&amp;nbsp; The simplest way to do LOCF is to take advantage of the UPDATE statement in a DATA step.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data locf ;
  update have(obs=0) have ;
  by id;
   output;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Do do the reverse just order the observations backwards.&amp;nbsp; Your sample data does have a sort key so perhaps you can just make one.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data reverse;
  set have;
  row+1;
run;
proc sort data=reverse ;
  by id descending row;
run;
data locb ;
  update reverse(obs=0) reverse ;
  by id;
  output;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But in addition to the reverse aspect you also added that you want to treat zeros as missing.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why do you want to treat zero as missing?&lt;/P&gt;
&lt;P&gt;If you do want to treat zero as missing then you could set them to missing in the step that is adding the ROW number variable.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data reverse;
  set have;
  row+1;
  array varlist var1-var2;
  do over varlist;
     if varlist=0 then varlist=.;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 08 May 2019 00:08:38 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2019-05-08T00:08:38Z</dc:date>
    <item>
      <title>How to populate variables using subsequent observations</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-to-populate-variables-using-subsequent-observations/m-p/556751#M17048</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a dataset with multiple observations for some participant IDs. I am wondering if there is a way to populate the first observation (by ID) with data in a subsequent observation for multiple variables, without using a different data step for each variable? For example:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID &amp;nbsp; &amp;nbsp; Var1 &amp;nbsp; &amp;nbsp; Var2&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; 8 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 9&lt;/P&gt;&lt;P&gt;2 &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; . &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; 4&lt;/P&gt;&lt;P&gt;2 &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; 3 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&lt;/P&gt;&lt;P&gt;3 &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; 5 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 7&lt;/P&gt;&lt;P&gt;4 &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; 0 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&lt;/P&gt;&lt;P&gt;4 &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; 6 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 10&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For the first observation of ID #2, I would want to populate Var1 with 3; for the first ID #4, I want to populate Var1 with 6 and Var2 with 10. I have tried retain (for the first observation) and creating an output dataset with only the populated observations to merge back with the original, but I am trying to find a simpler way that works across multiple variables. Thank you.&lt;/P&gt;</description>
      <pubDate>Tue, 07 May 2019 12:25:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-to-populate-variables-using-subsequent-observations/m-p/556751#M17048</guid>
      <dc:creator>kate90</dc:creator>
      <dc:date>2019-05-07T12:25:41Z</dc:date>
    </item>
    <item>
      <title>Re: How to populate variables using subsequent observations</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-to-populate-variables-using-subsequent-observations/m-p/556755#M17049</link>
      <description>&lt;P&gt;using retain as below&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID Var1 Var2;
cards;
1 8 9
2 . 4
2 3 2
3 5 7
4 0 .
4 6 10
;

proc sort data=have;
by id descending var1 descending var2;
run;

data want;
set have;
retain newvar1_ newvar2_;
by id;
if first.id then do; newvar1_=.;newvar2_=.;end;
if var1 ne . then newvar1_=var1;
if var2 ne . then newvar2_=var2;
drop var1 var2;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 07 May 2019 12:38:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-to-populate-variables-using-subsequent-observations/m-p/556755#M17049</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2019-05-07T12:38:34Z</dc:date>
    </item>
    <item>
      <title>Re: How to populate variables using subsequent observations</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-to-populate-variables-using-subsequent-observations/m-p/556758#M17050</link>
      <description>Suppose that an ID has a missing value for VAR1 on its fifth observation.  Do you want to replace that with a non missing value from its sixth observation?</description>
      <pubDate>Tue, 07 May 2019 12:56:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-to-populate-variables-using-subsequent-observations/m-p/556758#M17050</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2019-05-07T12:56:26Z</dc:date>
    </item>
    <item>
      <title>Re: How to populate variables using subsequent observations</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-to-populate-variables-using-subsequent-observations/m-p/556765#M17051</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/229594"&gt;@kate90&lt;/a&gt;&amp;nbsp; &amp;nbsp;I have the same doubt that&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954"&gt;@Astounding&lt;/a&gt;&amp;nbsp; asks as your sample isn't showing that. Nevertheless, i am going with the assumption i.e trusting your sample&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID Var1 Var2;
cards;
1 8 9
2 . 4
2 3 2
3 5 7
4 0 .
4 6 10
;


data want ;
if _n_=1 then do;
   dcl hash H () ;
   h.definekey  ("id","name") ;
   h.definedata ("value") ;
   h.definedone () ;
end;
do until(last.id);
set have end=lr;
by id;
array t(*) var1-var2;
do i=1 to dim(t);
if t(i)&amp;gt;. then do;
name=vname(t(i));
value=t(i);
rc=h.add();
end;
end;
end;
do until(last.id);
set have end=lr;
by id;
do i=1 to dim(t);
if t(i)=. then do;name=vname(t(i)); rc=h.find();t(i)=value;end;
end;
output;
end;
h.clear();
drop value name i rc;
run;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 07 May 2019 13:23:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-to-populate-variables-using-subsequent-observations/m-p/556765#M17051</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-05-07T13:23:53Z</dc:date>
    </item>
    <item>
      <title>Re: How to populate variables using subsequent observations</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-to-populate-variables-using-subsequent-observations/m-p/556766#M17052</link>
      <description>&lt;P&gt;I mainly want to populate the first observation with any subsequent non-missing values, so that all variables have data contained within one observation.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 07 May 2019 13:15:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-to-populate-variables-using-subsequent-observations/m-p/556766#M17052</guid>
      <dc:creator>kate90</dc:creator>
      <dc:date>2019-05-07T13:15:58Z</dc:date>
    </item>
    <item>
      <title>Re: How to populate variables using subsequent observations</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-to-populate-variables-using-subsequent-observations/m-p/556966#M17053</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your posts have finally convinced me that I need to learn hashing!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have one question about your proposed solution, using h.add.&amp;nbsp; What happens when there are 5 observations for an ID?&amp;nbsp; Does the add method ignore additional values once the data has been populated for that key?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 07 May 2019 22:37:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-to-populate-variables-using-subsequent-observations/m-p/556966#M17053</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2019-05-07T22:37:30Z</dc:date>
    </item>
    <item>
      <title>Re: How to populate variables using subsequent observations</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-to-populate-variables-using-subsequent-observations/m-p/556969#M17054</link>
      <description>&lt;P&gt;"I have one question about your proposed solution, using h.add.&amp;nbsp; &lt;STRONG&gt;What happens when there are 5 observations for an ID?&amp;nbsp; Does the add method &lt;U&gt;ignore additional values&lt;/U&gt; once the data has been populated for that key?"&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Yes Sir &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954"&gt;@Astounding&lt;/a&gt;&amp;nbsp;, &lt;STRONG&gt;Your understanding is spot on&lt;/STRONG&gt;. The reason being, we have not explicitly defined multidata;'y' during hash object instantiation to hold more than data item for a given key. Therefore the return code would be a non zero value. However, the catch is , object dot notation method must be assigned to a return code otherwise the implicit failure of the dot notation method will flood the log with errors stating duplicate entry. So in this case rc is a requirement. Thank you&lt;/P&gt;</description>
      <pubDate>Tue, 07 May 2019 22:45:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-to-populate-variables-using-subsequent-observations/m-p/556969#M17054</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-05-07T22:45:25Z</dc:date>
    </item>
    <item>
      <title>Re: How to populate variables using subsequent observations</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-to-populate-variables-using-subsequent-observations/m-p/556979#M17055</link>
      <description>&lt;P&gt;You essentially are asking how to do a reverse Last Observation Carried Forward (LOCF) operation.&amp;nbsp; The simplest way to do LOCF is to take advantage of the UPDATE statement in a DATA step.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data locf ;
  update have(obs=0) have ;
  by id;
   output;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Do do the reverse just order the observations backwards.&amp;nbsp; Your sample data does have a sort key so perhaps you can just make one.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data reverse;
  set have;
  row+1;
run;
proc sort data=reverse ;
  by id descending row;
run;
data locb ;
  update reverse(obs=0) reverse ;
  by id;
  output;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But in addition to the reverse aspect you also added that you want to treat zeros as missing.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why do you want to treat zero as missing?&lt;/P&gt;
&lt;P&gt;If you do want to treat zero as missing then you could set them to missing in the step that is adding the ROW number variable.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data reverse;
  set have;
  row+1;
  array varlist var1-var2;
  do over varlist;
     if varlist=0 then varlist=.;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 08 May 2019 00:08:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-to-populate-variables-using-subsequent-observations/m-p/556979#M17055</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-05-08T00:08:38Z</dc:date>
    </item>
  </channel>
</rss>

