<?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: conditionally replace missing values in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/conditionally-replace-missing-values/m-p/299703#M270325</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp﻿&lt;/a&gt;, I think the better way to read a dataset backwards is with the point= option, as in:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data ssalc / view=ssalc;
do i = nobs to 1 by -1;
    set sashelp.class nobs=nobs point=i;
    output;
    end;
stop;
run;

proc print data=ssalc; run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;It doesn't require the creation of an order variable (your n) and should be faster than a sort.&lt;/P&gt;</description>
    <pubDate>Tue, 20 Sep 2016 21:47:34 GMT</pubDate>
    <dc:creator>PGStats</dc:creator>
    <dc:date>2016-09-20T21:47:34Z</dc:date>
    <item>
      <title>conditionally replace missing values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/conditionally-replace-missing-values/m-p/299408#M270321</link>
      <description>&lt;P&gt;&lt;STRONG&gt;data&lt;/STRONG&gt; have;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; input id var1 var2;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; cards;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; 1 3 .&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; 1 5 .&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; 1 . 4&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; 1 . 7&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; 2 4 2&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; 2 8 .&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; 2 . 8&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; 2 9 .&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; 2 . .&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; 3 2 3&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; 3 7 .&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; 3 . 9&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; 3 . 6&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; 4 . 2&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; 5 . 1&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; 5 9 2&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; 6 . 5&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; 6 . 3&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;;&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;If the missing value is not at the beginning of each ID, then the missing values equal to lag non-missing value;&lt;/LI&gt;&lt;LI&gt;If the missing value is at the beginning of each ID, and no non-missing values after the missing value, then the missing values equal to 0. E.g., for ID=4 and ID=6;&lt;/LI&gt;&lt;LI&gt;If the missing value is at the beginning of each ID, and there exist non-missing values after the missing value, then the missing values equals to forward non-missing value; e.g., for ID=5, the missing value for var1 should be replaced by 9, rather than 7 (which is for ID=3) or 0 (which is for ID=4).&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I was thinking to generate two new variables, e.g., variable “count” to count by ID, and variable “total” represents total observations for each ID, use those two new variables as conditions for different missing value replacement. However, look-ahead replacement is out of my reach.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I also find some answers online, but not exactly what I want, like the following one is replacing all missing values with lag values.&lt;/P&gt;&lt;P&gt;&lt;A href="https://communities.sas.com/t5/SAS-Procedures/Replacing-missing-values-by-previous-observation/td-p/26707" target="_blank"&gt;https://communities.sas.com/t5/SAS-Procedures/Replacing-missing-values-by-previous-observation/td-p/26707&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;data&lt;/STRONG&gt; want;&lt;/P&gt;&lt;P&gt;set have;&lt;/P&gt;&lt;P&gt;n=_n_;&lt;/P&gt;&lt;P&gt;if missing(var1) then do;&lt;/P&gt;&lt;P&gt;do until (not missing(var1));&lt;/P&gt;&lt;P&gt;n=n-&lt;STRONG&gt;1&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;set have(keep= var1) point=n; *second SET statement;&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Desired output:&lt;/P&gt;&lt;P&gt;&lt;IMG title="pic.png" alt="pic.png" src="https://communities.sas.com/t5/image/serverpage/image-id/4955i302213E3B3281040/image-size/original?v=v2&amp;amp;px=-1" border="0" /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Mon, 19 Sep 2016 22:33:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/conditionally-replace-missing-values/m-p/299408#M270321</guid>
      <dc:creator>Jonate_H</dc:creator>
      <dc:date>2016-09-19T22:33:27Z</dc:date>
    </item>
    <item>
      <title>Re: conditionally replace missing values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/conditionally-replace-missing-values/m-p/299427#M270322</link>
      <description>&lt;P&gt;Double DO UNTIL() to the rescue:&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 3 .
1 5 .
1 . 4
1 . 7
2 4 2
2 8 .
2 . 8
2 9 .
2 . .
3 2 3
3 7 .
3 . 9
3 . 6
4 . 2
5 . 1
5 9 2
6 . 5
6 . 3
;

data want;
array firstExist{2};
array lastExist{2};
array var{2};
do until(last.id);
    set have; by id;
    do i = 1 to dim(var);
        if missing(firstExist{i}) then firstExist{i} = var{i};
        end;
    end;
do until(last.id);
    set have; by id;
    do i = 1 to dim(var);
        if missing(var{i}) then var{i} = coalesce(lastExist{i}, firstExist{i}, 0);
        else lastExist{i} = var{i};
        end;
    output;
    end;
keep id var:;
run;

proc print data=want noobs; var id var1 var2; run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 20 Sep 2016 03:59:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/conditionally-replace-missing-values/m-p/299427#M270322</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2016-09-20T03:59:37Z</dc:date>
    </item>
    <item>
      <title>Re: conditionally replace missing values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/conditionally-replace-missing-values/m-p/299430#M270323</link>
      <description>&lt;PRE&gt;

data have;
   input id var1 var2;
   n+1;
   cards;
   1 3 .
   1 5 .
   1 . 4
   1 . 7
   2 4 2
   2 8 .
   2 . 8
   2 9 .
   2 . .
   3 2 3
   3 7 .
   3 . 9
   3 . 6
   4 . 2
   5 . 1
   5 9 2
   6 . 5
   6 . 3
;
run;
data temp;
 set have;
 by id;
 retain v1 v2;
 if first.id then do;v1=.;v2=.;end;
 if not missing(var1) then v1=var1;
 if not missing(var2) then v2=var2;
 drop var1 var2;
run;

proc sort data=temp;by descending n;run;
data temp;
 set temp;
 by id notsorted;
 retain var1 var2;
 if first.id then do;var1=.;var2=.;end;
 if not missing(v1) then var1=v1;
 if not missing(v2) then var2=v2;
 drop v1 v2;
run;
proc sort data=temp;by n;run;
proc stdize data=temp out=want(drop=n) reponly missing=0;
 var var1 var2;
run;


&lt;/PRE&gt;</description>
      <pubDate>Tue, 20 Sep 2016 03:00:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/conditionally-replace-missing-values/m-p/299430#M270323</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-09-20T03:00:03Z</dc:date>
    </item>
    <item>
      <title>Re: conditionally replace missing values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/conditionally-replace-missing-values/m-p/299687#M270324</link>
      <description>&lt;P&gt;Thanks a lot PG and Ksharp! Both work great! I would like to choose both as solutions; but I can only choose one, so I choose the first response.&lt;/P&gt;</description>
      <pubDate>Tue, 20 Sep 2016 20:48:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/conditionally-replace-missing-values/m-p/299687#M270324</guid>
      <dc:creator>Jonate_H</dc:creator>
      <dc:date>2016-09-20T20:48:35Z</dc:date>
    </item>
    <item>
      <title>Re: conditionally replace missing values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/conditionally-replace-missing-values/m-p/299703#M270325</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp﻿&lt;/a&gt;, I think the better way to read a dataset backwards is with the point= option, as in:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data ssalc / view=ssalc;
do i = nobs to 1 by -1;
    set sashelp.class nobs=nobs point=i;
    output;
    end;
stop;
run;

proc print data=ssalc; run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;It doesn't require the creation of an order variable (your n) and should be faster than a sort.&lt;/P&gt;</description>
      <pubDate>Tue, 20 Sep 2016 21:47:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/conditionally-replace-missing-values/m-p/299703#M270325</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2016-09-20T21:47:34Z</dc:date>
    </item>
    <item>
      <title>Re: conditionally replace missing values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/conditionally-replace-missing-values/m-p/299740#M270326</link>
      <description>PG,
I don't think so. Especially for big table, I wouldn't expect POINT= is a good choice. POINT= has its advantage only when you POINT small obs . When POINT millions obs ,that would be very slowly .</description>
      <pubDate>Wed, 21 Sep 2016 04:48:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/conditionally-replace-missing-values/m-p/299740#M270326</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-09-21T04:48:11Z</dc:date>
    </item>
  </channel>
</rss>

