<?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: repeat previous row when missing for each columns in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/repeat-previous-row-when-missing-for-each-columns/m-p/543113#M7646</link>
    <description>&lt;P&gt;The approach for a long dataset looks like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id $ varnum value;
cards;
a 1 1
a 2 2
a 3 3
b 1 2
b 2 .
b 3 4
c 1 3
c 2 1
c 3 .
d 1 .
d 2 .
d 3 .
;
run;

proc sort data=have;
by varnum id;
run;

data want;
set have;
by varnum;
retain _value;
if first.varnum then _value = .;
if value ne .
then _value = value;
else value = _value;
drop _value;
run;

proc sort data=want;
by id varnum;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This code can easily be expanded to handle more complex groups, whereas in a wide format you'd have to define more and more arrays.&lt;/P&gt;</description>
    <pubDate>Thu, 14 Mar 2019 12:14:23 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2019-03-14T12:14:23Z</dc:date>
    <item>
      <title>repeat previous row when missing for each columns</title>
      <link>https://communities.sas.com/t5/New-SAS-User/repeat-previous-row-when-missing-for-each-columns/m-p/543086#M7641</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a data set with columns an_2005, an_2006.....an_2018.&lt;/P&gt;&lt;P&gt;I succedeed to get&lt;/P&gt;&lt;P&gt;AN_2006 AN_2007 AN_2008 AN_2009 AN_2010 AN_2011 AN_2012 AN_2013 AN_2014 AN_2015&lt;/P&gt;&lt;P&gt;AN_2016 AN_2017 AN_2018 into a macro variable "liste_annee".&lt;/P&gt;&lt;P&gt;Now I would like the non missing value to be repeated whenever a missing value met.&lt;/P&gt;&lt;P&gt;so muy code is :&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data t_mois_trsp_A ;
set t_mois_trsp_A ;
array v{*} &amp;amp;liste_annee ;
do i = 1 to dim(v);
	retain value ;
	if not missing(v{i}) then value = v{i} ; else v{i} = value ;
	drop value ;
end;
drop i ; 
run ;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;But It does not work as attended, I noticed that values comning from a column (year) are repeated on différents columns.&lt;/P&gt;&lt;P&gt;many thanks in advance for your help&lt;/P&gt;&lt;P&gt;regards&lt;/P&gt;&lt;P&gt;Nasser&lt;/P&gt;</description>
      <pubDate>Thu, 14 Mar 2019 11:18:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/repeat-previous-row-when-missing-for-each-columns/m-p/543086#M7641</guid>
      <dc:creator>Nasser_DRMCP</dc:creator>
      <dc:date>2019-03-14T11:18:15Z</dc:date>
    </item>
    <item>
      <title>Re: repeat previous row when missing for each columns</title>
      <link>https://communities.sas.com/t5/New-SAS-User/repeat-previous-row-when-missing-for-each-columns/m-p/543104#M7643</link>
      <description>&lt;P&gt;No need for macro-variables, you can use an_2008-an_2018 in the array statement. What do you expect if an_2008 is missing?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The statements drop and retain are hardly useful to solve the problem, haven't you read the documentation?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Overwriting a dataset is seldom a good idea, because all steps leading to the data set have to be repeated if something goes wrong.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The following step is untested, if you want tested code, provide data in usable form.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.want;
    set t_mois_trsp_A ;
    array v[*] an_2008-an_2018;

    do i = 2 to dim(v);
        if missing(v[i]) then v[i] = v[i-1];
    end;

    drop i;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 14 Mar 2019 11:54:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/repeat-previous-row-when-missing-for-each-columns/m-p/543104#M7643</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2019-03-14T11:54:06Z</dc:date>
    </item>
    <item>
      <title>Re: repeat previous row when missing for each columns</title>
      <link>https://communities.sas.com/t5/New-SAS-User/repeat-previous-row-when-missing-for-each-columns/m-p/543105#M7644</link>
      <description>&lt;P&gt;Maxims 19 &amp;amp; 33.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;With a wide format, you need to have a retained variable for every member of your array, and that is done most easily by defining a temporary array:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input val1 val2 val3;
cards;
1 2 3
2 . 4
3 1 .
. . .
;
run;

%let varlist=val1 val2 val3;
%let arraysize=%sysfunc(countw(&amp;amp;varlist.));

data want;
set have;
array v1 {&amp;amp;arraysize.} &amp;amp;varlist.;
array v2 {&amp;amp;arraysize.} _temporary_;
do i = 1 to dim(v1);
  if not missing(v1{i})
  then v2{i} = v1{i};
  else v1{i} = v2{i};
end;
drop i;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 14 Mar 2019 11:54:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/repeat-previous-row-when-missing-for-each-columns/m-p/543105#M7644</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-03-14T11:54:28Z</dc:date>
    </item>
    <item>
      <title>Re: repeat previous row when missing for each columns</title>
      <link>https://communities.sas.com/t5/New-SAS-User/repeat-previous-row-when-missing-for-each-columns/m-p/543106#M7645</link>
      <description>&lt;P&gt;Ignore the code posted, i should have read your post more carefully &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 14 Mar 2019 11:55:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/repeat-previous-row-when-missing-for-each-columns/m-p/543106#M7645</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2019-03-14T11:55:46Z</dc:date>
    </item>
    <item>
      <title>Re: repeat previous row when missing for each columns</title>
      <link>https://communities.sas.com/t5/New-SAS-User/repeat-previous-row-when-missing-for-each-columns/m-p/543113#M7646</link>
      <description>&lt;P&gt;The approach for a long dataset looks like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id $ varnum value;
cards;
a 1 1
a 2 2
a 3 3
b 1 2
b 2 .
b 3 4
c 1 3
c 2 1
c 3 .
d 1 .
d 2 .
d 3 .
;
run;

proc sort data=have;
by varnum id;
run;

data want;
set have;
by varnum;
retain _value;
if first.varnum then _value = .;
if value ne .
then _value = value;
else value = _value;
drop _value;
run;

proc sort data=want;
by id varnum;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This code can easily be expanded to handle more complex groups, whereas in a wide format you'd have to define more and more arrays.&lt;/P&gt;</description>
      <pubDate>Thu, 14 Mar 2019 12:14:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/repeat-previous-row-when-missing-for-each-columns/m-p/543113#M7646</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-03-14T12:14:23Z</dc:date>
    </item>
    <item>
      <title>Re: repeat previous row when missing for each columns</title>
      <link>https://communities.sas.com/t5/New-SAS-User/repeat-previous-row-when-missing-for-each-columns/m-p/543133#M7648</link>
      <description>&lt;P&gt;You have received a number of answers that are close to working.&amp;nbsp; Here's one that combines the best features of all.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;First, a question.&amp;nbsp; Do you have AN_2005 in your data, but not in your macro variable?&amp;nbsp; I'm going to assume the answer is yes but if the real answer is no, you might have to adjust slightly by removing AN_2005 from the ARRAY statement..&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   set have;
   array an {*} an_2005 &amp;amp;liste_anee;
   do k=2 to dim(an);
      if an{k} = . then an{k} = an{k-1};
   end;
   drop k;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 14 Mar 2019 13:09:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/repeat-previous-row-when-missing-for-each-columns/m-p/543133#M7648</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2019-03-14T13:09:35Z</dc:date>
    </item>
    <item>
      <title>Re: repeat previous row when missing for each columns</title>
      <link>https://communities.sas.com/t5/New-SAS-User/repeat-previous-row-when-missing-for-each-columns/m-p/543143#M7649</link>
      <description>&lt;P&gt;thanks all for your precious help.&lt;BR /&gt;unfortunatly, I did not succeed with your suggestions.&lt;BR /&gt;But I did succeed with this code below&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;%macro&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; mc_loop_repeat_missing(p_table=t_mois_trsp_A) ;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;%let&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; i=1;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;%let&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; an_loop = &lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;%scan&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;(&amp;amp;liste_annee, &amp;amp;i, &lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;%str&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;( ));&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;%do&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;%while&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; (&amp;amp;an_loop ne );&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;%let&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; an_loop = &lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;%scan&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;(&amp;amp;liste_annee, &amp;amp;i, &lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;%str&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;( ));&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data &amp;amp;p_table ;&lt;/P&gt;&lt;P&gt;set &amp;amp;p_table ;&lt;/P&gt;&lt;P&gt;retain _&amp;amp;an_loop ;&lt;/P&gt;&lt;P&gt;if not missing(&amp;amp;an_loop) then _&amp;amp;an_loop = &amp;amp;an_loop ; else &amp;amp;an_loop = _&amp;amp;an_loop ;&lt;/P&gt;&lt;P&gt;drop _&amp;amp;an_loop ;&lt;/P&gt;&lt;P&gt;run ;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;%LET&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; i = &lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;%eval&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;(&amp;amp;i+1);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;%let&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; an_loop = &lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;%scan&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;(&amp;amp;liste_annee, &amp;amp;i, &lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;%str&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;( ));&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;%end&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; ;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;%mend&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; ;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 14 Mar 2019 13:23:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/repeat-previous-row-when-missing-for-each-columns/m-p/543143#M7649</guid>
      <dc:creator>Nasser_DRMCP</dc:creator>
      <dc:date>2019-03-14T13:23:31Z</dc:date>
    </item>
  </channel>
</rss>

