<?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: Looping through column in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Looping-through-column/m-p/633530#M21167</link>
    <description>&lt;P&gt;Most common observation is the mode, but if there are ties (for example two records with 123 and two records with 456) SAS will pick one arbitrarily.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=have;
    class column1;
   var columns;
    output out=mode mode=mode;
run;
data want;
    merge have mode;
    if missing(column2) then column2=mode;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Adding: I see you have marked an answer as correct, but it does not seem to compute the "most common observation". So which is it? &lt;/P&gt;</description>
    <pubDate>Fri, 20 Mar 2020 10:39:58 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2020-03-20T10:39:58Z</dc:date>
    <item>
      <title>Looping through column</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Looping-through-column/m-p/633525#M21163</link>
      <description>&lt;P&gt;Dear community,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I was hoping for a bit of help with a simple question. I have a dataset with two columns such as this:&lt;/P&gt;&lt;P&gt;Column1 Column2&lt;/P&gt;&lt;P&gt;A 123&lt;/P&gt;&lt;P&gt;A .&lt;/P&gt;&lt;P&gt;A 123&lt;/P&gt;&lt;P&gt;A 456&lt;/P&gt;&lt;P&gt;B .&lt;/P&gt;&lt;P&gt;B 789&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;All I'm looking for is to fill in the missings of Column2 with the mode by Column 1, like this:&lt;/P&gt;&lt;P&gt;Column1 Column2&lt;/P&gt;&lt;P&gt;A 123&lt;/P&gt;&lt;P&gt;A 123&lt;/P&gt;&lt;P&gt;A 123&lt;/P&gt;&lt;P&gt;A 456&lt;/P&gt;&lt;P&gt;B 789&lt;/P&gt;&lt;P&gt;B 789&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I know that it's possible with a select distinct and left join in proc sql, but since I am an R user, I'd highly appreciate a loop solution.&lt;/P&gt;&lt;P&gt;In R, one solution is this:&lt;/P&gt;&lt;P&gt;for(X in unique(Data[,"Column1"]))&lt;/P&gt;&lt;P&gt;{&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;Data[Data[,"Column1"]==X&amp;amp;is.na(Data[,"Column2"]),"Column2"] &amp;lt;- which.max(table(Data[Data[,"Column1"]==X,"Column2"]))&lt;/P&gt;&lt;P&gt;}&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Surely there's some equivalent in SAS?&lt;/P&gt;&lt;P&gt;Thanks a lot!&lt;/P&gt;</description>
      <pubDate>Fri, 20 Mar 2020 10:21:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Looping-through-column/m-p/633525#M21163</guid>
      <dc:creator>RedBishop</dc:creator>
      <dc:date>2020-03-20T10:21:54Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through column</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Looping-through-column/m-p/633526#M21164</link>
      <description>&lt;P&gt;Please explain the algorithm that replaces the missing A with 123 and the missing B with 789. What if the missing A was underneath a 123 and above a 456, what would the algorithm do in that case?&lt;/P&gt;</description>
      <pubDate>Fri, 20 Mar 2020 10:30:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Looping-through-column/m-p/633526#M21164</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-03-20T10:30:15Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through column</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Looping-through-column/m-p/633527#M21165</link>
      <description>It takes the mode (most common observation) of Column2, grouped by Column1.</description>
      <pubDate>Fri, 20 Mar 2020 10:32:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Looping-through-column/m-p/633527#M21165</guid>
      <dc:creator>RedBishop</dc:creator>
      <dc:date>2020-03-20T10:32:25Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through column</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Looping-through-column/m-p/633528#M21166</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;
input Column1 $ Column2;
datalines;
A 123
A .
A 123
A 456
B .
B 789
;

data want (drop=c);
   do _N_=1 by 1 until (last.Column1);
      set have;
      by Column1;
      c = max(c, Column2);
   end;
   do _N_=1 to _N_;
      set have;
      Column2 = c;
      output;
   end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 20 Mar 2020 10:33:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Looping-through-column/m-p/633528#M21166</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2020-03-20T10:33:13Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through column</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Looping-through-column/m-p/633530#M21167</link>
      <description>&lt;P&gt;Most common observation is the mode, but if there are ties (for example two records with 123 and two records with 456) SAS will pick one arbitrarily.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=have;
    class column1;
   var columns;
    output out=mode mode=mode;
run;
data want;
    merge have mode;
    if missing(column2) then column2=mode;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Adding: I see you have marked an answer as correct, but it does not seem to compute the "most common observation". So which is it? &lt;/P&gt;</description>
      <pubDate>Fri, 20 Mar 2020 10:39:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Looping-through-column/m-p/633530#M21167</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-03-20T10:39:58Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through column</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Looping-through-column/m-p/633532#M21168</link>
      <description>The answer of draycut uses "max" instead of "mode", but correct enough for me.</description>
      <pubDate>Fri, 20 Mar 2020 10:42:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Looping-through-column/m-p/633532#M21168</guid>
      <dc:creator>RedBishop</dc:creator>
      <dc:date>2020-03-20T10:42:22Z</dc:date>
    </item>
  </channel>
</rss>

