<?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: Last/first statement in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Last-first-statement/m-p/670376#M201228</link>
    <description>&lt;P&gt;Thanks Sir, why do you use tables here rather than table? I try table again, it's noting different. Thanks.&lt;/P&gt;</description>
    <pubDate>Sat, 18 Jul 2020 20:07:13 GMT</pubDate>
    <dc:creator>Jianan_luna</dc:creator>
    <dc:date>2020-07-18T20:07:13Z</dc:date>
    <item>
      <title>Last/first statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Last-first-statement/m-p/670353#M201214</link>
      <description>&lt;P&gt;I am currently studying course Programming 2. In lesson (summarizing data), level 1 practice. I make sense the answer, but I am confused about question 2. When I write code as following:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=pg2.np_yearlyTraffic   
          out=sortedTraffic(keep=ParkType ParkName Location Count);
    by ParkType ParkName;
run;

proc sort data=pg2.np_yearlyTraffic   
          out=sortedTraffic(keep=ParkType ParkName Location Count);
    by ParkType ParkName;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;SAS sorted column Parktype, but when I check output data, there are only 2 type of park in ParkType column. Actually, there are 5 parktypes. I am super confused about it. Can you please help me figure it out?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 18 Jul 2020 17:44:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Last-first-statement/m-p/670353#M201214</guid>
      <dc:creator>Jianan_luna</dc:creator>
      <dc:date>2020-07-18T17:44:55Z</dc:date>
    </item>
    <item>
      <title>Re: Last/first statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Last-first-statement/m-p/670358#M201215</link>
      <description>&lt;P&gt;First, follow Maxim 2 and read the log. You will see that PROC SORT keeps all observations (since you did not use the nodup option).&lt;/P&gt;
&lt;P&gt;Next, follow Maxim 3 (Know Your Data), and inspect your source dataset. Run&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc freq data=pg2.np_yearlyTraffic;
tables parktype;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;to see the distribution of values in your variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 18 Jul 2020 18:37:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Last-first-statement/m-p/670358#M201215</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-07-18T18:37:44Z</dc:date>
    </item>
    <item>
      <title>Re: Last/first statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Last-first-statement/m-p/670362#M201217</link>
      <description>&lt;P&gt;Hi:&lt;/P&gt;
&lt;P&gt;&amp;nbsp; I would recommend trying a PROC FREQ after your PROC SORT. When I do this:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Cynthia_sas_0-1595096565374.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/47282i3EF183BBE1EA03E9/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Cynthia_sas_0-1595096565374.png" alt="Cynthia_sas_0-1595096565374.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I do see that there are 5 ParkType values in the sorted data.&lt;/P&gt;
&lt;P&gt;So then, when I run the next step for question #2:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Cynthia_sas_1-1595096663766.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/47283i249DF615CFB2768A/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Cynthia_sas_1-1595096663766.png" alt="Cynthia_sas_1-1595096663766.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;I get 478 rows and 2 columns in the output table work.TypeTraffic to answer #4.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; Next, for question #4, I do THIS:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Cynthia_sas_2-1595096988290.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/47284i5C2AAB5C07012F91/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Cynthia_sas_2-1595096988290.png" alt="Cynthia_sas_2-1595096988290.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;And then in my output (when I do a PROC PRINT), I see 5 rows, one for each value of ParkType, as shown in the PROC FREQ:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Cynthia_sas_3-1595097151218.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/47285i4BBA517955EE9C11/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Cynthia_sas_3-1595097151218.png" alt="Cynthia_sas_3-1595097151218.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;And the answer to #5 is shown in the PROC PRINT results as highlighted above.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want to see the result of using BY group processing in a data step, modify the first program to save the values of first.ParkType and Last.ParkType, as shown below:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data TypeTraffic;
    set work.sortedTraffic;
    by ParkType;
    if first.ParkType=1 then TypeCount=0;
    TypeCount+Count;
	** save the values of the first. and last. variables;
	first_by = first.ParkType;
	last_by = last.ParkType;
    format typeCount comma12.;
    keep ParkType first_by last_by ParkName Count TypeCount;
run;
     
proc print data=TypeTraffic;
  where first_by = 1 or last_by = 1;
  var ParkType first_by last_by ParkName Count TypeCount;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And then you'll see the beginning values for each park and the ending values for each park:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Cynthia_sas_4-1595098104687.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/47287iB533F3DB4234F1E7/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Cynthia_sas_4-1595098104687.png" alt="Cynthia_sas_4-1595098104687.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want to see all the First.BY and Last.BY values on every row, then run the PROC PRINT without the WHERE statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I hope this helps you understand the first.and last. processing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Cynthia&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 18 Jul 2020 18:48:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Last-first-statement/m-p/670362#M201217</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2020-07-18T18:48:58Z</dc:date>
    </item>
    <item>
      <title>Re: Last/first statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Last-first-statement/m-p/670376#M201228</link>
      <description>&lt;P&gt;Thanks Sir, why do you use tables here rather than table? I try table again, it's noting different. Thanks.&lt;/P&gt;</description>
      <pubDate>Sat, 18 Jul 2020 20:07:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Last-first-statement/m-p/670376#M201228</guid>
      <dc:creator>Jianan_luna</dc:creator>
      <dc:date>2020-07-18T20:07:13Z</dc:date>
    </item>
    <item>
      <title>Re: Last/first statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Last-first-statement/m-p/670390#M201236</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/336146"&gt;@Jianan_luna&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thanks Sir, why do you use tables here rather than table? I try table again, it's noting different. Thanks.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;In the &lt;A href="https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.4&amp;amp;docsetId=procstat&amp;amp;docsetTarget=procstat_freq_syntax08.htm&amp;amp;locale=en" target="_blank" rel="noopener"&gt;documentation&lt;/A&gt;, the statement is TABLES.&lt;/P&gt;</description>
      <pubDate>Sat, 18 Jul 2020 21:52:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Last-first-statement/m-p/670390#M201236</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-07-18T21:52:47Z</dc:date>
    </item>
  </channel>
</rss>

