<?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 last observation and frequency table in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/last-observation-and-frequency-table/m-p/759489#M240020</link>
    <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am attempting to take the last observation in each group of my dataset, and create a frequency table for a specific variable just using a subsetted dataset containing the last observation for each ID.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is a sample from the dataset to help with visualization:&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID AGE SMOKE&lt;/P&gt;&lt;P&gt;A 42 1&lt;/P&gt;&lt;P&gt;A 44 1&lt;/P&gt;&lt;P&gt;B 64 0&lt;/P&gt;&lt;P&gt;B 68 0&lt;/P&gt;&lt;P&gt;B 71 0&lt;/P&gt;&lt;P&gt;C 23 0&lt;/P&gt;&lt;P&gt;C 25 0&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have this code to take the last observation and another for the frequency table:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;proc sql;&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;select * from output_1&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;group by ID&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;having AGE=max(AGE);&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;quit;&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;proc freq data=output_1;&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;TABLES SMOKE;&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;quit;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My problem is the frequency table is using the whole dataset, not just the last observation. I want to pull the frequency for smoking (0 means not a smoker, 1 means yes smoker) from a dataset just using the last observation (oldest age) for ID A, B, and C. Can someone please provide guidance? Thank you!&lt;/P&gt;</description>
    <pubDate>Wed, 04 Aug 2021 20:09:52 GMT</pubDate>
    <dc:creator>keldel</dc:creator>
    <dc:date>2021-08-04T20:09:52Z</dc:date>
    <item>
      <title>last observation and frequency table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/last-observation-and-frequency-table/m-p/759489#M240020</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am attempting to take the last observation in each group of my dataset, and create a frequency table for a specific variable just using a subsetted dataset containing the last observation for each ID.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is a sample from the dataset to help with visualization:&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID AGE SMOKE&lt;/P&gt;&lt;P&gt;A 42 1&lt;/P&gt;&lt;P&gt;A 44 1&lt;/P&gt;&lt;P&gt;B 64 0&lt;/P&gt;&lt;P&gt;B 68 0&lt;/P&gt;&lt;P&gt;B 71 0&lt;/P&gt;&lt;P&gt;C 23 0&lt;/P&gt;&lt;P&gt;C 25 0&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have this code to take the last observation and another for the frequency table:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;proc sql;&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;select * from output_1&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;group by ID&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;having AGE=max(AGE);&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;quit;&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;proc freq data=output_1;&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;TABLES SMOKE;&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;quit;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My problem is the frequency table is using the whole dataset, not just the last observation. I want to pull the frequency for smoking (0 means not a smoker, 1 means yes smoker) from a dataset just using the last observation (oldest age) for ID A, B, and C. Can someone please provide guidance? Thank you!&lt;/P&gt;</description>
      <pubDate>Wed, 04 Aug 2021 20:09:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/last-observation-and-frequency-table/m-p/759489#M240020</guid>
      <dc:creator>keldel</dc:creator>
      <dc:date>2021-08-04T20:09:52Z</dc:date>
    </item>
    <item>
      <title>Re: last observation and frequency table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/last-observation-and-frequency-table/m-p/759494#M240022</link>
      <description>&lt;P&gt;Have you tried a Data step using LAST.ID?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Like the below.&amp;nbsp; The data must be in the correct order for NOTSORTED to work properly.&amp;nbsp; If the data is not already in the correct order, you would need to use Proc Sort and remove the NOTSORTED.&amp;nbsp; See results at bottom of post.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA	Have;
	INFILE	DATALINES;
	INPUT
	ID	$ AGE SMOKE;
DATALINES;
A 42 1
A 44 1
B 64 0
B 68 0
B 71 0
C 23 0
C 25 0
;
RUN;

DATA	Want;
	SET	Have;
		BY	ID	NOTSORTED;
	IF	LAST.ID;
RUN;

PROC	FREQ	DATA=Want;
	TABLES	Smoke;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Jim&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="jimbarbour_0-1628108445773.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/62268i607C9105E049FFFF/image-size/large?v=v2&amp;amp;px=999" role="button" title="jimbarbour_0-1628108445773.png" alt="jimbarbour_0-1628108445773.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 04 Aug 2021 20:21:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/last-observation-and-frequency-table/m-p/759494#M240022</guid>
      <dc:creator>jimbarbour</dc:creator>
      <dc:date>2021-08-04T20:21:07Z</dc:date>
    </item>
    <item>
      <title>Re: last observation and frequency table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/last-observation-and-frequency-table/m-p/759500#M240023</link>
      <description>&lt;P&gt;Wonderful! That worked. I hadn't been using the LAST. correctly when I tried that before.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for your time and guidance.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 04 Aug 2021 20:26:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/last-observation-and-frequency-table/m-p/759500#M240023</guid>
      <dc:creator>keldel</dc:creator>
      <dc:date>2021-08-04T20:26:35Z</dc:date>
    </item>
    <item>
      <title>Re: last observation and frequency table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/last-observation-and-frequency-table/m-p/759502#M240025</link>
      <description>&lt;P&gt;You're welcome.&amp;nbsp; I'm glad it was what you needed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Jim&lt;/P&gt;</description>
      <pubDate>Wed, 04 Aug 2021 20:28:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/last-observation-and-frequency-table/m-p/759502#M240025</guid>
      <dc:creator>jimbarbour</dc:creator>
      <dc:date>2021-08-04T20:28:05Z</dc:date>
    </item>
  </channel>
</rss>

