<?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 Fill in rows with previous value if next value is the same. in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Fill-in-rows-with-previous-value-if-next-value-is-the-same/m-p/775703#M246589</link>
    <description>&lt;P&gt;I have a file with a case number that is sporadically filled in.&amp;nbsp; The file is 500k plus rows, If the next time a case # is filled in happens to be the same case # as before I want to fill in the blanks.&amp;nbsp; I thought of using lag() but some of the missing rows are pretty large.&amp;nbsp; If the next case # populated is different the rows between should be left blank.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data have;&lt;BR /&gt;infile cards dsd;&lt;BR /&gt;input case$ ;&lt;BR /&gt;cards;&lt;BR /&gt;500&lt;BR /&gt;,&lt;BR /&gt;,&lt;BR /&gt;,&lt;BR /&gt;500&lt;BR /&gt;,&lt;BR /&gt;600&lt;BR /&gt;,&lt;BR /&gt;,&lt;BR /&gt;700&lt;BR /&gt;700&lt;BR /&gt;,&lt;BR /&gt;700&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;data want;&lt;BR /&gt;infile cards dsd;&lt;BR /&gt;input case$ ;&lt;BR /&gt;cards;&lt;BR /&gt;500&lt;BR /&gt;500&lt;BR /&gt;500&lt;BR /&gt;500&lt;BR /&gt;500&lt;BR /&gt;,&lt;BR /&gt;600&lt;BR /&gt;,&lt;BR /&gt;,&lt;BR /&gt;700&lt;BR /&gt;700&lt;BR /&gt;700&lt;BR /&gt;700&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;</description>
    <pubDate>Thu, 21 Oct 2021 17:31:59 GMT</pubDate>
    <dc:creator>Steelers_In_DC</dc:creator>
    <dc:date>2021-10-21T17:31:59Z</dc:date>
    <item>
      <title>Fill in rows with previous value if next value is the same.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Fill-in-rows-with-previous-value-if-next-value-is-the-same/m-p/775703#M246589</link>
      <description>&lt;P&gt;I have a file with a case number that is sporadically filled in.&amp;nbsp; The file is 500k plus rows, If the next time a case # is filled in happens to be the same case # as before I want to fill in the blanks.&amp;nbsp; I thought of using lag() but some of the missing rows are pretty large.&amp;nbsp; If the next case # populated is different the rows between should be left blank.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data have;&lt;BR /&gt;infile cards dsd;&lt;BR /&gt;input case$ ;&lt;BR /&gt;cards;&lt;BR /&gt;500&lt;BR /&gt;,&lt;BR /&gt;,&lt;BR /&gt;,&lt;BR /&gt;500&lt;BR /&gt;,&lt;BR /&gt;600&lt;BR /&gt;,&lt;BR /&gt;,&lt;BR /&gt;700&lt;BR /&gt;700&lt;BR /&gt;,&lt;BR /&gt;700&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;data want;&lt;BR /&gt;infile cards dsd;&lt;BR /&gt;input case$ ;&lt;BR /&gt;cards;&lt;BR /&gt;500&lt;BR /&gt;500&lt;BR /&gt;500&lt;BR /&gt;500&lt;BR /&gt;500&lt;BR /&gt;,&lt;BR /&gt;600&lt;BR /&gt;,&lt;BR /&gt;,&lt;BR /&gt;700&lt;BR /&gt;700&lt;BR /&gt;700&lt;BR /&gt;700&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Thu, 21 Oct 2021 17:31:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Fill-in-rows-with-previous-value-if-next-value-is-the-same/m-p/775703#M246589</guid>
      <dc:creator>Steelers_In_DC</dc:creator>
      <dc:date>2021-10-21T17:31:59Z</dc:date>
    </item>
    <item>
      <title>Re: Fill in rows with previous value if next value is the same.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Fill-in-rows-with-previous-value-if-next-value-is-the-same/m-p/775719#M246592</link>
      <description>&lt;P&gt;There is also an ID field,&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I can use:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;set have;&lt;/P&gt;
&lt;P&gt;by id;&lt;/P&gt;
&lt;P&gt;retain _case;&lt;/P&gt;
&lt;P&gt;if not missing(case_id) then _case = case_id;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;that gets me part of the way, but it fills in the last rows when the next value will not be the same.&amp;nbsp; &amp;nbsp;I suppose I could sort the other way and delete some.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 21 Oct 2021 18:03:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Fill-in-rows-with-previous-value-if-next-value-is-the-same/m-p/775719#M246592</guid>
      <dc:creator>Steelers_In_DC</dc:creator>
      <dc:date>2021-10-21T18:03:49Z</dc:date>
    </item>
    <item>
      <title>Re: Fill in rows with previous value if next value is the same.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Fill-in-rows-with-previous-value-if-next-value-is-the-same/m-p/775721#M246593</link>
      <description>&lt;P&gt;Try this&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile cards dsd;
input case ;
cards;
500
,
,
,
500
,
600
,
,
700
700
,
700
;
run;

data want;

   do _N_ = 1 by 1 until (case);
      if _N_ = 1 then c = case;
      set have;
   end;
   
   flag = (c = case);

   do _N_ = 1 to _N_;
      set have;
      if flag then case = c;
      output;
   end;

run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 21 Oct 2021 18:05:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Fill-in-rows-with-previous-value-if-next-value-is-the-same/m-p/775721#M246593</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2021-10-21T18:05:36Z</dc:date>
    </item>
    <item>
      <title>Re: Fill in rows with previous value if next value is the same.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Fill-in-rows-with-previous-value-if-next-value-is-the-same/m-p/775723#M246595</link>
      <description>&lt;P&gt;Next code can create a dataset where cases are in next format:&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;case1&amp;nbsp; case2&amp;nbsp; case3&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;case2&amp;nbsp; case3&amp;nbsp; case4&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;case3&amp;nbsp; case4&amp;nbsp; case5&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;...&lt;/P&gt;
&lt;P&gt;&amp;nbsp; case(n-2) case(n-1) case (n)&lt;/P&gt;
&lt;P&gt;&amp;nbsp; case (n-1) case(n)&lt;/P&gt;
&lt;P&gt;&amp;nbsp; case(n)&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; /* &amp;lt;&amp;lt;&amp;lt; the last one */&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The code is not tested:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data temp;
  merge have (rename=(case=case_prev))   /* firstobs=1 */
             have (firstobs=2 rename=(case=case_current))
             have (firstobs=3 reanme=(case=case_next);
    by;
 run;
 data want (keep=case);
 set temp end=eof;
      retain previous_case;
       if _N_=1 then do; 
           case = case_prev; output; 
           if  case_next = case_curr then case=.;   /* missing value as blank */
           else case = case_curr;
           output;
          previous_case = case;
       end; else do;
          if case_curr = previous_case then case=.;
          else case = case_curr;
          output;
          previous_case = case;
       end; 

       if eof then do; case = case_prev; output; end;
run;

          &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 21 Oct 2021 18:09:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Fill-in-rows-with-previous-value-if-next-value-is-the-same/m-p/775723#M246595</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2021-10-21T18:09:46Z</dc:date>
    </item>
    <item>
      <title>Re: Fill in rows with previous value if next value is the same.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Fill-in-rows-with-previous-value-if-next-value-is-the-same/m-p/775735#M246597</link>
      <description>&lt;P&gt;this looks great, can up modify this for case as a character?&lt;/P&gt;</description>
      <pubDate>Thu, 21 Oct 2021 19:55:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Fill-in-rows-with-previous-value-if-next-value-is-the-same/m-p/775735#M246597</guid>
      <dc:creator>Steelers_In_DC</dc:creator>
      <dc:date>2021-10-21T19:55:47Z</dc:date>
    </item>
    <item>
      <title>Re: Fill in rows with previous value if next value is the same.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Fill-in-rows-with-previous-value-if-next-value-is-the-same/m-p/775756#M246610</link>
      <description>&lt;P&gt;I see the issue, case is before the set statement so it's a number.&amp;nbsp; I added format case $500.; after the data step and this works.&amp;nbsp; thanks for the help.&lt;/P&gt;</description>
      <pubDate>Thu, 21 Oct 2021 21:00:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Fill-in-rows-with-previous-value-if-next-value-is-the-same/m-p/775756#M246610</guid>
      <dc:creator>Steelers_In_DC</dc:creator>
      <dc:date>2021-10-21T21:00:26Z</dc:date>
    </item>
    <item>
      <title>Re: Fill in rows with previous value if next value is the same.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Fill-in-rows-with-previous-value-if-next-value-is-the-same/m-p/775861#M246651</link>
      <description>&lt;P&gt;Just for fun .&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile cards dsd;
n+1;
input case ;
cards;
500
,
,
,
500
,
600
,
,
700
700
,
700
;
run;
data have;
 set have;
 retain x1;
 if not missing(case) then x1=case;
run;
proc sort data=have;by descending n;run;
data have;
 set have;
 retain x2;
 if not missing(case)  then x2=case;
run;
proc sort data=have;by  n;run;
data want;
 set have;
 if x1=x2 then case=x1;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 22 Oct 2021 12:57:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Fill-in-rows-with-previous-value-if-next-value-is-the-same/m-p/775861#M246651</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-10-22T12:57:59Z</dc:date>
    </item>
  </channel>
</rss>

