<?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 convert all missing observations to zero by group in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/convert-all-missing-observations-to-zero-by-group/m-p/485051#M125981</link>
    <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; Hello SAS users!&lt;/P&gt;&lt;P&gt;&amp;nbsp; Though this may be an easy task&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;to&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;those who have expertise&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;with&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;programming, I am struggling with this.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;So I got data 'have' below.&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;data have;&lt;BR /&gt;input id permno N x1 x2 x3 x4 x5;&lt;BR /&gt;cards;&lt;BR /&gt;1 1 5 4 3 2 2 1&lt;BR /&gt;1 2 5 . 1 1 3 2&lt;BR /&gt;1 3 5 9 . 3 9 6&lt;BR /&gt;1 4 5 8 . 7 3 2&lt;BR /&gt;1 5 5 8 2 . 6&lt;BR /&gt;1 6 5 . 9 6 3 9&lt;BR /&gt;2 1 3 . 13 8 . .&lt;BR /&gt;2 2 3 23 5 8 . .&lt;BR /&gt;2 3 3 11 . 5 . .&lt;BR /&gt;2 4 3 13 7 9 . .&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And I want to convert 'have' to 'want' like below.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data want;&lt;BR /&gt;input id permno N x1 x2 x3 x4 x5;&lt;BR /&gt;cards;&lt;BR /&gt;1 1 5 4 3 2 2 1&lt;BR /&gt;1 2 5 0 1 1 3 2&lt;BR /&gt;1 3 5 9 0 3 9 6&lt;BR /&gt;1 4 5 8 0 7 3 2&lt;BR /&gt;1 5 5 8 2 0 6&lt;BR /&gt;1 6 5 0 9 6 3 9&lt;BR /&gt;2 1 3 0 13 8 . .&lt;BR /&gt;2 2 3 23 5 8 . .&lt;BR /&gt;2 3 3 11 0 5 . .&lt;BR /&gt;2 4 3 13 7 9 . .&lt;BR /&gt;;&lt;BR /&gt;run;&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;So when you look at data set 'have',&lt;/P&gt;&lt;P&gt;id=1 have 5 not-all-missing numeric variables x1, x2, x3, x4, x5&lt;/P&gt;&lt;P&gt;and id=2 have only 3 not-all-missing numeric variables x1, x2, x3.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;That means id=1 have 5 variables and id=2 have 3 variables.&lt;/P&gt;&lt;P&gt;For id=1, I want to convert missing values in x1, x2, x3, x4, x5 to zero&amp;nbsp;&lt;/P&gt;&lt;P&gt;and For id=2, I want to convert missing values in x1, x2, x3 to zero but remains x4, x5 all missing.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I got thousands of id, so the array of variable x could be more than 100 (x1~x105)&lt;/P&gt;&lt;P&gt;How do I perform this EFFICIENTLY?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help will be appreciated.&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>Wed, 08 Aug 2018 09:00:06 GMT</pubDate>
    <dc:creator>jkim197</dc:creator>
    <dc:date>2018-08-08T09:00:06Z</dc:date>
    <item>
      <title>convert all missing observations to zero by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/convert-all-missing-observations-to-zero-by-group/m-p/485051#M125981</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; Hello SAS users!&lt;/P&gt;&lt;P&gt;&amp;nbsp; Though this may be an easy task&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;to&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;those who have expertise&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;with&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;programming, I am struggling with this.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;So I got data 'have' below.&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;data have;&lt;BR /&gt;input id permno N x1 x2 x3 x4 x5;&lt;BR /&gt;cards;&lt;BR /&gt;1 1 5 4 3 2 2 1&lt;BR /&gt;1 2 5 . 1 1 3 2&lt;BR /&gt;1 3 5 9 . 3 9 6&lt;BR /&gt;1 4 5 8 . 7 3 2&lt;BR /&gt;1 5 5 8 2 . 6&lt;BR /&gt;1 6 5 . 9 6 3 9&lt;BR /&gt;2 1 3 . 13 8 . .&lt;BR /&gt;2 2 3 23 5 8 . .&lt;BR /&gt;2 3 3 11 . 5 . .&lt;BR /&gt;2 4 3 13 7 9 . .&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And I want to convert 'have' to 'want' like below.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data want;&lt;BR /&gt;input id permno N x1 x2 x3 x4 x5;&lt;BR /&gt;cards;&lt;BR /&gt;1 1 5 4 3 2 2 1&lt;BR /&gt;1 2 5 0 1 1 3 2&lt;BR /&gt;1 3 5 9 0 3 9 6&lt;BR /&gt;1 4 5 8 0 7 3 2&lt;BR /&gt;1 5 5 8 2 0 6&lt;BR /&gt;1 6 5 0 9 6 3 9&lt;BR /&gt;2 1 3 0 13 8 . .&lt;BR /&gt;2 2 3 23 5 8 . .&lt;BR /&gt;2 3 3 11 0 5 . .&lt;BR /&gt;2 4 3 13 7 9 . .&lt;BR /&gt;;&lt;BR /&gt;run;&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;So when you look at data set 'have',&lt;/P&gt;&lt;P&gt;id=1 have 5 not-all-missing numeric variables x1, x2, x3, x4, x5&lt;/P&gt;&lt;P&gt;and id=2 have only 3 not-all-missing numeric variables x1, x2, x3.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;That means id=1 have 5 variables and id=2 have 3 variables.&lt;/P&gt;&lt;P&gt;For id=1, I want to convert missing values in x1, x2, x3, x4, x5 to zero&amp;nbsp;&lt;/P&gt;&lt;P&gt;and For id=2, I want to convert missing values in x1, x2, x3 to zero but remains x4, x5 all missing.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I got thousands of id, so the array of variable x could be more than 100 (x1~x105)&lt;/P&gt;&lt;P&gt;How do I perform this EFFICIENTLY?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help will be appreciated.&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>Wed, 08 Aug 2018 09:00:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/convert-all-missing-observations-to-zero-by-group/m-p/485051#M125981</guid>
      <dc:creator>jkim197</dc:creator>
      <dc:date>2018-08-08T09:00:06Z</dc:date>
    </item>
    <item>
      <title>Re: convert all missing observations to zero by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/convert-all-missing-observations-to-zero-by-group/m-p/485053#M125983</link>
      <description>&lt;P&gt;Something like:&lt;/P&gt;
&lt;PRE&gt;data have;&lt;BR /&gt; input id permno N x1 x2 x3 x4 x5;&lt;BR /&gt;cards;&lt;BR /&gt;1 1 5 4 3 2 2 1&lt;BR /&gt;1 2 5 . 1 1 3 2&lt;BR /&gt;1 3 5 9 . 3 9 6&lt;BR /&gt;1 4 5 8 . 7 3 2&lt;BR /&gt;1 5 5 8 2 . 6&lt;BR /&gt;1 6 5 . 9 6 3 9&lt;BR /&gt;2 1 3 . 13 8 . .&lt;BR /&gt;2 2 3 23 5 8 . .&lt;BR /&gt;2 3 3 11 . 5 . .&lt;BR /&gt;2 4 3 13 7 9 . .&lt;BR /&gt;;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;data want;&lt;BR /&gt; set have;&lt;BR /&gt; array val x:;&lt;BR /&gt; do over val;&lt;BR /&gt; if val=. then val=0;&lt;BR /&gt; end;&lt;BR /&gt;run; &lt;/PRE&gt;
&lt;P&gt;Would actually change the value.&amp;nbsp; However you might not need to.&amp;nbsp; If you just want missings to be displayed as zero then you could use:&lt;/P&gt;
&lt;PRE&gt;options missing="0";&lt;/PRE&gt;
&lt;P&gt;Note this afffects the whole session.&lt;/P&gt;
&lt;P&gt;You might want to be careful doing that anyways, 0 is not missing, therefore you are imputing data - this can affect calculations such as proc means.&lt;/P&gt;</description>
      <pubDate>Wed, 08 Aug 2018 09:06:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/convert-all-missing-observations-to-zero-by-group/m-p/485053#M125983</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-08-08T09:06:56Z</dc:date>
    </item>
    <item>
      <title>Re: convert all missing observations to zero by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/convert-all-missing-observations-to-zero-by-group/m-p/485068#M125992</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Dear RW9&lt;/P&gt;&lt;P&gt;What I want to do is to remain missing values of id=2's x4, x5&amp;nbsp;&lt;/P&gt;&lt;P&gt;not to change all missing values in the dataset to zero&lt;/P&gt;</description>
      <pubDate>Wed, 08 Aug 2018 10:17:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/convert-all-missing-observations-to-zero-by-group/m-p/485068#M125992</guid>
      <dc:creator>jkim197</dc:creator>
      <dc:date>2018-08-08T10:17:30Z</dc:date>
    </item>
    <item>
      <title>Re: convert all missing observations to zero by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/convert-all-missing-observations-to-zero-by-group/m-p/485070#M125993</link>
      <description>&lt;P&gt;Ah, ok then a couple of if statements are need:&lt;/P&gt;
&lt;PRE&gt;data have;
 input id permno N x1 x2 x3 x4 x5;
cards;
1 1 5 4 3 2 2 1
1 2 5 . 1 1 3 2
1 3 5 9 . 3 9 6
1 4 5 8 . 7 3 2
1 5 5 8 2 . 6
1 6 5 . 9 6 3 9
2 1 3 . 13 8 . .
2 2 3 23 5 8 . .
2 3 3 11 . 5 . .
2 4 3 13 7 9 . .
;
run;

data want (drop=flag i);
 set have;
 array x(5);
 flag=0;
 do i=1 to 5;
   if flag=0 and x{i}=. then do;
     x{i}=0;
     flag=1;
   end;
   else if flag=1 and x{i}=. then x{i}=0;
   else if flag=1 and x{i} ne . then leave;
 end;
run; &lt;/PRE&gt;</description>
      <pubDate>Wed, 08 Aug 2018 10:26:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/convert-all-missing-observations-to-zero-by-group/m-p/485070#M125993</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-08-08T10:26:35Z</dc:date>
    </item>
    <item>
      <title>Re: convert all missing observations to zero by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/convert-all-missing-observations-to-zero-by-group/m-p/485071#M125994</link>
      <description>Thanks for your help&lt;BR /&gt;But I still find zeros in x4 and x5 in id=2, permno=2, 4&lt;BR /&gt;Could you give me an advice regarding your code?</description>
      <pubDate>Wed, 08 Aug 2018 10:31:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/convert-all-missing-observations-to-zero-by-group/m-p/485071#M125994</guid>
      <dc:creator>jkim197</dc:creator>
      <dc:date>2018-08-08T10:31:46Z</dc:date>
    </item>
    <item>
      <title>Re: convert all missing observations to zero by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/convert-all-missing-observations-to-zero-by-group/m-p/485074#M125996</link>
      <description>&lt;P&gt;Ok, sorry, didn't see that.&amp;nbsp; Just work from the back to start:&lt;/P&gt;
&lt;PRE&gt;data have;
 input id permno N x1 x2 x3 x4 x5;
cards;
1 1 5 4 3 2 2 1
1 2 5 . 1 1 3 2
1 3 5 9 . 3 9 6
1 4 5 8 . 7 3 2
1 5 5 8 2 . 6
1 6 5 . 9 6 3 9
2 1 3 . 13 8 . .
2 2 3 23 5 8 . .
2 3 3 11 . 5 . .
2 4 3 13 7 9 . .
;
run;

data want (drop=flag i);
 set have;
 array x(5);
 flag=0;
 do i=5 to 1 by -1;
   if x{i} ne . then flag=1;
   if flag=1 and x{i}=. then x{i}=0;
 end;
run; &lt;/PRE&gt;</description>
      <pubDate>Wed, 08 Aug 2018 11:02:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/convert-all-missing-observations-to-zero-by-group/m-p/485074#M125996</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-08-08T11:02:17Z</dc:date>
    </item>
    <item>
      <title>Re: convert all missing observations to zero by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/convert-all-missing-observations-to-zero-by-group/m-p/485083#M125997</link>
      <description>&lt;P&gt;There is definitely a more efficient way, but here is a transpose/transpose back approach&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id permno N x1 x2 x3 x4 x5;
cards;
1 1 5 4 3 2 2 1
1 2 5 . 1 1 3 2
1 3 5 9 . 3 9 6
1 4 5 8 . 7 3 2
1 5 5 8 2 . 6
1 6 5 . 9 6 3 9
2 1 3 . 13 8 . .
2 2 3 23 5 8 . .
2 3 3 11 . 5 . .
2 4 3 13 7 9 . .
;
run;

proc transpose data=have out=temp1;
   by id permno N;
run;

proc sql;
   create table temp2 as
   select *
         ,case when count(col1)=0 then 0 else 1 end as flag
   from temp1
   group by id, _NAME_;
quit;

data temp3;
   set temp2;
   if col1=. &amp;amp; flag=1 then col1=0;
   drop flag;
run;

proc sort data=temp3;
   by id permno;
run;

proc transpose data=temp3 out=want(drop=_NAME_);
   by id permno;
   id _NAME_;
   var col1;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 08 Aug 2018 11:06:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/convert-all-missing-observations-to-zero-by-group/m-p/485083#M125997</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2018-08-08T11:06:46Z</dc:date>
    </item>
    <item>
      <title>Re: convert all missing observations to zero by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/convert-all-missing-observations-to-zero-by-group/m-p/485086#M125999</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/45151"&gt;@RW9&lt;/a&gt;, I politely disagree that this would work in the general case. What if the data looks like this?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
 input id permno N x1 x2 x3 x4 x5 x6;
cards;
1 1 5 4 3 2 2 1 1
1 2 5 . 1 1 3 2 2
1 3 5 9 . 3 9 6 3
1 4 5 8 . 7 3 2 4
1 5 5 8 2 . 6 5
1 6 5 . 9 6 3 9 6
2 1 3 . 13 8 . . 1
2 2 3 23 5 8 . . 2
2 3 3 11 . 5 . . 3
2 4 3 13 7 9 . . 4
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then your code would convert the missing values for ID=2 in the variables x4 and x5 even though they are all missing in the by group and should remain so.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That is of course, if I understand your needs correctly&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/145253"&gt;@jkim197&lt;/a&gt;? &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 08 Aug 2018 11:16:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/convert-all-missing-observations-to-zero-by-group/m-p/485086#M125999</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2018-08-08T11:16:58Z</dc:date>
    </item>
    <item>
      <title>Re: convert all missing observations to zero by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/convert-all-missing-observations-to-zero-by-group/m-p/485107#M126003</link>
      <description>&lt;P&gt;I don't disagree that taking different data, or different logic then my code would not work.&amp;nbsp; TBH its so hard to tell what the OP is trying to do...&lt;/P&gt;
&lt;P&gt;First step would be to ascertain why there is a transposed format in the first place, and why there are missings in a given set of data, which would most likely be simpler to fix earlier in the process.&amp;nbsp; However we have only what is posted here.&lt;/P&gt;</description>
      <pubDate>Wed, 08 Aug 2018 12:31:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/convert-all-missing-observations-to-zero-by-group/m-p/485107#M126003</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-08-08T12:31:58Z</dc:date>
    </item>
    <item>
      <title>Re: convert all missing observations to zero by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/convert-all-missing-observations-to-zero-by-group/m-p/485129#M126008</link>
      <description>&lt;P&gt;If you have many variable to impute , Make a macro .&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id permno N x1 x2 x3 x4 x5;
cards;
1 1 5 4 3 2 2 1
1 2 5 . 1 1 3 2
1 3 5 9 . 3 9 6
1 4 5 8 . 7 3 2
1 5 5 8 2 . 6 .
1 6 5 . 9 6 3 9
2 1 3 . 13 8 . .
2 2 3 23 5 8 . .
2 3 3 11 . 5 . .
2 4 3 13 7 9 . .
;
run;
proc sql;
create table temp as
 select *,n(x1)=0 as _x1,n(x2)=0 as _x2,n(x3)=0 as _x3,n(x4)=0 as _x4,n(x5)=0 as _x5
  from have
   group by id
    order by id,permno;
quit;
data want;
 set temp;
 array x{*} x1-x5;
 array _x{*} _x1-_x5;
 do i=1 to dim(x);
  if missing(x{i}) and _x{i}=0 then x{i}=0;
 end;
 drop i _:;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 08 Aug 2018 12:50:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/convert-all-missing-observations-to-zero-by-group/m-p/485129#M126008</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2018-08-08T12:50:21Z</dc:date>
    </item>
    <item>
      <title>Re: convert all missing observations to zero by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/convert-all-missing-observations-to-zero-by-group/m-p/485184#M126026</link>
      <description>&lt;P&gt;HI&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/145253"&gt;@jkim197&lt;/a&gt;&amp;nbsp;just out of curiosity and if you have time, does the variable N in your HAVE denote the number of variables in starting/ascending sequence&amp;nbsp; in the array group X1-Xn series to check and impute? as N is 5 and 3 for ID1 and ID2 respectively?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If that is the case the following should do? nevertheless ,I am wary when somebody brilliant like Ksharp didn't offer this,that makes me think I am the one whose understanding is wrong. Can you clarify plz?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
by id;
array t(*) x:;
do _n_=1 to n;
if missing(t(_n_)) then t(_n_)=0;
end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 08 Aug 2018 15:14:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/convert-all-missing-observations-to-zero-by-group/m-p/485184#M126026</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-08-08T15:14:01Z</dc:date>
    </item>
    <item>
      <title>Re: convert all missing observations to zero by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/convert-all-missing-observations-to-zero-by-group/m-p/485231#M126039</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/145253"&gt;@jkim197&lt;/a&gt;:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;First, please do not include irrelevant variables (such as Permno and N, in this case) in your sample input/output: They muddy up the picture and distract from the task at hand. Your sample input had better look like this:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;      
  input id x1-x5 
  cards ;        
1   4  3  2  2  1
1   .  1  1  3  2
1   9  .  3  9  6
1   8  .  7  3  2
1   8  2  .  6   
1   .  9  6  3  9
2   . 13  8  .  .
2  23  5  8  .  .
2  11  .  5  .  .
2  13  7  9  .  .
;                
run ;            &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Second, you want an &lt;EM&gt;efficient&lt;/EM&gt; solution. That means, to begin with, that you want to minimize the number of passes through the input data. In this case, you need at minimum 2 passes since you cannot do what you need to any BY group without pre-processing it beforehand. But any task involving the logic (1) read a BY group to discover something and (2) read it again to process it based on the discovery is a typical work for what is known as the double DoW-loop, whose extra advantage lies in processing the whole shebang in a single step.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hence, below: (1) every BY group is read first to determine the maximum number of non-missing values in any row _NX, then (2) this very BY group is scanned again to set the nulls to zeros in each row only for the first _NX leftmost X-variables.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop = _:) ; &lt;BR /&gt;  do _ng = 1 by 1 until (last.id) ; &lt;BR /&gt;    set have ; &lt;BR /&gt;    by ID ; &lt;BR /&gt;    _nx = _nx &amp;lt;&amp;gt; N (of x:) ; &lt;BR /&gt;  end ; &lt;BR /&gt;  do _ng = 1 to _ng ; &lt;BR /&gt;    set have ; &lt;BR /&gt;    array x [*] x: ; &lt;BR /&gt;    do _i = 1 to _nx ; &lt;BR /&gt;      if nmiss (x[_i]) then x[_i] = 0 ;&lt;BR /&gt;    end ; &lt;BR /&gt;    output ; &lt;BR /&gt;  end ; &lt;BR /&gt;run ; &lt;BR /&gt;                            &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Even if you have 105 X-variables, scanning through the array is a pretty quick affair. However, if you want to avoid that in favor of using a potentially faster string search/replace function, you can do as below. Note that it is &lt;EM&gt;not&lt;/EM&gt; to suggest that you code this way: APP tricks like this are reserved to extreme circumstances where performance gains are more than well worth the extra complexity; I just want to indicate that the possibility is there (plus perhaps it may pique someone's curiosity to learn more about the APP functions).&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop = _:) ;                                             
  do _ng = 1 by 1 until (last.id) ;                                 
    set have ;                                                      
    by ID ;                                                         
    _nx = _nx &amp;lt;&amp;gt; N (of x:) ;                                        
  end ;                                                             
  retain _a _m _z ;                                                 
  if _n_ = 1 then do ;                                              
    _a  = addrlong (x1) ;                                           
    _m  = put (. , rb8.) ;                                          
    _z  = put (0 , rb8.) ;                                          
  end ;                                                             
  _nx = 8 * _nx ;                                                   
  do _ng = 1 to _ng ;                                               
    set have ;                                                      
    call pokelong (tranwrd (peekclong (_a, _nx), _m, _z), _a, _nx) ;
    output ;                                                        
  end ;                                                             
run ;                                                               &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;FWIW&lt;/P&gt;&lt;P&gt;Paul D.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 08 Aug 2018 17:51:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/convert-all-missing-observations-to-zero-by-group/m-p/485231#M126039</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2018-08-08T17:51:12Z</dc:date>
    </item>
    <item>
      <title>Re: convert all missing observations to zero by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/convert-all-missing-observations-to-zero-by-group/m-p/485430#M126125</link>
      <description>&lt;P&gt;Yes you are right.&lt;/P&gt;&lt;P&gt;your code works great and fast.. Thank you&lt;/P&gt;</description>
      <pubDate>Thu, 09 Aug 2018 11:48:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/convert-all-missing-observations-to-zero-by-group/m-p/485430#M126125</guid>
      <dc:creator>jkim197</dc:creator>
      <dc:date>2018-08-09T11:48:35Z</dc:date>
    </item>
    <item>
      <title>Re: convert all missing observations to zero by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/convert-all-missing-observations-to-zero-by-group/m-p/485475#M126141</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/145253"&gt;@jkim197&lt;/a&gt;&amp;nbsp; That made me chuckle.&amp;nbsp; Like&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/21262"&gt;@hashman&lt;/a&gt;&amp;nbsp;pointed out, the description of the question explaining the contents of the dataset matters most in the first place for which a robust solution is required. I am glad, my peek into your dataset and the guesses or in other words intuition turned out to be correct.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Last but not the least, irony in lack of clarity gave us a gift to receive the privilege of yet another beautiful APP class of PD that I copied to my notes &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 09 Aug 2018 13:53:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/convert-all-missing-observations-to-zero-by-group/m-p/485475#M126141</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-08-09T13:53:09Z</dc:date>
    </item>
    <item>
      <title>Re: convert all missing observations to zero by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/convert-all-missing-observations-to-zero-by-group/m-p/485712#M126232</link>
      <description>&lt;P&gt;I propose this solution:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id permno N x1 x2 x3 x4 x5;
cards;
1 1 5 4 3 2 2 1
1 2 5 . 1 1 3 2
1 3 5 9 . 3 9 6
1 4 5 8 . 7 3 2
1 5 5 8 2 . 6
1 6 5 . 9 6 . 9
2 1 3 . 13 8 . .
2 2 3 23 5 8 . .
2 3 3 11 . 5 . .
2 4 3 13 7 9 . 10
;
run;

data want;
set have;
array X(*) x:;
do i=1 to dim(x);
   if x(i)=. then do;
   rc=i;
   do j=rc to dim(x);
   if x(j) ne . then do;
   x(i)=0;
   leave;
 
   end;  
  end;
 end;
end;
run; &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 10 Aug 2018 11:03:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/convert-all-missing-observations-to-zero-by-group/m-p/485712#M126232</guid>
      <dc:creator>mansour_ib_sas</dc:creator>
      <dc:date>2018-08-10T11:03:03Z</dc:date>
    </item>
  </channel>
</rss>

