<?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 fill in missing year and create dummy equal zero at the same time? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-fill-in-missing-year-and-create-dummy-equal-zero-at-the/m-p/820981#M324089</link>
    <description>Thank you so much for the reply promptly! It works!!</description>
    <pubDate>Wed, 29 Jun 2022 17:52:35 GMT</pubDate>
    <dc:creator>chimei0403</dc:creator>
    <dc:date>2022-06-29T17:52:35Z</dc:date>
    <item>
      <title>How to fill in missing year and create dummy equal zero at the same time?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-fill-in-missing-year-and-create-dummy-equal-zero-at-the/m-p/820942#M324074</link>
      <description>&lt;P&gt;I need some guidance.&lt;/P&gt;&lt;P&gt;I would like to fill in missing year&amp;nbsp;and create a dummy=0 by id.&lt;/P&gt;&lt;PRE&gt;DATA HAVE;
	INPUT  YEAR ID $ NUMBER;
    CARDS;
 2017 a   1
 2018 a   1
 2020 a   1
 2016 b   1
 2020 b   1
 2018 c   1
 2020 c   1
;
RUN;&lt;/PRE&gt;&lt;P&gt;I wish my final data look like below. Many thanks for the advice in advance!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;2017&amp;nbsp; &amp;nbsp; a&amp;nbsp; &amp;nbsp; 1&lt;BR /&gt;2018&amp;nbsp; &amp;nbsp; a&amp;nbsp; &amp;nbsp; 1&lt;BR /&gt;2019&amp;nbsp; &amp;nbsp; a&amp;nbsp; &amp;nbsp; 0&lt;BR /&gt;2020&amp;nbsp; &amp;nbsp; a&amp;nbsp; &amp;nbsp; 1&lt;BR /&gt;2016&amp;nbsp; &amp;nbsp; b&amp;nbsp; &amp;nbsp; 1&lt;BR /&gt;2017&amp;nbsp; &amp;nbsp; b&amp;nbsp; &amp;nbsp; 0&lt;BR /&gt;2018&amp;nbsp; &amp;nbsp; b&amp;nbsp; &amp;nbsp; 0&lt;BR /&gt;2019&amp;nbsp; &amp;nbsp; b&amp;nbsp; &amp;nbsp; 0&lt;BR /&gt;2020&amp;nbsp; &amp;nbsp; b&amp;nbsp; &amp;nbsp; 1&lt;BR /&gt;2018&amp;nbsp; &amp;nbsp; c&amp;nbsp; &amp;nbsp; 1&lt;BR /&gt;2019&amp;nbsp; &amp;nbsp; c&amp;nbsp; &amp;nbsp; 0&lt;BR /&gt;2020&amp;nbsp; &amp;nbsp; c&amp;nbsp; &amp;nbsp; 1&lt;/P&gt;</description>
      <pubDate>Wed, 29 Jun 2022 15:21:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-fill-in-missing-year-and-create-dummy-equal-zero-at-the/m-p/820942#M324074</guid>
      <dc:creator>chimei0403</dc:creator>
      <dc:date>2022-06-29T15:21:10Z</dc:date>
    </item>
    <item>
      <title>Re: How to fill in missing year and create dummy equal zero at the same time?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-fill-in-missing-year-and-create-dummy-equal-zero-at-the/m-p/820956#M324076</link>
      <description>&lt;P&gt;One way that works with the provided example:&lt;/P&gt;
&lt;PRE&gt;DATA HAVE;
	INPUT  YEAR ID $ NUMBER;
    CARDS;
 2017 a   1
 2018 a   1
 2020 a   1
 2016 b   1
 2020 b   1
 2018 c   1
 2020 c   1
;
RUN;

data want;
   set have;
   lid = lag(id);
   lyr = lag(year);
   if lid=id and ((year-lyr)&amp;gt;1) then do;
   /* store current year and number in temp variables*/
      tyr=year;
      tnum=number;
      do year=(lyr+1) to (tyr-1);
         number=0;
         output;
      end;
      /*restore from temp and write to output*/
      year=tyr;
      number=tnum;
      output;
   end;
   /* write to output when not a gap*/
   else output;
   keep year id number;
run;&lt;/PRE&gt;
&lt;P&gt;If you have not seen the LAG function it is a way to get the value of a variable from previous records (cautions apply).&lt;/P&gt;
&lt;P&gt;The loop goes through the "missing" year values.&lt;/P&gt;
&lt;P&gt;The "trick" is storing the values so you can set the desired values and control when the data is written to the output data which is done with the OUTPUT instruction.&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/424555"&gt;@chimei0403&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I need some guidance.&lt;/P&gt;
&lt;P&gt;I would like to fill in missing year&amp;nbsp;and create a dummy=0 by id.&lt;/P&gt;
&lt;PRE&gt;DATA HAVE;
	INPUT  YEAR ID $ NUMBER;
    CARDS;
 2017 a   1
 2018 a   1
 2020 a   1
 2016 b   1
 2020 b   1
 2018 c   1
 2020 c   1
;
RUN;&lt;/PRE&gt;
&lt;P&gt;I wish my final data look like below. Many thanks for the advice in advance!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2017&amp;nbsp; &amp;nbsp; a&amp;nbsp; &amp;nbsp; 1&lt;BR /&gt;2018&amp;nbsp; &amp;nbsp; a&amp;nbsp; &amp;nbsp; 1&lt;BR /&gt;2019&amp;nbsp; &amp;nbsp; a&amp;nbsp; &amp;nbsp; 0&lt;BR /&gt;2020&amp;nbsp; &amp;nbsp; a&amp;nbsp; &amp;nbsp; 1&lt;BR /&gt;2016&amp;nbsp; &amp;nbsp; b&amp;nbsp; &amp;nbsp; 1&lt;BR /&gt;2017&amp;nbsp; &amp;nbsp; b&amp;nbsp; &amp;nbsp; 0&lt;BR /&gt;2018&amp;nbsp; &amp;nbsp; b&amp;nbsp; &amp;nbsp; 0&lt;BR /&gt;2019&amp;nbsp; &amp;nbsp; b&amp;nbsp; &amp;nbsp; 0&lt;BR /&gt;2020&amp;nbsp; &amp;nbsp; b&amp;nbsp; &amp;nbsp; 1&lt;BR /&gt;2018&amp;nbsp; &amp;nbsp; c&amp;nbsp; &amp;nbsp; 1&lt;BR /&gt;2019&amp;nbsp; &amp;nbsp; c&amp;nbsp; &amp;nbsp; 0&lt;BR /&gt;2020&amp;nbsp; &amp;nbsp; c&amp;nbsp; &amp;nbsp; 1&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 29 Jun 2022 15:45:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-fill-in-missing-year-and-create-dummy-equal-zero-at-the/m-p/820956#M324076</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-06-29T15:45:30Z</dc:date>
    </item>
    <item>
      <title>Re: How to fill in missing year and create dummy equal zero at the same time?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-fill-in-missing-year-and-create-dummy-equal-zero-at-the/m-p/820966#M324079</link>
      <description>&lt;P&gt;Ordinarily I'd recommend a self-merge with lookahead.&amp;nbsp; But this pair of SET statements (one of which does a lookahead) works just as well:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;dATA HAVE;
	INPUT  YEAR ID $ NUMBER;
    CARDS;
 2017 a   1
 2018 a   1
 2020 a   1
 2016 b   1
 2020 b   1
 2018 c   1
 2020 c   1
RUN;


data want (drop=nxt_:);
  set have;
  by id;
  if end_of_nxt=0 then set have (firstobs=2 keep=year rename=(year=nxt_year)) end=end_of_nxt;
  output;
  if last.id=0;   /* Added after oversight noticed*/
  number=0;
  do while (year&amp;lt;nxt_year-1);
    year=year+1;
    output;
  end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 29 Jun 2022 17:00:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-fill-in-missing-year-and-create-dummy-equal-zero-at-the/m-p/820966#M324079</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2022-06-29T17:00:29Z</dc:date>
    </item>
    <item>
      <title>Re: How to fill in missing year and create dummy equal zero at the same time?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-fill-in-missing-year-and-create-dummy-equal-zero-at-the/m-p/820981#M324089</link>
      <description>Thank you so much for the reply promptly! It works!!</description>
      <pubDate>Wed, 29 Jun 2022 17:52:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-fill-in-missing-year-and-create-dummy-equal-zero-at-the/m-p/820981#M324089</guid>
      <dc:creator>chimei0403</dc:creator>
      <dc:date>2022-06-29T17:52:35Z</dc:date>
    </item>
    <item>
      <title>Re: How to fill in missing year and create dummy equal zero at the same time?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-fill-in-missing-year-and-create-dummy-equal-zero-at-the/m-p/820983#M324090</link>
      <description>Thank you so much! This approach also works! You guy are amazing!! Much appreciate it!!</description>
      <pubDate>Wed, 29 Jun 2022 17:53:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-fill-in-missing-year-and-create-dummy-equal-zero-at-the/m-p/820983#M324090</guid>
      <dc:creator>chimei0403</dc:creator>
      <dc:date>2022-06-29T17:53:39Z</dc:date>
    </item>
    <item>
      <title>Re: How to fill in missing year and create dummy equal zero at the same time?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-fill-in-missing-year-and-create-dummy-equal-zero-at-the/m-p/821114#M324164</link>
      <description>&lt;P&gt;If you have SAS/ETS module.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;DATA HAVE;
 INPUT  YEAR ID $ NUMBER;
    CARDS;
 2017 a   1
 2018 a   1
 2020 a   1
 2016 b   1
 2020 b   1
 2018 c   1
 2020 c   1
;
RUN;

proc timeseries data=have out=want ;
by id;
id year interval=day format=best.;
var NUMBER / setmissing=0;
run;

proc print data=want;run;&lt;/PRE&gt;</description>
      <pubDate>Thu, 30 Jun 2022 12:24:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-fill-in-missing-year-and-create-dummy-equal-zero-at-the/m-p/821114#M324164</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2022-06-30T12:24:50Z</dc:date>
    </item>
    <item>
      <title>Re: How to fill in missing year and create dummy equal zero at the same time?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-fill-in-missing-year-and-create-dummy-equal-zero-at-the/m-p/821663#M324401</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Sorry to bother you all again. I think I miss one critical criteria. I try and modify codes but can't figure it out.&amp;nbsp;&amp;nbsp;If the id only had&amp;nbsp; 1 number record in a given year, the rest of the year of number variable would equal zero. Something like below. Wish to hear the advice. Many thanks!&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;DATA HAVE;
	INPUT  YEAR ID $ NUMBER;
    CARDS;
 2017 a   1
 2018 a   1
 2020 a   1
 2016 b   1
 2020 b   1
 2018 c   1
 2020 c   1&lt;BR /&gt; 2016 d   1&lt;BR /&gt; 2018 e   1
;
RUN;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Wish to fix the want dataset as below:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;2017&amp;nbsp; &amp;nbsp; a&amp;nbsp; &amp;nbsp; 1&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;2018&amp;nbsp; &amp;nbsp; a&amp;nbsp; &amp;nbsp; 1&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;2019&amp;nbsp; &amp;nbsp; a&amp;nbsp; &amp;nbsp; 0&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;2020&amp;nbsp; &amp;nbsp; a&amp;nbsp; &amp;nbsp; 1&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;2016&amp;nbsp; &amp;nbsp; b&amp;nbsp; &amp;nbsp; 1&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;2017&amp;nbsp; &amp;nbsp; b&amp;nbsp; &amp;nbsp; 0&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;2018&amp;nbsp; &amp;nbsp; b&amp;nbsp; &amp;nbsp; 0&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;2019&amp;nbsp; &amp;nbsp; b&amp;nbsp; &amp;nbsp; 0&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;2020&amp;nbsp; &amp;nbsp; b&amp;nbsp; &amp;nbsp; 1&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;2018&amp;nbsp; &amp;nbsp; c&amp;nbsp; &amp;nbsp; 1&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;2019&amp;nbsp; &amp;nbsp; c&amp;nbsp; &amp;nbsp; 0&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;2020&amp;nbsp; &amp;nbsp; c&amp;nbsp; &amp;nbsp; 1&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;2016&amp;nbsp; &amp;nbsp;d&amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;2017&amp;nbsp; &amp;nbsp;d&amp;nbsp; &amp;nbsp; &amp;nbsp;0&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;2018&amp;nbsp; &amp;nbsp;d&amp;nbsp; &amp;nbsp; &amp;nbsp;0&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;2019&amp;nbsp; &amp;nbsp;d&amp;nbsp; &amp;nbsp; &amp;nbsp;0&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;2020&amp;nbsp; &amp;nbsp;d&amp;nbsp; &amp;nbsp; &amp;nbsp;0&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;2018&amp;nbsp; &amp;nbsp;e&amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;2019&amp;nbsp; &amp;nbsp;e&amp;nbsp; &amp;nbsp; &amp;nbsp;0&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;2020&amp;nbsp; &amp;nbsp;e&amp;nbsp; &amp;nbsp; &amp;nbsp;0&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 05 Jul 2022 19:06:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-fill-in-missing-year-and-create-dummy-equal-zero-at-the/m-p/821663#M324401</guid>
      <dc:creator>chimei0403</dc:creator>
      <dc:date>2022-07-05T19:06:33Z</dc:date>
    </item>
    <item>
      <title>Re: How to fill in missing year and create dummy equal zero at the same time?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-fill-in-missing-year-and-create-dummy-equal-zero-at-the/m-p/821767#M324446</link>
      <description>&lt;P&gt;So in addition to filling interior holes with the intervening years and number=0, you also want to similarly fill "trailing" holes through year=2020.&amp;nbsp; Now I will revert to recommending the &lt;EM&gt;&lt;STRONG&gt;self-merge with lookahead&lt;/STRONG&gt;&lt;/EM&gt; technique:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA HAVE;
	INPUT  YEAR ID $ NUMBER;
    CARDS;
 2017 a   1
 2018 a   1
 2020 a   1
 2016 b   1
 2020 b   1
 2018 c   1
 2020 c   1
 2016 d   1
 2018 e   1
RUN;
data want (drop=nxt_:);
  merge have
        have (firstobs=2 keep=year id rename=(year=nxt_yr id=nxt_id));
  if nxt_id^=id then nxt_yr=2021;
  do year=year to nxt_yr-1;
    output;
    number=0;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 06 Jul 2022 03:08:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-fill-in-missing-year-and-create-dummy-equal-zero-at-the/m-p/821767#M324446</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2022-07-06T03:08:05Z</dc:date>
    </item>
    <item>
      <title>Re: How to fill in missing year and create dummy equal zero at the same time?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-fill-in-missing-year-and-create-dummy-equal-zero-at-the/m-p/821811#M324468</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA HAVE;
 INPUT  YEAR ID $ NUMBER;
    CARDS;
 2017 a   1
 2018 a   1
 2020 a   1
 2016 b   1
 2020 b   1
 2018 c   1
 2020 c   1
 2016 d   1
 2018 e   1
;
RUN;

proc sql noprint;
create table temp as
select id,min(year) as min
 from have
  group by id;

select max(year) into :max from have;
quit;

data temp2;
 set temp;
 do year=min to &amp;amp;max.;
  output;
 end;
 keep id year;
run;

proc sql;
create table want as
select a.*,coalesce(b.number,0) as number 
 from temp2 as a left join have as b
  on a.id=b.id and a.year=b.year;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 06 Jul 2022 12:17:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-fill-in-missing-year-and-create-dummy-equal-zero-at-the/m-p/821811#M324468</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2022-07-06T12:17:28Z</dc:date>
    </item>
  </channel>
</rss>

