<?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: need first and last record in the form of sql in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/need-first-and-last-record-in-the-form-of-sql-or-datastep/m-p/946743#M370740</link>
    <description>&lt;P&gt;Since the requirement has changed then just make sure that the part that gets the minimum yearmonth value only looks at bits where the price has a value:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;proc sql;
   create table work.want as
   select a.id, a.pre_price, a.pre_year, b.post_price, b.post_year
   from (
         select id, price as Pre_price, yearmonth as Pre_year
         from (select * from have where not missing(price))
         group by id
         having yearmonth = min(yearmonth) 
        ) as a
        left join
        (
         select id, price as post_price, yearmonth as post_year
         from have
         group by id
         having yearmonth = max(yearmonth)) as b
         on a.id=b.id
   ;

quit;&lt;/PRE&gt;
&lt;P&gt;The more things related to order of values you impose the more likely that SQL answers, if possible, will become more complicated, hard to follow, and likely have much poorer performance.&lt;/P&gt;</description>
    <pubDate>Tue, 08 Oct 2024 21:24:12 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2024-10-08T21:24:12Z</dc:date>
    <item>
      <title>need first and last record in the form of sql or datastep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/need-first-and-last-record-in-the-form-of-sql-or-datastep/m-p/946551#M370675</link>
      <description>&lt;P&gt;Hello all&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a dataset like below (have) and need an output (want)&lt;/P&gt;&lt;P&gt;Please help me with the code in terms of proc sql or data step&lt;/P&gt;&lt;P&gt;TIA&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;HAVE&lt;/TD&gt;&lt;TD&gt;&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408&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;455405&lt;/TD&gt;&lt;TD&gt;2409&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Mon, 07 Oct 2024 17:21:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/need-first-and-last-record-in-the-form-of-sql-or-datastep/m-p/946551#M370675</guid>
      <dc:creator>kajal_30</dc:creator>
      <dc:date>2024-10-07T17:21:29Z</dc:date>
    </item>
    <item>
      <title>Re: need first and last record in the form of sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/need-first-and-last-record-in-the-form-of-sql-or-datastep/m-p/946553#M370676</link>
      <description>&lt;P&gt;Why do you need SQL code if the data step code works??&lt;/P&gt;</description>
      <pubDate>Mon, 07 Oct 2024 16:16:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/need-first-and-last-record-in-the-form-of-sql-or-datastep/m-p/946553#M370676</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-10-07T16:16:22Z</dc:date>
    </item>
    <item>
      <title>Re: need first and last record in the form of sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/need-first-and-last-record-in-the-form-of-sql-or-datastep/m-p/946554#M370677</link>
      <description>&lt;P&gt;its a requirement from the client as they need most of the code in sql to be run on other applications with different language than SAS for comparing results.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 07 Oct 2024 16:20:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/need-first-and-last-record-in-the-form-of-sql-or-datastep/m-p/946554#M370677</guid>
      <dc:creator>kajal_30</dc:creator>
      <dc:date>2024-10-07T16:20:33Z</dc:date>
    </item>
    <item>
      <title>Re: need first and last record in the form of sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/need-first-and-last-record-in-the-form-of-sql-or-datastep/m-p/946555#M370678</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/259983"&gt;@kajal_30&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;its a requirement from the client as they need most of the code in sql to be run on other applications with different language than SAS for comparing results.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Then look for solutions in that language.&lt;/P&gt;
&lt;P&gt;What flavor of SQL are you talking about? Each one has its own added features and quirky implementations.&amp;nbsp; If you are lucky they are using a version of SQL that is newer than what SAS uses so they can use what are called "windowing" features of SQL.&lt;/P&gt;</description>
      <pubDate>Mon, 07 Oct 2024 16:28:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/need-first-and-last-record-in-the-form-of-sql-or-datastep/m-p/946555#M370678</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-10-07T16:28:02Z</dc:date>
    </item>
    <item>
      <title>Re: need first and last record in the form of sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/need-first-and-last-record-in-the-form-of-sql-or-datastep/m-p/946556#M370679</link>
      <description>&lt;P&gt;I am talking about SAS SQL only we are not prioritizing other language our milestone is to get the SAS code working. So you are saying it is not possible in SAS to use sql to get this output ?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 07 Oct 2024 16:31:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/need-first-and-last-record-in-the-form-of-sql-or-datastep/m-p/946556#M370679</guid>
      <dc:creator>kajal_30</dc:creator>
      <dc:date>2024-10-07T16:31:22Z</dc:date>
    </item>
    <item>
      <title>Re: need first and last record in the form of sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/need-first-and-last-record-in-the-form-of-sql-or-datastep/m-p/946557#M370680</link>
      <description>&lt;P&gt;1) Since you are doing this in SAS, why not to use DATA STEP? It is _the_ tool to do such tasks.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2) SQL does not support firs. and last. by design. You would have to do it differently.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;3) I would not recommend this method for a production use, but if you really have to you could try something like this:&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID	PRICE	YEARMONTH;
cards;
1	455412	2312
1	455423	2402
1	455404	2403
1	455405	2404
2	455406	2312
2	455407	2402
2	455408	2403
2	455409	2404
2	455405	2409
2	455409	2410
3	455407	2402
3	455408	2403
3	455409	2404
3	455402	2405
3	455403	2406
3	455404	2407
3	455404	2408
3	455405	2409
;
run;

proc print;
run;

proc sql;
  select y.ID
  ,sum(case when y.n=y.min_n then y.PRICE else . end) as PRE_PRICE
  ,sum(case when y.n=y.min_n then y.YEARMONTH else . end) as PRE_YEAR
  ,sum(case when y.n=y.max_n then y.PRICE else . end) as POST_PRICE
  ,sum(case when y.n=y.max_n then y.YEARMONTH else . end) as POST_YEAR
  from
  (
    select c.* 
    , min(n) as min_n
    , max(n) as max_n
    from
    (
      select x.ID, x.PRICE, x.YEARMONTH, count(distinct ym) as n
      from
      (
        select a.*, b.YEARMONTH as ym 
        from have as a
           , have as b
        where a.ID = b.ID 
          and a.YEARMONTH &amp;gt;= b.YEARMONTH
      ) as x
      group by x.ID, x.PRICE, x.YEARMONTH
    ) as c
    group by c.ID
  ) as y
  group by y.ID
  order by y.ID
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Bart&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 07 Oct 2024 16:33:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/need-first-and-last-record-in-the-form-of-sql-or-datastep/m-p/946557#M370680</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2024-10-07T16:33:08Z</dc:date>
    </item>
    <item>
      <title>Re: need first and last record in the form of sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/need-first-and-last-record-in-the-form-of-sql-or-datastep/m-p/946569#M370683</link>
      <description>&lt;P&gt;Thank you can also please share the data step code and as my code is selecting all obs.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data want;
set have ;
by yearmonth ;
if first.yearmonth then pre_price = price and pre_year = yearmonth;
output;
if last.yearmonth then post_price = price and post_year = yearmonth ;
output;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;TIA&lt;/P&gt;&lt;P&gt;kajal&lt;/P&gt;</description>
      <pubDate>Mon, 07 Oct 2024 17:01:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/need-first-and-last-record-in-the-form-of-sql-or-datastep/m-p/946569#M370683</guid>
      <dc:creator>kajal_30</dc:creator>
      <dc:date>2024-10-07T17:01:12Z</dc:date>
    </item>
    <item>
      <title>Re: need first and last record in the form of sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/need-first-and-last-record-in-the-form-of-sql-or-datastep/m-p/946573#M370684</link>
      <description>&lt;P&gt;I thought you wanted to group by ID, not by your yearmonth variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is how to share data (rather then just posting a listing).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input ID PRICE YEARMONTH;
cards;
1 455412 2312
1 455423 2402
1 455404 2403
1 455405 2404
2 455406 2312
2 455407 2402
2 455408 2403
2 455409 2404
2 455405 2409
2 455409 2410
3 455407 2402
3 455408 2403
3 455409 2404
3 455402 2405
3 455403 2406
3 455404 2407
3 455404 2408
3 455405 2409
;

data expect ;
  input ID PRE_PRICE PRE_YEAR POST_PRICE POST_YEAR ;
cards;
1 455412 2312 455405 2404
2 455406 2312 455409 2410
3 455407 2402 455405 2409
;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So assuming the data is already sorted the data step to roll up the first/last month values per ID could look like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  by id yearmonth;
  if first.id then do;
    pre_price=price;
    pre_year=yearmonth;
  end;
  retain pre_price pre_year;
  if last.id;
  rename price=post_price yearmonth=post_year;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So let's check if we got what you expected.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc compare data=want compare=expect;
  id id;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results&lt;/P&gt;
&lt;PRE&gt;The COMPARE Procedure
Comparison of WORK.WANT with WORK.EXPECT
(Method=EXACT)

Data Set Summary

Dataset               Created          Modified  NVar    NObs

WORK.WANT    07OCT24:13:15:35  07OCT24:13:15:35     5       3
WORK.EXPECT  07OCT24:13:15:35  07OCT24:13:15:35     5       3


Variables Summary

Number of Variables in Common: 5.
Number of ID Variables: 1.


Observation Summary

Observation      Base  Compare  ID

First Obs           1        1  ID=1
Last  Obs           3        3  ID=3

Number of Observations in Common: 3.
Total Number of Observations Read from WORK.WANT: 3.
Total Number of Observations Read from WORK.EXPECT: 3.

Number of Observations with Some Compared Variables Unequal: 0.
Number of Observations with All Compared Variables Equal: 3.

NOTE: No unequal values were found. All values compared are exactly equal.
&lt;/PRE&gt;</description>
      <pubDate>Mon, 07 Oct 2024 17:18:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/need-first-and-last-record-in-the-form-of-sql-or-datastep/m-p/946573#M370684</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-10-07T17:18:35Z</dc:date>
    </item>
    <item>
      <title>Re: need first and last record in the form of sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/need-first-and-last-record-in-the-form-of-sql-or-datastep/m-p/946596#M370699</link>
      <description>&lt;P&gt;My take on an SQL solution:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;proc sql;
   create table work.want as
   select a.id, a.pre_price, a.pre_year, b.post_price, b.post_year
   from (
         select id, price as Pre_price, yearmonth as Pre_year
         from have
         group by id
         having yearmonth = min(yearmonth)
        ) as a
        left join
        (
         select id, price as post_price, yearmonth as post_year
         from have
         group by id
         having yearmonth = max(yearmonth)) as b
         on a.id=b.id
   ;

quit;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/259983"&gt;@kajal_30&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thank you can also please share the data step code and as my code is selecting all obs.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=""&gt;data want;
set have ;
by yearmonth ;
if first.yearmonth then pre_price = price and pre_year = yearmonth;
output;
if last.yearmonth then post_price = price and post_year = yearmonth ;
output;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;TIA&lt;/P&gt;
&lt;P&gt;kajal&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Doesn't consider ID at all which your desired seems to imply the ID is important.&lt;/P&gt;
&lt;P&gt;If you want the first and last observations you would have to RETAIN the PRE_ variables until you output with the last per ID.&lt;/P&gt;
&lt;P&gt;Your example data as presented actually would throw errors because the data is not sorted by yearmonth. If it were, then you would have scrambled the ID values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note that eventually bad things come those you think a number like Yearmonth is a good way to handle date information.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 07 Oct 2024 20:11:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/need-first-and-last-record-in-the-form-of-sql-or-datastep/m-p/946596#M370699</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-10-07T20:11:15Z</dc:date>
    </item>
    <item>
      <title>Re: need first and last record in the form of sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/need-first-and-last-record-in-the-form-of-sql-or-datastep/m-p/946741#M370739</link>
      <description>&lt;P&gt;Thank you so much multiple solution worked but I got one more challenge with the data as below.&lt;/P&gt;&lt;P&gt;can we please accommodate&amp;nbsp; the same&lt;/P&gt;&lt;P&gt;data have;&lt;BR /&gt;input ID PRICE YEARMONTH;&lt;BR /&gt;cards;&lt;BR /&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2312&lt;BR /&gt;1 455423 2402&lt;BR /&gt;1 455404 2403&lt;BR /&gt;1 455405 2404&lt;BR /&gt;2 455406 2312&lt;BR /&gt;2 455407 2402&lt;BR /&gt;2 455408 2403&lt;BR /&gt;2 455409 2404&lt;BR /&gt;2 455405 2409&lt;BR /&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2410&lt;BR /&gt;3 455407 2402&lt;BR /&gt;3 455408 2403&lt;BR /&gt;3 455409 2404&lt;BR /&gt;3 455402 2405&lt;BR /&gt;3 455403 2406&lt;BR /&gt;3 455404 2407&lt;BR /&gt;3 455404 2408&lt;BR /&gt;3 455405 2409&lt;BR /&gt;;&lt;BR /&gt;RUN;&lt;/P&gt;&lt;P&gt;data expect ;&lt;BR /&gt;input ID PRE_PRICE PRE_YEAR POST_PRICE POST_YEAR ;&lt;BR /&gt;cards;&lt;BR /&gt;1 &lt;STRONG&gt;455423 2402&lt;/STRONG&gt; 455405 2404&lt;BR /&gt;2 455406&amp;nbsp; 2312 .&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2410&lt;BR /&gt;3 455407 2402 455405 2409&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;while doing the first. we cannot have null value so pre_ value should go to next non null value but post_ value can have null. I am stuck as we can not simply put where price is not null as it will also eliminate null values for post&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;TIA&lt;/P&gt;&lt;P&gt;kajal&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 08 Oct 2024 21:11:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/need-first-and-last-record-in-the-form-of-sql-or-datastep/m-p/946741#M370739</guid>
      <dc:creator>kajal_30</dc:creator>
      <dc:date>2024-10-08T21:11:06Z</dc:date>
    </item>
    <item>
      <title>Re: need first and last record in the form of sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/need-first-and-last-record-in-the-form-of-sql-or-datastep/m-p/946743#M370740</link>
      <description>&lt;P&gt;Since the requirement has changed then just make sure that the part that gets the minimum yearmonth value only looks at bits where the price has a value:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;proc sql;
   create table work.want as
   select a.id, a.pre_price, a.pre_year, b.post_price, b.post_year
   from (
         select id, price as Pre_price, yearmonth as Pre_year
         from (select * from have where not missing(price))
         group by id
         having yearmonth = min(yearmonth) 
        ) as a
        left join
        (
         select id, price as post_price, yearmonth as post_year
         from have
         group by id
         having yearmonth = max(yearmonth)) as b
         on a.id=b.id
   ;

quit;&lt;/PRE&gt;
&lt;P&gt;The more things related to order of values you impose the more likely that SQL answers, if possible, will become more complicated, hard to follow, and likely have much poorer performance.&lt;/P&gt;</description>
      <pubDate>Tue, 08 Oct 2024 21:24:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/need-first-and-last-record-in-the-form-of-sql-or-datastep/m-p/946743#M370740</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-10-08T21:24:12Z</dc:date>
    </item>
    <item>
      <title>Re: need first and last record in the form of sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/need-first-and-last-record-in-the-form-of-sql-or-datastep/m-p/946745#M370741</link>
      <description>&lt;P&gt;Use a different selection criteria for when to remember the price.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  by id;
  retain pre_price pre_year ;
  if first.id then call missing(pre_price,pre_year);
  if missing(pre_price) then do;
    pre_price=price;
    pre_year=yearmonth;
  end;
  if last.id;
  rename price=post_price yearmonth=post_year;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 09 Oct 2024 00:06:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/need-first-and-last-record-in-the-form-of-sql-or-datastep/m-p/946745#M370741</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-10-09T00:06:34Z</dc:date>
    </item>
    <item>
      <title>Re: need first and last record in the form of sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/need-first-and-last-record-in-the-form-of-sql-or-datastep/m-p/946809#M370746</link>
      <description>&lt;P&gt;thank you so much&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;regards&lt;/P&gt;&lt;P&gt;kajal&lt;/P&gt;</description>
      <pubDate>Wed, 09 Oct 2024 16:26:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/need-first-and-last-record-in-the-form-of-sql-or-datastep/m-p/946809#M370746</guid>
      <dc:creator>kajal_30</dc:creator>
      <dc:date>2024-10-09T16:26:05Z</dc:date>
    </item>
  </channel>
</rss>

