<?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: How to retain records grouped by multiple records in SAS data step? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-retain-records-grouped-by-multiple-records-in-SAS-data/m-p/928224#M365201</link>
    <description>&lt;P&gt;Just having some fun for posting a PROC SQL solution:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
input id $ x y;
cards;
a1 1 2
a1 1 4
a1 2 8
a1 2 16
b1 2 2
b1 2 4
b1 2 8
b1 3 16
b1 4 32
;
run;
proc sql;
create table want as 
select * from test group by id having x=min(x);
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 14 May 2024 02:56:55 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2024-05-14T02:56:55Z</dc:date>
    <item>
      <title>How to retain records grouped by multiple records in SAS data step?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-retain-records-grouped-by-multiple-records-in-SAS-data/m-p/928173#M365186</link>
      <description>&lt;P&gt;Using the following dataset for illustration:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
input id $ x y;
cards;
a1 1 2
a1 1 4
a1 2 8
a1 2 16
b1 2 2
b1 2 4
b1 2 8
b1 3 16
b1 4 32
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I'd like to retain all records sharing the lowest values of x by subject id:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;id x y
a1 1 2
a1 1 4
b1 2 2
b1 2 4
b1 2 8&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;There's a simple solution I'm missing -- tried the following code but it didn't work:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test2;
set test;
by id x;
if first.x;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 13 May 2024 17:10:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-retain-records-grouped-by-multiple-records-in-SAS-data/m-p/928173#M365186</guid>
      <dc:creator>RobertWF1</dc:creator>
      <dc:date>2024-05-13T17:10:02Z</dc:date>
    </item>
    <item>
      <title>Re: How to retain records grouped by multiple records in SAS data step?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-retain-records-grouped-by-multiple-records-in-SAS-data/m-p/928175#M365187</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=test nway;
    class id;
    var x;
    output out=stats min=min_x;
run;
data want;
    merge test stats(drop=_:);
    by id;
    if x=min_x;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 13 May 2024 17:18:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-retain-records-grouped-by-multiple-records-in-SAS-data/m-p/928175#M365187</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-05-13T17:18:10Z</dc:date>
    </item>
    <item>
      <title>Re: How to retain records grouped by multiple records in SAS data step?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-retain-records-grouped-by-multiple-records-in-SAS-data/m-p/928179#M365189</link>
      <description>&lt;P&gt;Since your data are sorted by ID and X, when you read the first record for an ID-group, you know it will have the minimum value for x.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test2;
  set test;
  by id x;
  retain _xmin ;
  if first.id then _xmin=x;
  if x=_xmin ;
  *drop _xmin ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Of course if you have missing values for X, this won't work because they will be the minimum value.&lt;/P&gt;</description>
      <pubDate>Mon, 13 May 2024 17:41:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-retain-records-grouped-by-multiple-records-in-SAS-data/m-p/928179#M365189</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2024-05-13T17:41:11Z</dc:date>
    </item>
    <item>
      <title>Re: How to retain records grouped by multiple records in SAS data step?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-retain-records-grouped-by-multiple-records-in-SAS-data/m-p/928180#M365190</link>
      <description>&lt;P&gt;Thanks, that worked!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So it's not a matter of tweaking the syntax in my original attempt - looks like we have to find a work around or else use proc sql?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 13 May 2024 17:50:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-retain-records-grouped-by-multiple-records-in-SAS-data/m-p/928180#M365190</guid>
      <dc:creator>RobertWF1</dc:creator>
      <dc:date>2024-05-13T17:50:28Z</dc:date>
    </item>
    <item>
      <title>Re: How to retain records grouped by multiple records in SAS data step?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-retain-records-grouped-by-multiple-records-in-SAS-data/m-p/928181#M365191</link>
      <description>&lt;P&gt;Thanks Quentin!&lt;/P&gt;</description>
      <pubDate>Mon, 13 May 2024 17:52:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-retain-records-grouped-by-multiple-records-in-SAS-data/m-p/928181#M365191</guid>
      <dc:creator>RobertWF1</dc:creator>
      <dc:date>2024-05-13T17:52:10Z</dc:date>
    </item>
    <item>
      <title>Re: How to retain records grouped by multiple records in SAS data step?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-retain-records-grouped-by-multiple-records-in-SAS-data/m-p/928182#M365192</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/19879"&gt;@Quentin&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Since your data are sorted by ID and X, when you read the first record for an ID-group, you know it will have the minimum value for x.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/408179"&gt;@RobertWF1&lt;/a&gt;&amp;nbsp;is that always the case that the data will be sorted like you show?&lt;/P&gt;</description>
      <pubDate>Mon, 13 May 2024 18:05:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-retain-records-grouped-by-multiple-records-in-SAS-data/m-p/928182#M365192</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-05-13T18:05:14Z</dc:date>
    </item>
    <item>
      <title>Re: How to retain records grouped by multiple records in SAS data step?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-retain-records-grouped-by-multiple-records-in-SAS-data/m-p/928193#M365197</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/19879"&gt;@Quentin&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Since your data are sorted by ID and X, when you read the first record for an ID-group, you know it will have the minimum value for x.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/408179"&gt;@RobertWF1&lt;/a&gt;&amp;nbsp;is that always the case that the data will be sorted like you show?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;If it isn't sorted, the&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; by id x;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;statement will throw an error.&amp;nbsp; I often include "extra" variables on the BY statement like this, just to confirm that the data are sorted as the logic expects.&amp;nbsp; There is an efficiency cost to this, because the DATA step will create first.x and last.x which are not needed, but the efficiency loss is worth it to me to know that the data are sorted, and know I will get and error rather than a wrong result if the data are not sorted.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 13 May 2024 18:57:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-retain-records-grouped-by-multiple-records-in-SAS-data/m-p/928193#M365197</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2024-05-13T18:57:50Z</dc:date>
    </item>
    <item>
      <title>Re: How to retain records grouped by multiple records in SAS data step?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-retain-records-grouped-by-multiple-records-in-SAS-data/m-p/928224#M365201</link>
      <description>&lt;P&gt;Just having some fun for posting a PROC SQL solution:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
input id $ x y;
cards;
a1 1 2
a1 1 4
a1 2 8
a1 2 16
b1 2 2
b1 2 4
b1 2 8
b1 3 16
b1 4 32
;
run;
proc sql;
create table want as 
select * from test group by id having x=min(x);
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 14 May 2024 02:56:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-retain-records-grouped-by-multiple-records-in-SAS-data/m-p/928224#M365201</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2024-05-14T02:56:55Z</dc:date>
    </item>
  </channel>
</rss>

