<?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: Single Mining View in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Single-Mining-View/m-p/287059#M59000</link>
    <description>&lt;PRE&gt;
So you want the max five value for each ID ?



data have; 
infile cards expandtabs truncover;
input id	var1	var2;
datalines;
8250	1	23987
8250	51	4733
8250	45	13841
8250	71	4109
8250	24	10031
8250	54	8849
8250	88	8284
8250	79	13223
14984	10	96
14984	55	195
14984	26	166
14984	84	141
14984	40	127
14984	83	184
14984	17	122
14984	80	1695
14984	96	120
;
run;
proc sort data=have;
 by id descending var2;
run;
data temp;
 set have;
 by id;
 if first.id then n=0;
 n+1;
 if n le 5;
run;
data want;
 length var1_2 $ 2000;
 do until(last.id);
  set temp;
  by id;
  var1_2=catx(' ',var1_2,var1);
 end; 
 keep id var1_2;
run;


&lt;/PRE&gt;</description>
    <pubDate>Tue, 26 Jul 2016 01:47:10 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2016-07-26T01:47:10Z</dc:date>
    <item>
      <title>Single Mining View</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Single-Mining-View/m-p/286886#M58938</link>
      <description>&lt;P&gt;Hi All,&lt;BR /&gt;&lt;BR /&gt;I am wondering if its possible to solve the following problem in SAS:&lt;BR /&gt;&lt;BR /&gt;I have a dataset as follows -&lt;/P&gt;
&lt;TABLE style="border-collapse: collapse; width: 144pt;" border="0" cellpadding="0" cellspacing="0" width="192"&gt;&lt;COLGROUP&gt;&lt;COL style="width: 48pt;" span="3" width="64" /&gt; &lt;/COLGROUP&gt;
&lt;TBODY&gt;
&lt;TR style="height: 15.75pt;"&gt;
&lt;TD class="xl65" style="height: 15.75pt; width: 48pt;" height="21" width="64"&gt;id&lt;/TD&gt;
&lt;TD class="xl65" style="border-left: none; width: 48pt;" width="64"&gt;var1&lt;/TD&gt;
&lt;TD class="xl65" style="border-left: none; width: 48pt;" width="64"&gt;var2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.75pt;"&gt;
&lt;TD class="xl66" style="height: 15.75pt; border-top: none; width: 48pt;" height="21" width="64"&gt;8250&lt;/TD&gt;
&lt;TD class="xl66" style="border-top: none; border-left: none; width: 48pt;" width="64"&gt;1&lt;/TD&gt;
&lt;TD class="xl66" style="border-top: none; border-left: none; width: 48pt;" width="64"&gt;23987&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.75pt;"&gt;
&lt;TD class="xl66" style="height: 15.75pt; border-top: none; width: 48pt;" height="21" width="64"&gt;8250&lt;/TD&gt;
&lt;TD class="xl66" style="border-top: none; border-left: none; width: 48pt;" width="64"&gt;45&lt;/TD&gt;
&lt;TD class="xl66" style="border-top: none; border-left: none; width: 48pt;" width="64"&gt;13841&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.75pt;"&gt;
&lt;TD class="xl66" style="height: 15.75pt; border-top: none; width: 48pt;" height="21" width="64"&gt;8250&lt;/TD&gt;
&lt;TD class="xl66" style="border-top: none; border-left: none; width: 48pt;" width="64"&gt;79&lt;/TD&gt;
&lt;TD class="xl66" style="border-top: none; border-left: none; width: 48pt;" width="64"&gt;13223&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.75pt;"&gt;
&lt;TD class="xl66" style="height: 15.75pt; border-top: none; width: 48pt;" height="21" width="64"&gt;8250&lt;/TD&gt;
&lt;TD class="xl66" style="border-top: none; border-left: none; width: 48pt;" width="64"&gt;24&lt;/TD&gt;
&lt;TD class="xl66" style="border-top: none; border-left: none; width: 48pt;" width="64"&gt;10031&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.75pt;"&gt;
&lt;TD class="xl66" style="height: 15.75pt; border-top: none; width: 48pt;" height="21" width="64"&gt;8250&lt;/TD&gt;
&lt;TD class="xl66" style="border-top: none; border-left: none; width: 48pt;" width="64"&gt;54&lt;/TD&gt;
&lt;TD class="xl66" style="border-top: none; border-left: none; width: 48pt;" width="64"&gt;8849&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.75pt;"&gt;
&lt;TD class="xl66" style="height: 15.75pt; border-top: none; width: 48pt;" height="21" width="64"&gt;8250&lt;/TD&gt;
&lt;TD class="xl66" style="border-top: none; border-left: none; width: 48pt;" width="64"&gt;88&lt;/TD&gt;
&lt;TD class="xl66" style="border-top: none; border-left: none; width: 48pt;" width="64"&gt;8284&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.75pt;"&gt;
&lt;TD class="xl66" style="height: 15.75pt; border-top: none; width: 48pt;" height="21" width="64"&gt;8250&lt;/TD&gt;
&lt;TD class="xl66" style="border-top: none; border-left: none; width: 48pt;" width="64"&gt;51&lt;/TD&gt;
&lt;TD class="xl66" style="border-top: none; border-left: none; width: 48pt;" width="64"&gt;4733&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.75pt;"&gt;
&lt;TD class="xl66" style="height: 15.75pt; border-top: none; width: 48pt;" height="21" width="64"&gt;8250&lt;/TD&gt;
&lt;TD class="xl66" style="border-top: none; border-left: none; width: 48pt;" width="64"&gt;71&lt;/TD&gt;
&lt;TD class="xl66" style="border-top: none; border-left: none; width: 48pt;" width="64"&gt;4109&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.75pt;"&gt;
&lt;TD class="xl67" style="height: 15.75pt; border-top: none; width: 48pt;" height="21" width="64"&gt;14984&lt;/TD&gt;
&lt;TD class="xl67" style="border-top: none; border-left: none; width: 48pt;" width="64"&gt;80&lt;/TD&gt;
&lt;TD class="xl67" style="border-top: none; border-left: none; width: 48pt;" width="64"&gt;1695&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.75pt;"&gt;
&lt;TD class="xl67" style="height: 15.75pt; border-top: none; width: 48pt;" height="21" width="64"&gt;14984&lt;/TD&gt;
&lt;TD class="xl67" style="border-top: none; border-left: none; width: 48pt;" width="64"&gt;55&lt;/TD&gt;
&lt;TD class="xl67" style="border-top: none; border-left: none; width: 48pt;" width="64"&gt;195&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.75pt;"&gt;
&lt;TD class="xl67" style="height: 15.75pt; border-top: none; width: 48pt;" height="21" width="64"&gt;14984&lt;/TD&gt;
&lt;TD class="xl67" style="border-top: none; border-left: none; width: 48pt;" width="64"&gt;83&lt;/TD&gt;
&lt;TD class="xl67" style="border-top: none; border-left: none; width: 48pt;" width="64"&gt;184&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.75pt;"&gt;
&lt;TD class="xl67" style="height: 15.75pt; border-top: none; width: 48pt;" height="21" width="64"&gt;14984&lt;/TD&gt;
&lt;TD class="xl67" style="border-top: none; border-left: none; width: 48pt;" width="64"&gt;26&lt;/TD&gt;
&lt;TD class="xl67" style="border-top: none; border-left: none; width: 48pt;" width="64"&gt;166&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.75pt;"&gt;
&lt;TD class="xl67" style="height: 15.75pt; border-top: none; width: 48pt;" height="21" width="64"&gt;14984&lt;/TD&gt;
&lt;TD class="xl67" style="border-top: none; border-left: none; width: 48pt;" width="64"&gt;84&lt;/TD&gt;
&lt;TD class="xl67" style="border-top: none; border-left: none; width: 48pt;" width="64"&gt;141&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.75pt;"&gt;
&lt;TD class="xl67" style="height: 15.75pt; border-top: none; width: 48pt;" height="21" width="64"&gt;14984&lt;/TD&gt;
&lt;TD class="xl67" style="border-top: none; border-left: none; width: 48pt;" width="64"&gt;40&lt;/TD&gt;
&lt;TD class="xl67" style="border-top: none; border-left: none; width: 48pt;" width="64"&gt;127&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.75pt;"&gt;
&lt;TD class="xl67" style="height: 15.75pt; border-top: none; width: 48pt;" height="21" width="64"&gt;14984&lt;/TD&gt;
&lt;TD class="xl67" style="border-top: none; border-left: none; width: 48pt;" width="64"&gt;17&lt;/TD&gt;
&lt;TD class="xl67" style="border-top: none; border-left: none; width: 48pt;" width="64"&gt;122&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.75pt;"&gt;
&lt;TD class="xl67" style="height: 15.75pt; border-top: none; width: 48pt;" height="21" width="64"&gt;14984&lt;/TD&gt;
&lt;TD class="xl67" style="border-top: none; border-left: none; width: 48pt;" width="64"&gt;96&lt;/TD&gt;
&lt;TD class="xl67" style="border-top: none; border-left: none; width: 48pt;" width="64"&gt;120&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.75pt;"&gt;
&lt;TD class="xl67" style="height: 15.75pt; border-top: none; width: 48pt;" height="21" width="64"&gt;14984&lt;/TD&gt;
&lt;TD class="xl67" style="border-top: none; border-left: none; width: 48pt;" width="64"&gt;10&lt;/TD&gt;
&lt;TD class="xl67" style="border-top: none; border-left: none; width: 48pt;" width="64"&gt;96&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;I just would like to have a unique record per column id which takes the max value from var 2 and gives -&lt;/P&gt;
&lt;TABLE width="187"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="64"&gt;id&lt;/TD&gt;
&lt;TD width="123"&gt;var1_2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="64"&gt;8250&lt;/TD&gt;
&lt;TD width="123"&gt;1 45 79 24 54&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="64"&gt;14984&lt;/TD&gt;
&lt;TD width="123"&gt;80 55 83 26 84&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is thsi possible in Base SAS?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;KR&lt;/P&gt;
&lt;P&gt;SK&lt;/P&gt;</description>
      <pubDate>Mon, 25 Jul 2016 15:01:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Single-Mining-View/m-p/286886#M58938</guid>
      <dc:creator>Siddharth123</dc:creator>
      <dc:date>2016-07-25T15:01:26Z</dc:date>
    </item>
    <item>
      <title>Re: Single Mining View</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Single-Mining-View/m-p/286933#M58954</link>
      <description>&lt;P&gt;[1] Your input data seems to be sorted both ID and VAR2. Here is a solution which requires data to be sorted by ID alone.&lt;/P&gt;&lt;P&gt;[2] You seem to want the first 5 largest values of Var2, although it is not mentioned. The present solution can handle as many values as you want.&lt;/P&gt;&lt;P&gt;[3] An array approch is used. It requires the largest ID-Group size which is 9 in your data. Again this size can be done programmatically. Proc Sql may be used but I use a data step which will be faster than SQL when data set is large.&lt;/P&gt;&lt;P&gt;[4] Your input data set is slightly disordered by Var2 within ID.&lt;/P&gt;&lt;P&gt;Here is the program:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have; 
input id	var1	var2;
datalines;
8250	1	23987
8250	51	4733
8250	45	13841
8250	71	4109
8250	24	10031
8250	54	8849
8250	88	8284
8250	79	13223
14984	10	96
14984	55	195
14984	26	166
14984	84	141
14984	40	127
14984	83	184
14984	17	122
14984	80	1695
14984	96	120

;
run;

data _null_;
   retain max  0;
   do i = 1 by 1 until(last.id);
      set have end = eof;
      by id;
      
   end;
   if i &amp;gt; max then max = i;
   if eof then call symputx('groupsize', max);
run;


%let h = 5;

data want;
   array k[&amp;amp;groupsize] _temporary_;
   array m[&amp;amp;groupsize] _temporary_;
   length var1_2 $50;
   do i = 1 by 1 until(last.id);
      set have;
      by id;
      k[i] = var2;
      m[i] = var1;
      if first.id then call missing(var1_2);
   end;
   call catx(' ', var1_2, id);
   do i = 1 to &amp;amp;h;
      b = largest(i, of k[*]);
      v = m[whichn(b, of k[*])];
      call catx(' ', var1_2, v);
   end;
keep var1_2;   
run;
 &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I hope you will not have any problem to understand the program and this will be an acceptable solution to you.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Edited . . . . .&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In the above program ID goes as part of new variable, VAR1_2. If you don't want it, delete&lt;/P&gt;&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;call &lt;SPAN class="token function"&gt;catx&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token string"&gt;' '&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt; var1_2&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt; &lt;SPAN class="token keyword"&gt;id&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;and change the last statement to&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token keyword"&gt;keep&lt;/SPAN&gt; ID var1_2&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If you don't want a general program but just a simple program for the given data(where ID and VAR2 are sorted), few statements will do as in:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want1;
   do i = 1 by 1 until(last.id);
      set have;
      by id;
   length var1_2 $50;
   if i &amp;lt; 6 then call catx(' ', var1_2, var1);
   else continue;
   end;
keep id var1_2;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 25 Jul 2016 18:35:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Single-Mining-View/m-p/286933#M58954</guid>
      <dc:creator>KachiM</dc:creator>
      <dc:date>2016-07-25T18:35:37Z</dc:date>
    </item>
    <item>
      <title>Re: Single Mining View</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Single-Mining-View/m-p/287059#M59000</link>
      <description>&lt;PRE&gt;
So you want the max five value for each ID ?



data have; 
infile cards expandtabs truncover;
input id	var1	var2;
datalines;
8250	1	23987
8250	51	4733
8250	45	13841
8250	71	4109
8250	24	10031
8250	54	8849
8250	88	8284
8250	79	13223
14984	10	96
14984	55	195
14984	26	166
14984	84	141
14984	40	127
14984	83	184
14984	17	122
14984	80	1695
14984	96	120
;
run;
proc sort data=have;
 by id descending var2;
run;
data temp;
 set have;
 by id;
 if first.id then n=0;
 n+1;
 if n le 5;
run;
data want;
 length var1_2 $ 2000;
 do until(last.id);
  set temp;
  by id;
  var1_2=catx(' ',var1_2,var1);
 end; 
 keep id var1_2;
run;


&lt;/PRE&gt;</description>
      <pubDate>Tue, 26 Jul 2016 01:47:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Single-Mining-View/m-p/287059#M59000</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-07-26T01:47:10Z</dc:date>
    </item>
    <item>
      <title>Re: Single Mining View</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Single-Mining-View/m-p/287141#M59030</link>
      <description>&lt;P&gt;You seemed to have not gone through my answers. Just see the end of my answer.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 26 Jul 2016 10:54:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Single-Mining-View/m-p/287141#M59030</guid>
      <dc:creator>KachiM</dc:creator>
      <dc:date>2016-07-26T10:54:53Z</dc:date>
    </item>
  </channel>
</rss>

