<?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 solve the query using last. and first. variable concept in data step in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/solve-the-query-using-last-and-first-variable-concept-in-data/m-p/127193#M25938</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I have a data set like &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data test;&lt;/P&gt;&lt;P&gt;input name$ roll marks;&lt;/P&gt;&lt;P&gt;cards;&lt;/P&gt;&lt;P&gt;A&amp;nbsp; 1&amp;nbsp; 200&lt;/P&gt;&lt;P&gt;A&amp;nbsp; 3&amp;nbsp; 800&lt;/P&gt;&lt;P&gt;A&amp;nbsp; 5&amp;nbsp; 400&lt;/P&gt;&lt;P&gt;A&amp;nbsp; 6&amp;nbsp; 100&lt;/P&gt;&lt;P&gt;B&amp;nbsp; 7&amp;nbsp;&amp;nbsp; 300&lt;/P&gt;&lt;P&gt;B&amp;nbsp; 2&amp;nbsp; 800&lt;/P&gt;&lt;P&gt;B&amp;nbsp; 4&amp;nbsp; 700&lt;/P&gt;&lt;P&gt;B&amp;nbsp; 6&amp;nbsp; 900&lt;/P&gt;&lt;P&gt;B&amp;nbsp; 1&amp;nbsp; 100&lt;/P&gt;&lt;P&gt;C&amp;nbsp; 2&amp;nbsp; 300&lt;/P&gt;&lt;P&gt;C&amp;nbsp; 3&amp;nbsp; 100&lt;/P&gt;&lt;P&gt;C&amp;nbsp; 4&amp;nbsp; 600&lt;/P&gt;&lt;P&gt;C&amp;nbsp; 5&amp;nbsp; 800&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;RUN;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have to write query as per below condition using last. and first. variable&lt;/P&gt;&lt;P&gt;1.2nd highest mark of each student;&lt;/P&gt;&lt;P&gt;2.highest mark of each student.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Kindly help to resolve these query.&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Ashwini&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sun, 27 May 2012 03:37:37 GMT</pubDate>
    <dc:creator>Ashwini</dc:creator>
    <dc:date>2012-05-27T03:37:37Z</dc:date>
    <item>
      <title>solve the query using last. and first. variable concept in data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/solve-the-query-using-last-and-first-variable-concept-in-data/m-p/127193#M25938</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I have a data set like &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data test;&lt;/P&gt;&lt;P&gt;input name$ roll marks;&lt;/P&gt;&lt;P&gt;cards;&lt;/P&gt;&lt;P&gt;A&amp;nbsp; 1&amp;nbsp; 200&lt;/P&gt;&lt;P&gt;A&amp;nbsp; 3&amp;nbsp; 800&lt;/P&gt;&lt;P&gt;A&amp;nbsp; 5&amp;nbsp; 400&lt;/P&gt;&lt;P&gt;A&amp;nbsp; 6&amp;nbsp; 100&lt;/P&gt;&lt;P&gt;B&amp;nbsp; 7&amp;nbsp;&amp;nbsp; 300&lt;/P&gt;&lt;P&gt;B&amp;nbsp; 2&amp;nbsp; 800&lt;/P&gt;&lt;P&gt;B&amp;nbsp; 4&amp;nbsp; 700&lt;/P&gt;&lt;P&gt;B&amp;nbsp; 6&amp;nbsp; 900&lt;/P&gt;&lt;P&gt;B&amp;nbsp; 1&amp;nbsp; 100&lt;/P&gt;&lt;P&gt;C&amp;nbsp; 2&amp;nbsp; 300&lt;/P&gt;&lt;P&gt;C&amp;nbsp; 3&amp;nbsp; 100&lt;/P&gt;&lt;P&gt;C&amp;nbsp; 4&amp;nbsp; 600&lt;/P&gt;&lt;P&gt;C&amp;nbsp; 5&amp;nbsp; 800&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;RUN;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have to write query as per below condition using last. and first. variable&lt;/P&gt;&lt;P&gt;1.2nd highest mark of each student;&lt;/P&gt;&lt;P&gt;2.highest mark of each student.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Kindly help to resolve these query.&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Ashwini&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 27 May 2012 03:37:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/solve-the-query-using-last-and-first-variable-concept-in-data/m-p/127193#M25938</guid>
      <dc:creator>Ashwini</dc:creator>
      <dc:date>2012-05-27T03:37:37Z</dc:date>
    </item>
    <item>
      <title>Re: solve the query using last. and first. variable concept in data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/solve-the-query-using-last-and-first-variable-concept-in-data/m-p/127194#M25939</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Assuming you can't simply sort by name and mark descending (and keep the first two obs in each by group), you only need last.name :&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data test;&lt;BR /&gt;input name$ roll mark;&lt;BR /&gt;datalines;&lt;BR /&gt;A&amp;nbsp; 1&amp;nbsp; 200&lt;BR /&gt;A&amp;nbsp; 3&amp;nbsp; 800&lt;BR /&gt;A&amp;nbsp; 5&amp;nbsp; 400&lt;BR /&gt;A&amp;nbsp; 6&amp;nbsp; 100&lt;BR /&gt;B&amp;nbsp; 7&amp;nbsp;&amp;nbsp; 300&lt;BR /&gt;B&amp;nbsp; 2&amp;nbsp; 800&lt;BR /&gt;B&amp;nbsp; 4&amp;nbsp; 700&lt;BR /&gt;B&amp;nbsp; 6&amp;nbsp; 900&lt;BR /&gt;B&amp;nbsp; 1&amp;nbsp; 100&lt;BR /&gt;C&amp;nbsp; 2&amp;nbsp; 300&lt;BR /&gt;C&amp;nbsp; 3&amp;nbsp; 100&lt;BR /&gt;C&amp;nbsp; 4&amp;nbsp; 600&lt;BR /&gt;C&amp;nbsp; 5&amp;nbsp; 800&lt;BR /&gt;;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data want(keep=name highMark highRoll secondHighMark secondHighRoll);&lt;BR /&gt;do until(last.name);&lt;BR /&gt; set test; &lt;BR /&gt; by name;&lt;BR /&gt; if mark &amp;gt;= highMark then do;&lt;BR /&gt;&amp;nbsp; secondHighMark = highMark;&lt;BR /&gt;&amp;nbsp; secondHighRoll = highRoll;&lt;BR /&gt;&amp;nbsp; highMark = mark;&lt;BR /&gt;&amp;nbsp; highRoll = roll;&lt;BR /&gt; end;&lt;BR /&gt; else if mark &amp;gt; secondHighMark then do;&lt;BR /&gt;&amp;nbsp; secondHighMark = mark;&lt;BR /&gt;&amp;nbsp; secondHighRoll = roll;&lt;BR /&gt; end;&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc print; run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;PG&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 27 May 2012 04:48:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/solve-the-query-using-last-and-first-variable-concept-in-data/m-p/127194#M25939</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2012-05-27T04:48:07Z</dc:date>
    </item>
    <item>
      <title>Re: solve the query using last. and first. variable concept in data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/solve-the-query-using-last-and-first-variable-concept-in-data/m-p/127195#M25940</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Ashwini, If i understand correctly, you want to find out second high score and high score of each student using First.Obs and last.Obs concept. For this, I have sorted the data by name and score in ascending order (the default working of proc sort and then used it with SET statement with BY keyword. I have made use of LAG function to accomplish the task. I hope the below program will address your query and if required with little changes. But below mentioned may not be close to ansewer but yes it may help you.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data test;&lt;/P&gt;&lt;P&gt;input name$ roll mark;&lt;/P&gt;&lt;P&gt;datalines;&lt;/P&gt;&lt;P&gt;A&amp;nbsp; 1&amp;nbsp; 200&lt;/P&gt;&lt;P&gt;A&amp;nbsp; 3&amp;nbsp; 800&lt;/P&gt;&lt;P&gt;A&amp;nbsp; 5&amp;nbsp; 400&lt;/P&gt;&lt;P&gt;A&amp;nbsp; 6&amp;nbsp; 100&lt;/P&gt;&lt;P&gt;B&amp;nbsp; 7&amp;nbsp;&amp;nbsp; 300&lt;/P&gt;&lt;P&gt;B&amp;nbsp; 2&amp;nbsp; 800&lt;/P&gt;&lt;P&gt;B&amp;nbsp; 4&amp;nbsp; 700&lt;/P&gt;&lt;P&gt;B&amp;nbsp; 6&amp;nbsp; 900&lt;/P&gt;&lt;P&gt;B&amp;nbsp; 1&amp;nbsp; 100&lt;/P&gt;&lt;P&gt;C&amp;nbsp; 2&amp;nbsp; 300&lt;/P&gt;&lt;P&gt;C&amp;nbsp; 3&amp;nbsp; 100&lt;/P&gt;&lt;P&gt;C&amp;nbsp; 4&amp;nbsp; 600&lt;/P&gt;&lt;P&gt;C&amp;nbsp; 5&amp;nbsp; 800&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc sort data = test out = test;&lt;/P&gt;&lt;P&gt;by name, marks;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data _null_;&lt;/P&gt;&lt;P&gt;set test;&lt;/P&gt;&lt;P&gt;by name;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;if last.name ;&lt;/P&gt;&lt;P&gt;secondhigh = lag(score);&lt;/P&gt;&lt;P&gt;high = score;&lt;/P&gt;&lt;P&gt;out put;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc print data = test;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 27 May 2012 11:07:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/solve-the-query-using-last-and-first-variable-concept-in-data/m-p/127195#M25940</guid>
      <dc:creator>yogeshs</dc:creator>
      <dc:date>2012-05-27T11:07:24Z</dc:date>
    </item>
    <item>
      <title>Re: solve the query using last. and first. variable concept in data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/solve-the-query-using-last-and-first-variable-concept-in-data/m-p/127196#M25941</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&lt;STRONG style="color: navy; background: white; font-size: 10pt; font-family: 'Courier New';"&gt;proc&lt;/STRONG&gt; &lt;STRONG style="color: navy; background: white; font-size: 10pt; font-family: 'Courier New';"&gt;summary&lt;/STRONG&gt; &lt;SPAN style="font-size: 10pt; background: white; color: blue; font-family: 'Courier New';"&gt;nway&lt;/SPAN&gt; &lt;SPAN style="font-size: 10pt; background: white; color: blue; font-family: 'Courier New';"&gt;data&lt;/SPAN&gt;&lt;SPAN style="font-size: 10pt; background: white; color: black; font-family: 'Courier New';"&gt;=test;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; &lt;SPAN style="font-size: 10pt; background: white; color: blue; font-family: 'Courier New';"&gt;class&lt;/SPAN&gt;&lt;SPAN style="font-size: 10pt; background: white; color: black; font-family: 'Courier New';"&gt; name;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; &lt;SPAN style="font-size: 10pt; background: white; color: blue; font-family: 'Courier New';"&gt;output&lt;/SPAN&gt; &lt;SPAN style="font-size: 10pt; background: white; color: blue; font-family: 'Courier New';"&gt;out&lt;/SPAN&gt;&lt;SPAN style="font-size: 10pt; background: white; color: black; font-family: 'Courier New';"&gt;=one2(drop=_:)&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="font-size: 10pt; background: white; color: blue; font-family: 'Courier New';"&gt;idgroup&lt;/SPAN&gt;&lt;SPAN style="font-size: 10pt; background: white; color: black; font-family: 'Courier New';"&gt;(&lt;/SPAN&gt;&lt;SPAN style="font-size: 10pt; background: white; color: blue; font-family: 'Courier New';"&gt;max&lt;/SPAN&gt;&lt;SPAN style="font-size: 10pt; background: white; color: black; font-family: 'Courier New';"&gt;(marks) &lt;/SPAN&gt;&lt;SPAN style="font-size: 10pt; background: white; color: blue; font-family: 'Courier New';"&gt;out&lt;/SPAN&gt;&lt;SPAN style="font-size: 10pt; background: white; color: black; font-family: 'Courier New';"&gt;[&lt;/SPAN&gt;&lt;STRONG style="color: teal; background: white; font-size: 10pt; font-family: 'Courier New';"&gt;2&lt;/STRONG&gt;&lt;SPAN style="font-size: 10pt; background: white; color: black; font-family: 'Courier New';"&gt;](marks)=);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; &lt;STRONG style="color: navy; background: white; font-size: 10pt; font-family: 'Courier New';"&gt;run&lt;/STRONG&gt;&lt;SPAN style="font-size: 10pt; background: white; color: black; font-family: 'Courier New';"&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 27 May 2012 14:00:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/solve-the-query-using-last-and-first-variable-concept-in-data/m-p/127196#M25941</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2012-05-27T14:00:12Z</dc:date>
    </item>
    <item>
      <title>Re: solve the query using last. and first. variable concept in data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/solve-the-query-using-last-and-first-variable-concept-in-data/m-p/127197#M25942</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;If you are allowed to presort then the logic is even simpler. The case of tied high marks, as for name=D below,&amp;nbsp; is not clear. If the second highest mark must be lower than the highest then uncomment nodupkey in the code :&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data test;&lt;BR /&gt;input name$ roll mark;&lt;BR /&gt;datalines;&lt;BR /&gt;A&amp;nbsp; 1&amp;nbsp; 200&lt;BR /&gt;A&amp;nbsp; 3&amp;nbsp; 800&lt;BR /&gt;A&amp;nbsp; 5&amp;nbsp; 400&lt;BR /&gt;A&amp;nbsp; 6&amp;nbsp; 100&lt;BR /&gt;B&amp;nbsp; 7&amp;nbsp;&amp;nbsp; 300&lt;BR /&gt;B&amp;nbsp; 2&amp;nbsp; 800&lt;BR /&gt;B&amp;nbsp; 4&amp;nbsp; 700&lt;BR /&gt;B&amp;nbsp; 6&amp;nbsp; 900&lt;BR /&gt;B&amp;nbsp; 1&amp;nbsp; 100&lt;BR /&gt;C&amp;nbsp; 2&amp;nbsp; 300&lt;BR /&gt;C&amp;nbsp; 3&amp;nbsp; 100&lt;BR /&gt;C&amp;nbsp; 4&amp;nbsp; 600&lt;BR /&gt;C&amp;nbsp; 5&amp;nbsp; 800&lt;BR /&gt;D&amp;nbsp; 2&amp;nbsp; 300&lt;BR /&gt;D&amp;nbsp; 3&amp;nbsp; 100&lt;BR /&gt;D&amp;nbsp; 4&amp;nbsp; 800&lt;BR /&gt;D&amp;nbsp; 5&amp;nbsp; 800&lt;BR /&gt;;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc sort data=test /*nodupkey*/; by name descending mark; run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data want(keep=name high: second:);&lt;BR /&gt;do until(last.name);&lt;BR /&gt; set test; &lt;BR /&gt; by name;&lt;BR /&gt; if first.name then do;&lt;BR /&gt;&amp;nbsp; highMark = mark;&lt;BR /&gt;&amp;nbsp; highRoll = roll;&lt;BR /&gt; end;&lt;BR /&gt; else if missing(secondHighMark) then do;&lt;BR /&gt;&amp;nbsp; secondHighMark = mark;&lt;BR /&gt;&amp;nbsp; secondHighRoll = roll;&lt;BR /&gt; end;&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc print;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;PG&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 27 May 2012 15:46:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/solve-the-query-using-last-and-first-variable-concept-in-data/m-p/127197#M25942</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2012-05-27T15:46:56Z</dc:date>
    </item>
    <item>
      <title>Re: solve the query using last. and first. variable concept in data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/solve-the-query-using-last-and-first-variable-concept-in-data/m-p/127198#M25943</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;And just to show that the datastep is far from the only (or best) tool to do this :&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data test;&lt;BR /&gt;input name$ roll mark;&lt;BR /&gt;datalines;&lt;BR /&gt;A&amp;nbsp; 1&amp;nbsp; 200&lt;BR /&gt;A&amp;nbsp; 3&amp;nbsp; 800&lt;BR /&gt;A&amp;nbsp; 5&amp;nbsp; 400&lt;BR /&gt;A&amp;nbsp; 6&amp;nbsp; 100&lt;BR /&gt;B&amp;nbsp; 7&amp;nbsp; 300&lt;BR /&gt;B&amp;nbsp; 2&amp;nbsp; 800&lt;BR /&gt;B&amp;nbsp; 4&amp;nbsp; 700&lt;BR /&gt;B&amp;nbsp; 6&amp;nbsp; 900&lt;BR /&gt;B&amp;nbsp; 1&amp;nbsp; 100&lt;BR /&gt;C&amp;nbsp; 2&amp;nbsp; 300&lt;BR /&gt;C&amp;nbsp; 3&amp;nbsp; 100&lt;BR /&gt;C&amp;nbsp; 4&amp;nbsp; 600&lt;BR /&gt;C&amp;nbsp; 5&amp;nbsp; 800&lt;BR /&gt;D&amp;nbsp; 2&amp;nbsp; 300&lt;BR /&gt;D&amp;nbsp; 3&amp;nbsp; 100&lt;BR /&gt;D&amp;nbsp; 4&amp;nbsp; 800&lt;BR /&gt;D&amp;nbsp; 5&amp;nbsp; 800&lt;BR /&gt;;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc rank data=test descending ties=low out=rTest(where=(rank&amp;lt;3));&lt;BR /&gt;by name;&lt;BR /&gt;var mark;&lt;BR /&gt;ranks rank;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt; &lt;BR /&gt;proc print;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;PG&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 27 May 2012 19:07:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/solve-the-query-using-last-and-first-variable-concept-in-data/m-p/127198#M25943</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2012-05-27T19:07:10Z</dc:date>
    </item>
    <item>
      <title>Re: solve the query using last. and first. variable concept in data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/solve-the-query-using-last-and-first-variable-concept-in-data/m-p/127199#M25944</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Your don't post what output you need.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;data test;
input name$ roll mark;
datalines;
A&amp;nbsp; 1&amp;nbsp; 200
A&amp;nbsp; 3&amp;nbsp; 800
A&amp;nbsp; 5&amp;nbsp; 400
A&amp;nbsp; 6&amp;nbsp; 100
B&amp;nbsp; 7&amp;nbsp;&amp;nbsp; 300
B&amp;nbsp; 2&amp;nbsp; 800
B&amp;nbsp; 4&amp;nbsp; 700
B&amp;nbsp; 6&amp;nbsp; 900
B&amp;nbsp; 1&amp;nbsp; 100
C&amp;nbsp; 2&amp;nbsp; 300
C&amp;nbsp; 3&amp;nbsp; 100
C&amp;nbsp; 4&amp;nbsp; 600
C&amp;nbsp; 5&amp;nbsp; 800
D&amp;nbsp; 2&amp;nbsp; 300
D&amp;nbsp; 3&amp;nbsp; 100
D&amp;nbsp; 4&amp;nbsp; 800
D&amp;nbsp; 5&amp;nbsp; 800
;
run;
 

proc sort data=test nodupkey; by name descending mark; run;
data want;
 set test;
 by name;
 if first.name then count=0;
 count+1;
 if count in (1 2);
run;


&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Ksharp&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 28 May 2012 03:17:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/solve-the-query-using-last-and-first-variable-concept-in-data/m-p/127199#M25944</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2012-05-28T03:17:12Z</dc:date>
    </item>
    <item>
      <title>Re: solve the query using last. and first. variable concept in data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/solve-the-query-using-last-and-first-variable-concept-in-data/m-p/127200#M25945</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;hi ... prefer the data _null_ solution, but here a last.var solution ... &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;just make sure the array dimension is as large as the largest group size ...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;data top_two (keep=name max two);&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;array x(10);&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;do j=1 by 1 until(last.name);&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;&amp;nbsp;&amp;nbsp; set test;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;&amp;nbsp;&amp;nbsp; by name;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;&amp;nbsp;&amp;nbsp; x(j) = mark;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;end;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;max = max(of x(*));&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;two = largest(2,of x(*));&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;run;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 28 May 2012 03:31:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/solve-the-query-using-last-and-first-variable-concept-in-data/m-p/127200#M25945</guid>
      <dc:creator>MikeZdeb</dc:creator>
      <dc:date>2012-05-28T03:31:46Z</dc:date>
    </item>
  </channel>
</rss>

