<?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: SAS programming: add missing rows to a long file in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/SAS-programming-add-missing-rows-to-a-long-file/m-p/950063#M371561</link>
    <description>&lt;P&gt;This will fill missing rows at the end and in the middle.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
   do projid='28533', '29344';
      do year = 0 to 3, 5;
         y = ranuni(1234);
         output;
         end;
      end;
   run;

proc format;
   value xyear(notsorted)
      0 = 'Baseline'
      1 = '1 Year'
      2 = '2 Year'
      3 = '3 Year'
      4 = '4 Year'
      5 = '5 Year'
      6 = '6 Year'
      7 = '7 Year'
      ;
   quit;
proc summary data=have nway missing completetypes;
   by projid;
   class year / preloadfmt;
   format year xyear.;
   output out=expanded(drop=_type_);
   run;
data want;
   merge expanded have;
   by projid year;
   run;
proc print;
   run;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.PNG" style="width: 233px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/102006i09422C261E1D7738/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture.PNG" alt="Capture.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 06 Nov 2024 18:15:50 GMT</pubDate>
    <dc:creator>data_null__</dc:creator>
    <dc:date>2024-11-06T18:15:50Z</dc:date>
    <item>
      <title>SAS programming: add missing rows to a long file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-programming-add-missing-rows-to-a-long-file/m-p/950043#M371557</link>
      <description>&lt;P&gt;I have a long file with data like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="hein68_0-1730908528973.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/101992i07BCD4F170D51E4C/image-size/medium?v=v2&amp;amp;px=400" role="button" title="hein68_0-1730908528973.png" alt="hein68_0-1730908528973.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Some projids have one or more of the 13 rows missing, but it's random as to which row(s) are missing.&amp;nbsp; What is the easiest way to write code to add the rows that are missing for each projid?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks!!&lt;/P&gt;</description>
      <pubDate>Wed, 06 Nov 2024 15:58:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-programming-add-missing-rows-to-a-long-file/m-p/950043#M371557</guid>
      <dc:creator>hein68</dc:creator>
      <dc:date>2024-11-06T15:58:17Z</dc:date>
    </item>
    <item>
      <title>Re: SAS programming: add missing rows to a long file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-programming-add-missing-rows-to-a-long-file/m-p/950046#M371559</link>
      <description>&lt;P&gt;How many obs should each projid have? Is the number constant?&lt;/P&gt;
&lt;P&gt;What is the real contents of "time"? Can you show the unformatted values?&lt;/P&gt;
&lt;P&gt;Can you please post data in usable form, so that we have something to work with?&lt;/P&gt;
&lt;P&gt;Usable form = data step with datalines.&lt;/P&gt;</description>
      <pubDate>Wed, 06 Nov 2024 16:19:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-programming-add-missing-rows-to-a-long-file/m-p/950046#M371559</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2024-11-06T16:19:49Z</dc:date>
    </item>
    <item>
      <title>Re: SAS programming: add missing rows to a long file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-programming-add-missing-rows-to-a-long-file/m-p/950051#M371560</link>
      <description>&lt;P&gt;Here is my code that works on fake data. This could be extremely slow on large data sets. But really, we need answers to all the questions from&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15475"&gt;@andreas_lds&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data years;
input id time var1;
cards;
12345  1   0.089
12345  2   0.099
12345  3   0.0762
12345  5   0.103
12345  6   0.089
12345  7   0.089
12345  8   0.089
12345  9   0.089
12345 10   0.089
12345 11   0.089
;
data thirteen;
    do time=1 to 13;
        output;
    end;
run;

proc sql;
    create table ids as select distinct id from years;
    create table all_years as select distinct a.id,b.time from years as a,thirteen as b; /* Cartesian join */
    create table want as select a.id,a.time,b.var1 from all_years as a left join years as b
        on a.id=b.id and a.time=b.time;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 06 Nov 2024 16:54:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-programming-add-missing-rows-to-a-long-file/m-p/950051#M371560</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-11-06T16:54:13Z</dc:date>
    </item>
    <item>
      <title>Re: SAS programming: add missing rows to a long file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-programming-add-missing-rows-to-a-long-file/m-p/950063#M371561</link>
      <description>&lt;P&gt;This will fill missing rows at the end and in the middle.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
   do projid='28533', '29344';
      do year = 0 to 3, 5;
         y = ranuni(1234);
         output;
         end;
      end;
   run;

proc format;
   value xyear(notsorted)
      0 = 'Baseline'
      1 = '1 Year'
      2 = '2 Year'
      3 = '3 Year'
      4 = '4 Year'
      5 = '5 Year'
      6 = '6 Year'
      7 = '7 Year'
      ;
   quit;
proc summary data=have nway missing completetypes;
   by projid;
   class year / preloadfmt;
   format year xyear.;
   output out=expanded(drop=_type_);
   run;
data want;
   merge expanded have;
   by projid year;
   run;
proc print;
   run;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.PNG" style="width: 233px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/102006i09422C261E1D7738/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture.PNG" alt="Capture.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 06 Nov 2024 18:15:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-programming-add-missing-rows-to-a-long-file/m-p/950063#M371561</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2024-11-06T18:15:50Z</dc:date>
    </item>
    <item>
      <title>Re: SAS programming: add missing rows to a long file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-programming-add-missing-rows-to-a-long-file/m-p/950065#M371562</link>
      <description>&lt;P&gt;Each projid should have 13 observations/rows.&amp;nbsp; In the example I show in the screenshot, the original data source does not include a row for observation 10 (for 10 years).&amp;nbsp; I need to get a blank row added that would be for observation 10 (10 years).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In another example, the projid had 2 observations (baseline and 1 year), so in this case I need to add the blank rows for years 3 through 13.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This code shows how the time variable is formatted.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;value timept 0="Baseline" 1="1 year" 2="2 year" 3="3 year"
		4="4 year" 5="5 year" 6="6 year" 7="7 year" 8="8 year"
		9="9 year" 10="10 year" 11="11 year" 12="12 year" 13="13 year"
		14="14 year" 15="15 year" 16="16 year" 17="17 year"
		18="18 year" 19="19 year";&lt;/PRE&gt;
&lt;P&gt;Thanks!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 06 Nov 2024 17:39:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-programming-add-missing-rows-to-a-long-file/m-p/950065#M371562</guid>
      <dc:creator>hein68</dc:creator>
      <dc:date>2024-11-06T17:39:56Z</dc:date>
    </item>
    <item>
      <title>Re: SAS programming: add missing rows to a long file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-programming-add-missing-rows-to-a-long-file/m-p/950077#M371563</link>
      <description>&lt;P&gt;This program has two data steps, but only demands the original data from storage&amp;nbsp; be retrieved once, because it uses a data set view.&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=" language-sas"&gt;proc format;
  value timept 0="Baseline" 1="1 year" 2="2 year" 3="3 year"
		4="4 year" 5="5 year" 6="6 year" 7="7 year" 8="8 year"
		9="9 year" 10="10 year" 11="11 year" 12="12 year" 13="13 year"
		14="14 year" 15="15 year" 16="16 year" 17="17 year"
		18="18 year" 19="19 year";

data years;
input id time var1;
format time timept. ;
cards;
12345  0   0.089
12345  1   0.099
12345  3   0.0762
12345  5   0.103
12345  6   0.089
12345  7   0.089
12345  8   0.089
12345  9   0.089
12345  10   0.089
12345  11   0.089
run;

data all_time_values/view=all_time_values ;
  set years (keep=id);
  by id;
  if first.id;
  do time=0 to 13;  /* Baseline to Year 13 */
    output;
  end;
run;


data want ;
  merge all_time_values years;
  by id time;
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;This assumes the data are sorted by ID and time.&lt;/P&gt;</description>
      <pubDate>Wed, 06 Nov 2024 19:16:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-programming-add-missing-rows-to-a-long-file/m-p/950077#M371563</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2024-11-06T19:16:05Z</dc:date>
    </item>
    <item>
      <title>Re: SAS programming: add missing rows to a long file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-programming-add-missing-rows-to-a-long-file/m-p/950081#M371565</link>
      <description>&lt;P&gt;This worked beautifully!&amp;nbsp; Thank you!&lt;/P&gt;</description>
      <pubDate>Wed, 06 Nov 2024 21:01:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-programming-add-missing-rows-to-a-long-file/m-p/950081#M371565</guid>
      <dc:creator>hein68</dc:creator>
      <dc:date>2024-11-06T21:01:38Z</dc:date>
    </item>
  </channel>
</rss>

