<?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: Filter data by multiple variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Filter-data-by-multiple-variables/m-p/931250#M366379</link>
    <description>&lt;P&gt;First, please note that neither of your data steps run because the DATA statement ends with a comma and not a semicolon.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;WITHIN Session EACH level of Grade would have a "first.grade".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You are close. You want the first session with that sort:&lt;/P&gt;
&lt;PRE&gt;data work.dsout;
    set work.dsin;
    by id session grade;
    if first.session;
run;&lt;/PRE&gt;</description>
    <pubDate>Fri, 07 Jun 2024 15:05:08 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2024-06-07T15:05:08Z</dc:date>
    <item>
      <title>Filter data by multiple variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Filter-data-by-multiple-variables/m-p/931248#M366378</link>
      <description>&lt;P&gt;Hi, we need to filter a dataset (dsin). Grouping by each ID, we need to select by session the lowest grade. In cases when there is the same session value and same grade, the session that happened last (the highest numbered month)should be chosen (see dsout).&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data dsin,
input id session month grade;
datalines;
1	2	3	4
1	3	4	1
1	4	5	1
2	1	1	4
2	3	3	1
2	3	5	3
2	4	6	3
3	1	2	1
3	1	4	1
3	2	6	1
3	3	10	4
3	4	12	2
;
run;


data dsout,
input id session month grade;
datalines;
1	2	3	4
1	3	4	1
1	4	5	1
2	1	1	4
2	3	3	1
2	4	6	3
3	1	4	1
3	2	6	1
3	3	10	4
3	4	12	2
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I tried this code but it is not filtering correctly:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;proc sort data=dsin;
    by id session grade descending month;
run;

data dsout;
    set dsin;
    by id session grade;
    if first.grade;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I am not sure what I am missing. Any advice is appreciated. Thanks!&lt;/P&gt;</description>
      <pubDate>Fri, 07 Jun 2024 14:51:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Filter-data-by-multiple-variables/m-p/931248#M366378</guid>
      <dc:creator>ANKH1</dc:creator>
      <dc:date>2024-06-07T14:51:25Z</dc:date>
    </item>
    <item>
      <title>Re: Filter data by multiple variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Filter-data-by-multiple-variables/m-p/931250#M366379</link>
      <description>&lt;P&gt;First, please note that neither of your data steps run because the DATA statement ends with a comma and not a semicolon.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;WITHIN Session EACH level of Grade would have a "first.grade".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You are close. You want the first session with that sort:&lt;/P&gt;
&lt;PRE&gt;data work.dsout;
    set work.dsin;
    by id session grade;
    if first.session;
run;&lt;/PRE&gt;</description>
      <pubDate>Fri, 07 Jun 2024 15:05:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Filter-data-by-multiple-variables/m-p/931250#M366379</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-06-07T15:05:08Z</dc:date>
    </item>
    <item>
      <title>Re: Filter data by multiple variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Filter-data-by-multiple-variables/m-p/931251#M366380</link>
      <description>&lt;P&gt;First, I don't think your sort is correct. DESCENDING is not needed here.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=dsin;
    by id session grade month;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then the first element for each session is what you need.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data dsout;
    set dsin;
    by id session;
    if first.session;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note: it is likely to be a poor choice in the real world to represent month via an integer 1 through 12. If the session crosses a year boundary, storing month without the associated year will lead to incorrect results. Try to always use valid SAS dates, which contain year, month and date ... although in this case the date is not needed and can be set to 1.&lt;/P&gt;</description>
      <pubDate>Fri, 07 Jun 2024 15:42:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Filter-data-by-multiple-variables/m-p/931251#M366380</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-06-07T15:42:17Z</dc:date>
    </item>
    <item>
      <title>Re: Filter data by multiple variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Filter-data-by-multiple-variables/m-p/931272#M366389</link>
      <description>thanks!</description>
      <pubDate>Fri, 07 Jun 2024 15:52:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Filter-data-by-multiple-variables/m-p/931272#M366389</guid>
      <dc:creator>ANKH1</dc:creator>
      <dc:date>2024-06-07T15:52:32Z</dc:date>
    </item>
  </channel>
</rss>

