<?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 SAS Data Step question in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/SAS-Data-Step-question/m-p/881245#M348195</link>
    <description>&lt;P&gt;data a;&lt;BR /&gt;input clm clmv;&lt;BR /&gt;cards;&lt;BR /&gt;0 10&lt;BR /&gt;100 10&lt;BR /&gt;200 10&lt;BR /&gt;300 10&lt;BR /&gt;600 10&lt;BR /&gt;400 20&lt;BR /&gt;500 20&lt;BR /&gt;1000 20&lt;BR /&gt;2000 20&lt;BR /&gt;3000 20&lt;BR /&gt;4000 30&lt;BR /&gt;5000 30&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;data b;&lt;BR /&gt;value = first.clmv;&lt;/P&gt;&lt;P&gt;set a;&lt;BR /&gt;by clmv;&lt;BR /&gt;value1 = first.clmv;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My question is: why is there a difference&amp;nbsp; when I write "first." before the set statement and after the set&amp;nbsp;statement? Can anyone explain that?&lt;/P&gt;</description>
    <pubDate>Sat, 17 Jun 2023 09:21:10 GMT</pubDate>
    <dc:creator>SAIKDE</dc:creator>
    <dc:date>2023-06-17T09:21:10Z</dc:date>
    <item>
      <title>SAS Data Step question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Data-Step-question/m-p/881245#M348195</link>
      <description>&lt;P&gt;data a;&lt;BR /&gt;input clm clmv;&lt;BR /&gt;cards;&lt;BR /&gt;0 10&lt;BR /&gt;100 10&lt;BR /&gt;200 10&lt;BR /&gt;300 10&lt;BR /&gt;600 10&lt;BR /&gt;400 20&lt;BR /&gt;500 20&lt;BR /&gt;1000 20&lt;BR /&gt;2000 20&lt;BR /&gt;3000 20&lt;BR /&gt;4000 30&lt;BR /&gt;5000 30&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;data b;&lt;BR /&gt;value = first.clmv;&lt;/P&gt;&lt;P&gt;set a;&lt;BR /&gt;by clmv;&lt;BR /&gt;value1 = first.clmv;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My question is: why is there a difference&amp;nbsp; when I write "first." before the set statement and after the set&amp;nbsp;statement? Can anyone explain that?&lt;/P&gt;</description>
      <pubDate>Sat, 17 Jun 2023 09:21:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Data-Step-question/m-p/881245#M348195</guid>
      <dc:creator>SAIKDE</dc:creator>
      <dc:date>2023-06-17T09:21:10Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Data Step question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Data-Step-question/m-p/881249#M348197</link>
      <description>&lt;P&gt;The correct usage is to put any statements that refer to variables AFTER the set statement. Otherwise, they are undefined (missing) when first encountered.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In your program, the VALUE1 variable is correct. The VALUE variable is looking at the PREVIOUS observation to determine the value of first.clmv, except for the first time, which sees missing values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If this is not clear, look at the following simpler example. Do you see how the VALUE variable is missing the first time and then has the value from the PREVIOUS observation? In symbols, VALUE=LAG(VALUE1).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data c;
value = clm + clmv;
set a;
by clmv;
value1 = clm + clmv;
run;

proc print;run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 17 Jun 2023 09:57:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Data-Step-question/m-p/881249#M348197</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2023-06-17T09:57:47Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Data Step question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Data-Step-question/m-p/881251#M348199</link>
      <description>&lt;P&gt;The SET statement has a double nature:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;it is declarative; it directs the data step compiler to read the datasets metadata and set up the PDV&lt;/LI&gt;
&lt;LI&gt;it is imperative, as it will perform the "read" during data step execution&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;And it is the "read" which populates dependent variables like your FIRST.&lt;/P&gt;</description>
      <pubDate>Sat, 17 Jun 2023 10:27:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Data-Step-question/m-p/881251#M348199</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-06-17T10:27:36Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Data Step question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Data-Step-question/m-p/881252#M348200</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/144848"&gt;@SAIKDE&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P data-unlink="true"&gt;The values of variable &lt;FONT face="courier new,courier"&gt;value1&lt;/FONT&gt; are explained in section "&lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lestmtsref/p0yeyftk8ftuckn1o5qzy53284gz.htm#p0z2v37ykk47i8n171lzad8fyrvg" target="_blank" rel="noopener"&gt;How SAS Identifies the Beginning and End of a BY Group&lt;/A&gt;" of the documentation of the BY statement:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P data-unlink="true"&gt;&lt;SPAN&gt;SAS sets the value of FIRST.&lt;/SPAN&gt;&lt;EM class="xisDoc-userSuppliedValue"&gt;variable&lt;/EM&gt;&lt;SPAN&gt;&amp;nbsp;to 1 when it reads the first observation in a BY group&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P data-unlink="true"&gt;&lt;SPAN&gt;and in subsection "&lt;A href="https://documentation.sas.com/doc/en/lrcon/9.4/n01a08zkzy5igbn173zjz82zsi1s.htm#n135t95kb0v5omn1wnw7ucjsx7al" target="_blank" rel="noopener"&gt;How SAS Determines FIRST.&lt;EM&gt;variable&lt;/EM&gt; and LAST.&lt;EM&gt;variable&lt;/EM&gt;&lt;/A&gt;" of the section "&lt;/SPAN&gt;FIRST. and LAST. DATA Step Variables" in the documentation "BY-Group Processing in the DATA Step":&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;UL class="xisDoc-listUnordered"&gt;
&lt;LI class="xisDoc-item"&gt;For all other observations in the BY group, the value of FIRST.&lt;EM class="xisDoc-userSuppliedValue"&gt;variable&lt;/EM&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;is 0.&lt;/LI&gt;
&lt;/UL&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P data-unlink="true"&gt;&lt;SPAN&gt;It is the SET statement which reads the observations, so only after the execution of the SET statement variable &lt;FONT face="courier new,courier"&gt;first.clmv&lt;/FONT&gt; is updated for the current observation.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P data-unlink="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P data-unlink="true"&gt;&lt;SPAN&gt;The values of variable &lt;FONT face="courier new,courier"&gt;value&lt;/FONT&gt; are explained by the facts that&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;variables &lt;FONT face="courier new,courier"&gt;first.&lt;EM&gt;variable&lt;/EM&gt;&lt;/FONT&gt;&amp;nbsp;and &lt;FONT face="courier new,courier"&gt;last.&lt;EM&gt;variable&lt;/EM&gt;&lt;/FONT&gt;&amp;nbsp;are&lt;/SPAN&gt;&lt;/P&gt;
&lt;UL&gt;
&lt;LI data-unlink="true"&gt;&lt;SPAN&gt;&lt;EM&gt;initialized&lt;/EM&gt; to 1 at the beginning of the DATA step&lt;/SPAN&gt;&lt;/LI&gt;
&lt;LI data-unlink="true"&gt;&lt;SPAN&gt;automatically &lt;EM&gt;retained&lt;/EM&gt; (i.e., not set to missing when the DATA step iterates)&lt;/SPAN&gt;&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;&lt;SPAN&gt;(which seem to be not clearly stated in the documentation linked above).&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 17 Jun 2023 10:34:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Data-Step-question/m-p/881252#M348200</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2023-06-17T10:34:31Z</dc:date>
    </item>
  </channel>
</rss>

