<?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: Multiple columns and loop and create ranks in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Multiple-columns-and-loop-and-create-ranks/m-p/694323#M211739</link>
    <description>&lt;P&gt;It shouldn't be repeating day&amp;amp;i., but you may be seeing the&amp;nbsp;&lt;STRONG&gt;day&amp;nbsp;&lt;/STRONG&gt;column's format only showing four characters (i.e. so "Day10" is &lt;EM&gt;displayed&lt;/EM&gt; as "Day1").&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Try adding the following format statement to the&amp;nbsp;&lt;STRONG&gt;top_3&amp;nbsp;&lt;/STRONG&gt;data step, as shown below:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data top_3;
    &lt;STRONG&gt;format day $8.;&lt;/STRONG&gt;
    /* . . . */&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Adding that works for me when I update the input data to have 10+ days.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 26 Oct 2020 18:41:31 GMT</pubDate>
    <dc:creator>mklangley</dc:creator>
    <dc:date>2020-10-26T18:41:31Z</dc:date>
    <item>
      <title>Multiple columns and loop and create ranks</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Multiple-columns-and-loop-and-create-ranks/m-p/694275#M211723</link>
      <description>&lt;P&gt;I have the following data:&lt;/P&gt;
&lt;P&gt;Name Day1 Day2 Day3 Day4&lt;/P&gt;
&lt;P&gt;Kim&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&amp;nbsp; &amp;nbsp; 3&amp;nbsp; &amp;nbsp;9&amp;nbsp; &amp;nbsp;0&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Anna&amp;nbsp; &amp;nbsp; &amp;nbsp; 0&amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp;2&amp;nbsp; &amp;nbsp;9&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Joe&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 5&amp;nbsp; &amp;nbsp; 5&amp;nbsp; &amp;nbsp;9&amp;nbsp; &amp;nbsp;0&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bob&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&amp;nbsp; &amp;nbsp; 0&amp;nbsp; &amp;nbsp;9&amp;nbsp; &amp;nbsp;0&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Enward&amp;nbsp; 2&amp;nbsp; &amp;nbsp; 6&amp;nbsp; &amp;nbsp;5&amp;nbsp; &amp;nbsp;0&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would like to get the top 3 ranks for each day and summation of remaining data and formatted as a long data as below:&lt;/P&gt;
&lt;P&gt;Name Day Counts&lt;/P&gt;
&lt;P&gt;Joe Day1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;5&lt;/P&gt;
&lt;P&gt;Bob Day1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&lt;/P&gt;
&lt;P&gt;Edward Day1 2&amp;nbsp; (when tie, the take the first name--Edward is before Kim)&lt;/P&gt;
&lt;P&gt;Others Day1&amp;nbsp; 2&lt;/P&gt;
&lt;P&gt;Edward Day2 6&lt;/P&gt;
&lt;P&gt;Joe Day2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;5&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Kim Day2&amp;nbsp; &amp;nbsp; &amp;nbsp; 3&lt;BR /&gt;Others Day2 1&lt;/P&gt;
&lt;P&gt;Bob Day3&amp;nbsp; &amp;nbsp; &amp;nbsp;9&lt;/P&gt;
&lt;P&gt;Joe Day3&amp;nbsp; &amp;nbsp; &amp;nbsp; 9&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Kim Day3&amp;nbsp; &amp;nbsp; &amp;nbsp; 9&lt;/P&gt;
&lt;P&gt;Others Day3&amp;nbsp; 7&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Anna Day4&amp;nbsp; &amp;nbsp; &amp;nbsp;9&lt;/P&gt;
&lt;P&gt;Bob Day4&amp;nbsp; &amp;nbsp; &amp;nbsp; 0&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Joe Day4&amp;nbsp; &amp;nbsp; &amp;nbsp; 0&lt;/P&gt;
&lt;P&gt;Others Day4&amp;nbsp; 0&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Actual data have many columns. Thank you.&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>Mon, 26 Oct 2020 15:46:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Multiple-columns-and-loop-and-create-ranks/m-p/694275#M211723</guid>
      <dc:creator>Emma8</dc:creator>
      <dc:date>2020-10-26T15:46:04Z</dc:date>
    </item>
    <item>
      <title>Re: Multiple columns and loop and create ranks</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Multiple-columns-and-loop-and-create-ranks/m-p/694298#M211731</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/294547"&gt;@Emma8&lt;/a&gt;&amp;nbsp; &amp;nbsp;Is this what you're looking for?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;  
    input name $ day1-day4;
    datalines;
Kim 2 3 9 0
Anna 0 1 2 9
Joe 5 5 9 0
Bob 2 0 9 0
Edward 2 6 5 0
    ;
run;

%macro loop(days=);
    %do i = 1 %to &amp;amp;days.;
        proc sql;
            create table summarize as
            select Name, "Day&amp;amp;i." as day, sum(day&amp;amp;i.) as total
            from have
            group by name, day
            order by calculated total desc, name
            ;
        quit;

        data top_3;
            set summarize end=eof;
            retain sum 0;
            if _n_ gt 3
                then do;
                    sum + total;
                    if eof then do;
                        name = 'Others';
                        count = sum;
                        output;
                    end;
                end;
            else do;
                count = total;
                output;
            end;
            drop total sum;
        run;

        proc append base=want
            data=top_3
            force;
        run;
    %end;
%mend;

%loop(days=4);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note: Per your example, I am assuming your columns are named Day1, Day2, etc.&amp;nbsp; The parameter of the macro call can be modified if you have more columns (e.g. 7, if you have Day1-Day7).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="mklangley_0-1603730732215.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/51064i8E0E5BDD70E1EEC4/image-size/medium?v=v2&amp;amp;px=400" role="button" title="mklangley_0-1603730732215.png" alt="mklangley_0-1603730732215.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 26 Oct 2020 16:45:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Multiple-columns-and-loop-and-create-ranks/m-p/694298#M211731</guid>
      <dc:creator>mklangley</dc:creator>
      <dc:date>2020-10-26T16:45:40Z</dc:date>
    </item>
    <item>
      <title>Re: Multiple columns and loop and create ranks</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Multiple-columns-and-loop-and-create-ranks/m-p/694320#M211738</link>
      <description>HI-the actual data has ay1 to day50, so I changed days=4 to 50, and the day.i repeat after 9th day. Could you fix that, please? Thank you</description>
      <pubDate>Mon, 26 Oct 2020 18:31:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Multiple-columns-and-loop-and-create-ranks/m-p/694320#M211738</guid>
      <dc:creator>Emma8</dc:creator>
      <dc:date>2020-10-26T18:31:15Z</dc:date>
    </item>
    <item>
      <title>Re: Multiple columns and loop and create ranks</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Multiple-columns-and-loop-and-create-ranks/m-p/694323#M211739</link>
      <description>&lt;P&gt;It shouldn't be repeating day&amp;amp;i., but you may be seeing the&amp;nbsp;&lt;STRONG&gt;day&amp;nbsp;&lt;/STRONG&gt;column's format only showing four characters (i.e. so "Day10" is &lt;EM&gt;displayed&lt;/EM&gt; as "Day1").&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Try adding the following format statement to the&amp;nbsp;&lt;STRONG&gt;top_3&amp;nbsp;&lt;/STRONG&gt;data step, as shown below:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data top_3;
    &lt;STRONG&gt;format day $8.;&lt;/STRONG&gt;
    /* . . . */&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Adding that works for me when I update the input data to have 10+ days.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 26 Oct 2020 18:41:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Multiple-columns-and-loop-and-create-ranks/m-p/694323#M211739</guid>
      <dc:creator>mklangley</dc:creator>
      <dc:date>2020-10-26T18:41:31Z</dc:date>
    </item>
  </channel>
</rss>

