<?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: Replacing missing values with closest non-missing observation within the same group in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Replacing-missing-values-with-closest-non-missing-observation/m-p/366073#M87032</link>
    <description>&lt;P&gt;You can try change sort types:&lt;/P&gt;
&lt;P&gt;- 1st to descending&lt;/P&gt;
&lt;P&gt;- 2nd drop the ascending to be ascening&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I hope that may help. I haven't tried it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Otherwise: maybe next code should help&amp;nbsp;, to be entered after ascending sort&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
if last.id and industry=. then industry=9999;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 12 Jun 2017 06:56:39 GMT</pubDate>
    <dc:creator>Shmuel</dc:creator>
    <dc:date>2017-06-12T06:56:39Z</dc:date>
    <item>
      <title>Replacing missing values with closest non-missing observation within the same group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replacing-missing-values-with-closest-non-missing-observation/m-p/366053#M87019</link>
      <description>&lt;P&gt;I am trying to replace the missing industry codes for a given company with its most closest industry code (by date). I was able to write a code that replaces the missing value if there all industry codes for the given company are identical. However, in my data, occasionally the industry code changes, but I am not sure how I can replace the nearest industry code with the ones that are missing. With the following data, my code works well:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA work.sample;
	INPUT ID Year Industry;
	DATALINES; 
	004605 1972 . 
	004605 1973 .
	004605 1974 6798
	004610 1984 6799
	004610 1985 .
;
RUN;


data work.non_missing;
	set work.sample;
	if industry^=.;
	industry_correct=industry;
run;

proc sql;
	create table work.adding_industry as 
	select * 
	from work.sample as a 
	left join work.non_missing as b 
	on a.id=b.id;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;BR /&gt;However, with the following data (with non-identical industry codes for a given company) my code fails:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA work.sample;
	INPUT ID Year Industry;
	DATALINES; 
	004605 1972 . 
	004605 1973 .
	004605 1974 6798
	004605 1979 7788
	004610 1984 6799
	004610 1985 .
;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Here is what the correct code will generate:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA work.want;
	INPUT ID Year Industry;
	DATALINES; 
	004605 1972 6798 
	004605 1973 6798
	004605 1974 6798
	004605 1979 7788
	004610 1984 6799
	004610 1985 6799
;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;BR /&gt;I thought of the following, but I am wondering if there is a more robust way of doing this?&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
	create table work.adding_industry as 
	select * 
	from work.sample as a 
	left join work.non_missing as b 
	on a.id=b.id and a.year=&amp;lt;b.year;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 12 Jun 2017 04:40:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replacing-missing-values-with-closest-non-missing-observation/m-p/366053#M87019</guid>
      <dc:creator>Yegen</dc:creator>
      <dc:date>2017-06-12T04:40:31Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing missing values with closest non-missing observation within the same group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replacing-missing-values-with-closest-non-missing-observation/m-p/366055#M87021</link>
      <description>&lt;P&gt;Try next code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have; by id year; run;

data temp;
 set have;
      by id ;
          retain ic;   /* industry code */
          if first.id then ic=industry;
          if industry=. then industry=ic;
          else ic=industry; drop ic;
run;

proc sort data=temp; by id descending year; run;

data want;
 set temp;
      by id ;
          retain ic;   /* industry code */
          if first.id then ic=industry;
          if industry=. then industry=ic;
          else ic=industry; drop ic;
run;
             &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 12 Jun 2017 05:05:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replacing-missing-values-with-closest-non-missing-observation/m-p/366055#M87021</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2017-06-12T05:05:12Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing missing values with closest non-missing observation within the same group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replacing-missing-values-with-closest-non-missing-observation/m-p/366056#M87022</link>
      <description>DATA work.sample;
	INPUT ID Year Industry;
	DATALINES; 
	004605 1972 . 
	004605 1973 .
	004605 1974 6798
	004610 1984 6799
	004610 1985 .
;
RUN;

PROC SORT DATA=SAMPLE ;BY ID Industry ;RUN;

DATA SAMPLE_1;
SET SAMPLE;
BY ID ;
IF LAST.ID THEN OUTPUT;
RUN;

PROC SQL;
CREATE TABLE WANT AS 
SELECT A.*,B.Industry AS Industry_NEW 
FROM SAMPLE AS A
LEFT JOIN SAMPLE_1 AS B
ON A.ID = B.ID;
QUIT;</description>
      <pubDate>Mon, 12 Jun 2017 05:11:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replacing-missing-values-with-closest-non-missing-observation/m-p/366056#M87022</guid>
      <dc:creator>TarunKumar</dc:creator>
      <dc:date>2017-06-12T05:11:36Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing missing values with closest non-missing observation within the same group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replacing-missing-values-with-closest-non-missing-observation/m-p/366063#M87027</link>
      <description>&lt;P&gt;Thanks,&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/88384"&gt;@Shmuel&lt;/a&gt;. The code works very well. The retain command is very powerful, I have to make use of it more often!&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;The&amp;nbsp;following part (let's call it part 1) of your&amp;nbsp;code replaces&amp;nbsp;missing observations with non-missing historical values:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data temp;
 set have;
      by id ;
          retain ic;   /* industry code */
          if first.id then ic=industry;
          if industry=. then industry=ic;
          else ic=industry; drop ic;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The last part of your code (let's call it part 2) replaces the&amp;nbsp;the missing observations&amp;nbsp;with&amp;nbsp;non-missing future values:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=temp; by id descending year; run;

data want;
 set temp;
      by id ;
          retain ic;   /* industry code */
          if first.id then ic=industry;
          if industry=. then industry=ic;
          else ic=industry; drop ic;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;BR /&gt;Suppose I have the following data:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA work.sample;
	INPUT ID Year Industry;
	DATALINES; 
	004610 1983 .
	004610 1984 6799
	004610 1985 9999
	004610 1986 .
	004610 1987 .
	004610 1988 8888
	004610 1989 .
	004610 1990 9999
;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Whenever there is a future non-missing industry code and&amp;nbsp;the code replaces&amp;nbsp;missing industry code with that particular future non-missing idustry code, then we have the following output:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA work.want1;
	INPUT ID Year Industry;
	DATALINES; 
	004610 1983 6799
	004610 1984 6799
	004610 1985 9999
	004610 1986 8888
	004610 1987 8888
	004610 1988 8888
	004610 1989 9999
	004610 1990 9999
;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;BR /&gt;However, with your code the year=1989 is replaced with 8888 rather than 9999. Would you suggest just to remove the order of the code? &amp;nbsp;I.e., replace part 1 with part 2?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Update:&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/88384"&gt;@Shmuel&lt;/a&gt;, I just tried the reverse order (part 2, then part 1), but the output seems incorrect.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 12 Jun 2017 05:38:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replacing-missing-values-with-closest-non-missing-observation/m-p/366063#M87027</guid>
      <dc:creator>Yegen</dc:creator>
      <dc:date>2017-06-12T05:38:08Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing missing values with closest non-missing observation within the same group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replacing-missing-values-with-closest-non-missing-observation/m-p/366064#M87028</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks, &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/49775"&gt;@TarunKumar&lt;/a&gt;.&amp;nbsp;I have tried your code with the following dataset, but the output seems incorrect:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA work.sample;
	INPUT ID Year Industry;
	DATALINES; 
	004610 1983 .
	004610 1984 6799
	004610 1985 9999
	004610 1986 .
	004610 1987 .
	004610 1988 8888
	004610 1989 .
	004610 1990 9999
;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 12 Jun 2017 05:36:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replacing-missing-values-with-closest-non-missing-observation/m-p/366064#M87028</guid>
      <dc:creator>Yegen</dc:creator>
      <dc:date>2017-06-12T05:36:39Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing missing values with closest non-missing observation within the same group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replacing-missing-values-with-closest-non-missing-observation/m-p/366073#M87032</link>
      <description>&lt;P&gt;You can try change sort types:&lt;/P&gt;
&lt;P&gt;- 1st to descending&lt;/P&gt;
&lt;P&gt;- 2nd drop the ascending to be ascening&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I hope that may help. I haven't tried it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Otherwise: maybe next code should help&amp;nbsp;, to be entered after ascending sort&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
if last.id and industry=. then industry=9999;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 12 Jun 2017 06:56:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replacing-missing-values-with-closest-non-missing-observation/m-p/366073#M87032</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2017-06-12T06:56:39Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing missing values with closest non-missing observation within the same group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replacing-missing-values-with-closest-non-missing-observation/m-p/366848#M87293</link>
      <description>&lt;P&gt;This is very helpful,&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/88384"&gt;@Shmuel&lt;/a&gt;. I will give it a try. Thanks again for all of your valuable&amp;nbsp;&amp;nbsp;suggestions.&lt;/P&gt;</description>
      <pubDate>Wed, 14 Jun 2017 06:01:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replacing-missing-values-with-closest-non-missing-observation/m-p/366848#M87293</guid>
      <dc:creator>Yegen</dc:creator>
      <dc:date>2017-06-14T06:01:36Z</dc:date>
    </item>
  </channel>
</rss>

