<?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: replace missing value with an array in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/replace-missing-value-with-an-array/m-p/334251#M75490</link>
    <description>&lt;P&gt;i just want to replicate the code below to macro. the code below is replace the variable 'audit' missing value by the previous value.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA temp2;
   SET temp;
   by countryname;
   retain _audit;
   if countryname = lag(countryname) then do;
   if not missing(audit) then _audit=audit;
   else audit=_audit;
   end;
   drop _audit;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 20 Feb 2017 04:13:48 GMT</pubDate>
    <dc:creator>JNWong</dc:creator>
    <dc:date>2017-02-20T04:13:48Z</dc:date>
    <item>
      <title>replace missing value with an array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/replace-missing-value-with-an-array/m-p/334247#M75486</link>
      <description>&lt;P&gt;hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; as i want to replace mutiple variables' missing value by the previus value using an array.&lt;/P&gt;&lt;P&gt;i have tried the code below, &amp;nbsp;expected to replace three variables,(not create new variables): dev,legal,audit. which was defined as vars.&lt;/P&gt;&lt;P&gt;&amp;nbsp;however,it does not work. &amp;nbsp;and except for &amp;nbsp;other variables there only a new _dev variable in the output dataset. whose value is all missing. it is not my expected results.i do not need the new variables ,just replace the original variable' missing value.&lt;/P&gt;&lt;PRE&gt;%macro replace(n1=,id=,data=,n4=,vars=);
   %let m1 = %sysfunc(lag(&amp;amp;id));
   proc sort data = &amp;amp;n1 out = temp;
   by &amp;amp;id;
   run;
data &amp;amp;data;
   set temp;
   by &amp;amp;id;
   array miss(&amp;amp;n4) &amp;amp;vars;
   array missg(&amp;amp;n4);
   retain missg(&amp;amp;n4);
   do i = 1 to &amp;amp;n4;
%if &amp;amp;id = m1 %then %do;
missg(i)=miss(i);
else miss(i)=missg(i);
%end;
end; 
  drop of missg: i;
run;
%mend replace;
%replace(n1=temp,id=countryname,data=temp1,n4=3,vars=dev legal audit);&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 20 Feb 2017 03:31:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/replace-missing-value-with-an-array/m-p/334247#M75486</guid>
      <dc:creator>JNWong</dc:creator>
      <dc:date>2017-02-20T03:31:43Z</dc:date>
    </item>
    <item>
      <title>Re: replace missing value with an array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/replace-missing-value-with-an-array/m-p/334248#M75487</link>
      <description>&lt;P&gt;Post sample data so we can test your code.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why are you using macro If/then loop?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, in the IF/THEN you refer to m1, but are missing the ampersand, most likely should be &amp;amp;M1.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 20 Feb 2017 03:56:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/replace-missing-value-with-an-array/m-p/334248#M75487</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-02-20T03:56:46Z</dc:date>
    </item>
    <item>
      <title>Re: replace missing value with an array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/replace-missing-value-with-an-array/m-p/334249#M75488</link>
      <description>&lt;P&gt;I also don't think M1 makes sense at all with a LAG function.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It might be worth you commenting your code so we help you figure out where you went wrong.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 20 Feb 2017 03:58:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/replace-missing-value-with-an-array/m-p/334249#M75488</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-02-20T03:58:25Z</dc:date>
    </item>
    <item>
      <title>Re: replace missing value with an array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/replace-missing-value-with-an-array/m-p/334250#M75489</link>
      <description>&lt;P&gt;Thanks, i changed the m1 to &amp;amp;m1, but i still get the unexpected result. i am wondering if i should try the do over. id is countryname.&lt;/P&gt;&lt;PRE&gt;data temp3;
input countryname $1-5 +1 dev 1. +1 legal 1. +1 audit 1.;
datalines;
china . . .
china 0 0 5
china 0 0 5
china . . .
china . 5 9
japan . . .
japan 0 5 9
japan . . 5
japan 0 . .
;
run;&lt;/PRE&gt;</description>
      <pubDate>Mon, 20 Feb 2017 04:07:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/replace-missing-value-with-an-array/m-p/334250#M75489</guid>
      <dc:creator>JNWong</dc:creator>
      <dc:date>2017-02-20T04:07:46Z</dc:date>
    </item>
    <item>
      <title>Re: replace missing value with an array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/replace-missing-value-with-an-array/m-p/334251#M75490</link>
      <description>&lt;P&gt;i just want to replicate the code below to macro. the code below is replace the variable 'audit' missing value by the previous value.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA temp2;
   SET temp;
   by countryname;
   retain _audit;
   if countryname = lag(countryname) then do;
   if not missing(audit) then _audit=audit;
   else audit=_audit;
   end;
   drop _audit;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 20 Feb 2017 04:13:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/replace-missing-value-with-an-array/m-p/334251#M75490</guid>
      <dc:creator>JNWong</dc:creator>
      <dc:date>2017-02-20T04:13:48Z</dc:date>
    </item>
    <item>
      <title>Re: replace missing value with an array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/replace-missing-value-with-an-array/m-p/334252#M75491</link>
      <description>&lt;P&gt;Something like below could work.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input countryname $1-5 +1 dev 1. +1 legal 1. +1 audit 1.;
datalines;
china . . .
china 0 0 5
china 0 0 5
china . . .
china . 5 9
japan . . .
japan 0 5 9
japan . . 5
japan 0 . .
;
run;

data want(drop=_i);
  set have;
  by countryname notsorted;

  array vars {3} dev legal audit;
  array lag_vars {3} 8 _temporary_;

  do _i=1 to dim(vars);
    if not first.countryname then vars[_i]=coalesce(vars[_i],lag_vars[_i]);
    lag_vars[_i]=vars[_i];
  end;

run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 20 Feb 2017 04:34:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/replace-missing-value-with-an-array/m-p/334252#M75491</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2017-02-20T04:34:14Z</dc:date>
    </item>
    <item>
      <title>Re: replace missing value with an array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/replace-missing-value-with-an-array/m-p/334253#M75492</link>
      <description>&lt;P&gt;Like this?&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;
%macro dolag(idvar=, lagvars=);
  %local var;
  %do i=1 %to %sysfunc(countw(&amp;amp;lagvars));
    %let var=%scan(&amp;amp;lagvars,&amp;amp;i);   
    _LAGVAR_&amp;amp;i=lag(&amp;amp;var);
    if lag(&amp;amp;idvar.)=&amp;amp;idvar. &amp;amp; missing(&amp;amp;var) then &amp;amp;var=_LAGVAR_&amp;amp;i;
  %end;
  drop _LAGVAR_:;
%mend;


data WANT;
  set HAVE;
  %dolag(idvar=COUNTRYNAME, lagvars=DEV LEGAL AUDIT);
run;

proc print noobs; 
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;&amp;nbsp;&lt;/P&gt;
&lt;DIV class="branch"&gt;
&lt;DIV&gt;
&lt;DIV align="center"&gt;
&lt;TABLE class="table" summary="Procedure Print: Data Set WORK.WANT" frame="box" rules="all" cellpadding="5" cellspacing="0"&gt;
&lt;THEAD&gt;
&lt;TR&gt;
&lt;TH class="l header" scope="col"&gt;COUNTRYNAME&lt;/TH&gt;
&lt;TH class="r header" scope="col"&gt;DEV&lt;/TH&gt;
&lt;TH class="r header" scope="col"&gt;LEGAL&lt;/TH&gt;
&lt;TH class="r header" scope="col"&gt;AUDIT&lt;/TH&gt;
&lt;/TR&gt;
&lt;/THEAD&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD class="l data"&gt;china&lt;/TD&gt;
&lt;TD class="r data"&gt;.&lt;/TD&gt;
&lt;TD class="r data"&gt;.&lt;/TD&gt;
&lt;TD class="r data"&gt;.&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="l data"&gt;china&lt;/TD&gt;
&lt;TD class="r data"&gt;0&lt;/TD&gt;
&lt;TD class="r data"&gt;0&lt;/TD&gt;
&lt;TD class="r data"&gt;5&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="l data"&gt;china&lt;/TD&gt;
&lt;TD class="r data"&gt;0&lt;/TD&gt;
&lt;TD class="r data"&gt;0&lt;/TD&gt;
&lt;TD class="r data"&gt;5&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="l data"&gt;china&lt;/TD&gt;
&lt;TD class="r data"&gt;0&lt;/TD&gt;
&lt;TD class="r data"&gt;0&lt;/TD&gt;
&lt;TD class="r data"&gt;5&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="l data"&gt;china&lt;/TD&gt;
&lt;TD class="r data"&gt;.&lt;/TD&gt;
&lt;TD class="r data"&gt;5&lt;/TD&gt;
&lt;TD class="r data"&gt;9&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="l data"&gt;japan&lt;/TD&gt;
&lt;TD class="r data"&gt;.&lt;/TD&gt;
&lt;TD class="r data"&gt;.&lt;/TD&gt;
&lt;TD class="r data"&gt;.&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="l data"&gt;japan&lt;/TD&gt;
&lt;TD class="r data"&gt;0&lt;/TD&gt;
&lt;TD class="r data"&gt;5&lt;/TD&gt;
&lt;TD class="r data"&gt;9&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="l data"&gt;japan&lt;/TD&gt;
&lt;TD class="r data"&gt;0&lt;/TD&gt;
&lt;TD class="r data"&gt;5&lt;/TD&gt;
&lt;TD class="r data"&gt;5&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="l data"&gt;japan&lt;/TD&gt;
&lt;TD class="r data"&gt;0&lt;/TD&gt;
&lt;TD class="r data"&gt;.&lt;/TD&gt;
&lt;TD class="r data"&gt;5&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 20 Feb 2017 04:36:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/replace-missing-value-with-an-array/m-p/334253#M75492</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2017-02-20T04:36:46Z</dc:date>
    </item>
    <item>
      <title>Re: replace missing value with an array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/replace-missing-value-with-an-array/m-p/334262#M75494</link>
      <description>&lt;P&gt;thanks ,i have tried the code,but still have some missing values.&lt;/P&gt;&lt;P&gt;as for the dataset you have printed.&lt;/P&gt;&lt;P&gt;the last line 'japan 0 . 5 &amp;nbsp;i hope to replace all the missing value within a group.&amp;nbsp;&lt;/P&gt;&lt;P&gt;so i tried the retain function.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 20 Feb 2017 05:43:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/replace-missing-value-with-an-array/m-p/334262#M75494</guid>
      <dc:creator>JNWong</dc:creator>
      <dc:date>2017-02-20T05:43:31Z</dc:date>
    </item>
    <item>
      <title>Re: replace missing value with an array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/replace-missing-value-with-an-array/m-p/334264#M75495</link>
      <description>You said:  replace mutiple variables' missing value by the previus value&lt;BR /&gt;Not:  replace mutiple variables' missing value by the previus non-missing value.&lt;BR /&gt;Which one is it?</description>
      <pubDate>Mon, 20 Feb 2017 06:11:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/replace-missing-value-with-an-array/m-p/334264#M75495</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2017-02-20T06:11:24Z</dc:date>
    </item>
    <item>
      <title>Re: replace missing value with an array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/replace-missing-value-with-an-array/m-p/334265#M75496</link>
      <description>&lt;P&gt;sorry,it is the latter one ,replace the missing value by the previous non-missing value.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 20 Feb 2017 06:15:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/replace-missing-value-with-an-array/m-p/334265#M75496</guid>
      <dc:creator>JNWong</dc:creator>
      <dc:date>2017-02-20T06:15:32Z</dc:date>
    </item>
    <item>
      <title>Re: replace missing value with an array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/replace-missing-value-with-an-array/m-p/334266#M75497</link>
      <description>&lt;P&gt;thanks,it is a good method.as i need a macro to replace the 'hardcode'.&lt;/P&gt;</description>
      <pubDate>Mon, 20 Feb 2017 07:26:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/replace-missing-value-with-an-array/m-p/334266#M75497</guid>
      <dc:creator>JNWong</dc:creator>
      <dc:date>2017-02-20T07:26:25Z</dc:date>
    </item>
  </channel>
</rss>

