<?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: data - filling gaps in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/data-filling-gaps/m-p/834325#M35860</link>
    <description>&lt;P&gt;Try this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id city $ week;
datalines;
1 A 3
1 B 6
1 B 7
1 C 10
2 A 3
2 B 6
2 B 9
2 C 10
2 C 15
;
run;


data have2;
set have;
by id;
if not first.id;
rename week=week2;
drop city;
run;

data want;
merge have have2; 
by id;
if last.id then 
  output;
else 
  do week = week to week2-1;
    output;
  end;
drop week2;
run;

proc print data = want; 
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
    <pubDate>Tue, 20 Sep 2022 16:56:38 GMT</pubDate>
    <dc:creator>yabwon</dc:creator>
    <dc:date>2022-09-20T16:56:38Z</dc:date>
    <item>
      <title>data - filling gaps</title>
      <link>https://communities.sas.com/t5/New-SAS-User/data-filling-gaps/m-p/834319#M35859</link>
      <description>&lt;P&gt;Hello SAS community,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have quick question related to data structure or formatting. I have the current data that looks like:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data have;
input id city $ week;
datalines;
1 A 3
1 B 6
1 B 7
1 C 10
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But I would actually need to fill the weeks after the first week the person shows up in the city (i.e.):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data want;
input id city $ week;
datalines;
1 A 3
1 A 4
1 A 5
1 B 6
1 B 7
1 B 8
1 B 9
1 C 10
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The week gaps would be filled with the former city value until a city change. There are about 1000 subjects in the data.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Might someone be willing to provide assistance how to code it?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 20 Sep 2022 16:30:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/data-filling-gaps/m-p/834319#M35859</guid>
      <dc:creator>hellorc</dc:creator>
      <dc:date>2022-09-20T16:30:17Z</dc:date>
    </item>
    <item>
      <title>Re: data - filling gaps</title>
      <link>https://communities.sas.com/t5/New-SAS-User/data-filling-gaps/m-p/834325#M35860</link>
      <description>&lt;P&gt;Try this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id city $ week;
datalines;
1 A 3
1 B 6
1 B 7
1 C 10
2 A 3
2 B 6
2 B 9
2 C 10
2 C 15
;
run;


data have2;
set have;
by id;
if not first.id;
rename week=week2;
drop city;
run;

data want;
merge have have2; 
by id;
if last.id then 
  output;
else 
  do week = week to week2-1;
    output;
  end;
drop week2;
run;

proc print data = want; 
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Tue, 20 Sep 2022 16:56:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/data-filling-gaps/m-p/834325#M35860</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2022-09-20T16:56:38Z</dc:date>
    </item>
    <item>
      <title>Re: data - filling gaps</title>
      <link>https://communities.sas.com/t5/New-SAS-User/data-filling-gaps/m-p/834339#M35862</link>
      <description>Hello yabwon, thank you so much for your reply. I have an extra question if you don't mind. Let's say the final week is 20 for everyone. But the last.id for subject 1 is week=10. and for subject 2 is week=15 as in your code. How would you adjust the code so that there would be:&lt;BR /&gt;&lt;BR /&gt;1 C 10&lt;BR /&gt;1 C 11&lt;BR /&gt;...&lt;BR /&gt;1 C 20&lt;BR /&gt;&lt;BR /&gt;for subject 1?</description>
      <pubDate>Tue, 20 Sep 2022 17:58:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/data-filling-gaps/m-p/834339#M35862</guid>
      <dc:creator>hellorc</dc:creator>
      <dc:date>2022-09-20T17:58:03Z</dc:date>
    </item>
    <item>
      <title>Re: data - filling gaps</title>
      <link>https://communities.sas.com/t5/New-SAS-User/data-filling-gaps/m-p/834377#M35863</link>
      <description>&lt;P&gt;Try this (for 3 it's 23 just to test if it's covering info from data):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id city $ week;
datalines;
1 A 3
1 B 6
1 B 7
1 C 10
2 A 3
2 B 6
2 B 9
2 C 10
2 C 15
3 A 3
3 B 6
3 B 9
3 C 23
;
run;


data have2;
set have;
by id;
if not first.id;
rename week=week2;
drop city;
run;

data want;
merge have have2; 
by id;
if last.id then 
  do week = week to max(week2,20);
    output;
  end;
else 
  do week = week to week2-1;
    output;
  end;
drop week2;
run;

proc print data = want; 
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Tue, 20 Sep 2022 20:17:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/data-filling-gaps/m-p/834377#M35863</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2022-09-20T20:17:47Z</dc:date>
    </item>
    <item>
      <title>Re: data - filling gaps</title>
      <link>https://communities.sas.com/t5/New-SAS-User/data-filling-gaps/m-p/834452#M35883</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id city $ week;
datalines;
1 A 3
1 B 6
1 B 7
1 C 10
2 A 3
2 B 6
2 B 9
2 C 10
2 C 15
;
run;

data want;
merge have have(keep=id week rename=(id=_id week=_week) firstobs=2);
output;
if id=_id then do;
 do week=week+1 to _week-1;
  output;
 end;
end;
drop _:;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 21 Sep 2022 11:54:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/data-filling-gaps/m-p/834452#M35883</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2022-09-21T11:54:06Z</dc:date>
    </item>
    <item>
      <title>Re: data - filling gaps</title>
      <link>https://communities.sas.com/t5/New-SAS-User/data-filling-gaps/m-p/834459#M35885</link>
      <description>Thank you both, yabwon and Ksharp!! Both worked!</description>
      <pubDate>Wed, 21 Sep 2022 12:24:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/data-filling-gaps/m-p/834459#M35885</guid>
      <dc:creator>hellorc</dc:creator>
      <dc:date>2022-09-21T12:24:59Z</dc:date>
    </item>
  </channel>
</rss>

