<?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: first.variable not working in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/first-variable-not-working/m-p/665173#M198840</link>
    <description>&lt;P&gt;The code you posted gives seems to work, and makes sense. (Try running what I have below.) Perhaps double-check that you ran the same thing. If you're still seeing a different result, please include the code that produced the output you were seeing.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data example1;
    input site $;
    datalines;
    100
    100
    100
    200
    200
    300
    300
    300
    300
    ;
run;

data example;
     set example1;
     retain _obs 0;
     by site;
     if first.site then _obs = 0;
     _obs + 1;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 25 Jun 2020 21:39:45 GMT</pubDate>
    <dc:creator>mklangley</dc:creator>
    <dc:date>2020-06-25T21:39:45Z</dc:date>
    <item>
      <title>first.variable not working</title>
      <link>https://communities.sas.com/t5/SAS-Programming/first-variable-not-working/m-p/665172#M198839</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data example;
      set example1;
     retain _obs 0;
     by site;
     if first.site then _obs = 0;
     _obs + 1;
run;

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;According to the code above, I am supposed to get an output dataset something like this:&lt;/P&gt;&lt;P&gt;site &amp;nbsp;_obs&lt;/P&gt;&lt;P&gt;100 &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;&lt;P&gt;100. &amp;nbsp; &amp;nbsp;2&lt;/P&gt;&lt;P&gt;100. &amp;nbsp; &amp;nbsp;3&lt;/P&gt;&lt;P&gt;200. &amp;nbsp; &amp;nbsp;1&lt;/P&gt;&lt;P&gt;200. &amp;nbsp; &amp;nbsp;2&lt;/P&gt;&lt;P&gt;300. &amp;nbsp; &amp;nbsp;1&lt;/P&gt;&lt;P&gt;300. &amp;nbsp; &amp;nbsp;2&lt;/P&gt;&lt;P&gt;300. &amp;nbsp; &amp;nbsp;3&lt;/P&gt;&lt;P&gt;300. &amp;nbsp; &amp;nbsp;4&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But instead, I got an output without a proper order:&lt;/P&gt;&lt;P&gt;site &amp;nbsp;_obs&lt;/P&gt;&lt;P&gt;100 &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;&lt;P&gt;100. &amp;nbsp; &amp;nbsp;2&lt;/P&gt;&lt;P&gt;100. &amp;nbsp; &amp;nbsp;3&lt;/P&gt;&lt;P&gt;200. &amp;nbsp; &amp;nbsp;1&lt;/P&gt;&lt;P&gt;200. &amp;nbsp; &amp;nbsp;2&lt;/P&gt;&lt;P&gt;300. &amp;nbsp; &amp;nbsp;3&lt;/P&gt;&lt;P&gt;300. &amp;nbsp; &amp;nbsp;4&lt;/P&gt;&lt;P&gt;300. &amp;nbsp; &amp;nbsp;5&lt;/P&gt;&lt;P&gt;300. &amp;nbsp; &amp;nbsp;6&lt;/P&gt;&lt;P&gt;Supposedly, the variable _obs should reset to zero(0) when the first.site encounters, but it did not. I am not sure what's the problem with this. Is this a syntax error or a flaw in SAS?&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would really appreciate your help.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 25 Jun 2020 21:31:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/first-variable-not-working/m-p/665172#M198839</guid>
      <dc:creator>pdkc494949</dc:creator>
      <dc:date>2020-06-25T21:31:08Z</dc:date>
    </item>
    <item>
      <title>Re: first.variable not working</title>
      <link>https://communities.sas.com/t5/SAS-Programming/first-variable-not-working/m-p/665173#M198840</link>
      <description>&lt;P&gt;The code you posted gives seems to work, and makes sense. (Try running what I have below.) Perhaps double-check that you ran the same thing. If you're still seeing a different result, please include the code that produced the output you were seeing.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data example1;
    input site $;
    datalines;
    100
    100
    100
    200
    200
    300
    300
    300
    300
    ;
run;

data example;
     set example1;
     retain _obs 0;
     by site;
     if first.site then _obs = 0;
     _obs + 1;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 25 Jun 2020 21:39:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/first-variable-not-working/m-p/665173#M198840</guid>
      <dc:creator>mklangley</dc:creator>
      <dc:date>2020-06-25T21:39:45Z</dc:date>
    </item>
    <item>
      <title>Re: first.variable not working</title>
      <link>https://communities.sas.com/t5/SAS-Programming/first-variable-not-working/m-p/665174#M198841</link>
      <description>Thank you for your reply. My actual input data is quite long and complicated. The data I wrote in the description is a simplified version of it. I'll get back to you if I can copy my actual data in here.</description>
      <pubDate>Thu, 25 Jun 2020 21:42:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/first-variable-not-working/m-p/665174#M198841</guid>
      <dc:creator>pdkc494949</dc:creator>
      <dc:date>2020-06-25T21:42:42Z</dc:date>
    </item>
    <item>
      <title>Re: first.variable not working</title>
      <link>https://communities.sas.com/t5/SAS-Programming/first-variable-not-working/m-p/665177#M198842</link>
      <description>&lt;P&gt;I'm sorry. I forgot adding one statement in the data step.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm not sure if it would play an important role in distorting my output.&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if vistdat le &amp;amp;cutdate&lt;/CODE&gt;;&lt;/PRE&gt;</description>
      <pubDate>Thu, 25 Jun 2020 21:52:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/first-variable-not-working/m-p/665177#M198842</guid>
      <dc:creator>pdkc494949</dc:creator>
      <dc:date>2020-06-25T21:52:47Z</dc:date>
    </item>
    <item>
      <title>Re: first.variable not working</title>
      <link>https://communities.sas.com/t5/SAS-Programming/first-variable-not-working/m-p/665179#M198843</link>
      <description>&lt;P&gt;Is there any chance that your example1 data set already has a variable named _obs in it?&lt;/P&gt;
&lt;P&gt;When the variable is in the source data set retain does not do quite what you expect because the value from the next record is read into the variable in the source data.&lt;/P&gt;
&lt;P&gt;Consider:&lt;/P&gt;
&lt;PRE&gt;data example1;
    input site $ _obs;
    datalines;
    100  1
    100  1
    100  1
    200  1
    200  1
    300  1
    300  1
    300  1
    300  1
    ;
run;

data example;
     set example1;
     retain _obs 0 newobs 0;
     by site;
     if first.site then do;
         _obs = 0;
         newobs=0;
      end;
     _obs + 1;
     newobs+1;
run;&lt;/PRE&gt;
&lt;P&gt;_obs and newobs behave quite differently.&lt;/P&gt;</description>
      <pubDate>Thu, 25 Jun 2020 21:56:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/first-variable-not-working/m-p/665179#M198843</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-06-25T21:56:13Z</dc:date>
    </item>
    <item>
      <title>Re: first.variable not working</title>
      <link>https://communities.sas.com/t5/SAS-Programming/first-variable-not-working/m-p/665181#M198844</link>
      <description>my source data does not have _obs or newobs in it. Thanks</description>
      <pubDate>Thu, 25 Jun 2020 21:59:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/first-variable-not-working/m-p/665181#M198844</guid>
      <dc:creator>pdkc494949</dc:creator>
      <dc:date>2020-06-25T21:59:10Z</dc:date>
    </item>
    <item>
      <title>Re: first.variable not working</title>
      <link>https://communities.sas.com/t5/SAS-Programming/first-variable-not-working/m-p/665183#M198846</link>
      <description>Hmm. Hard to say without seeing your code or data. Could you include those?</description>
      <pubDate>Thu, 25 Jun 2020 22:03:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/first-variable-not-working/m-p/665183#M198846</guid>
      <dc:creator>mklangley</dc:creator>
      <dc:date>2020-06-25T22:03:40Z</dc:date>
    </item>
    <item>
      <title>Re: first.variable not working</title>
      <link>https://communities.sas.com/t5/SAS-Programming/first-variable-not-working/m-p/665185#M198847</link>
      <description>&lt;P&gt;I actually resolved the issue by removing the if statement which subsets the dataset by date.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Anyways, thank you for your help! &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 25 Jun 2020 22:25:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/first-variable-not-working/m-p/665185#M198847</guid>
      <dc:creator>pdkc494949</dc:creator>
      <dc:date>2020-06-25T22:25:36Z</dc:date>
    </item>
    <item>
      <title>Re: first.variable not working</title>
      <link>https://communities.sas.com/t5/SAS-Programming/first-variable-not-working/m-p/665201#M198859</link>
      <description>&lt;P&gt;Based on a few of your posts put together, here's what happened and why removing the IF statement worked.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The program accurately created the BY variables.&amp;nbsp; However, the order of the statements made a difference because IF deleted observations.&amp;nbsp; More specifically, it deleted observations before getting to the logic about resetting _OBS.&amp;nbsp; So that part never executed on observations that got deleted.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you don't have a WHERE statement in your DATA step already, that would be the simple solution.&amp;nbsp; Change this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if vistdat le &amp;amp;cutdate;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;to this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;where vistdat le &amp;amp;cutdate;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The WHERE statement subsets differently than IF.&amp;nbsp; When using IF, the DATA step reads in observations then deletes some of them. When using WHERE instead, the DATA step reads in only the observations that meet the WHERE condition.&amp;nbsp; As a result, the BY variables get set differently (and properly for your purposes).&lt;/P&gt;</description>
      <pubDate>Fri, 26 Jun 2020 00:18:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/first-variable-not-working/m-p/665201#M198859</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2020-06-26T00:18:00Z</dc:date>
    </item>
    <item>
      <title>Re: first.variable not working</title>
      <link>https://communities.sas.com/t5/SAS-Programming/first-variable-not-working/m-p/665472#M198986</link>
      <description>&lt;P&gt;Thanks, It works!&lt;/P&gt;</description>
      <pubDate>Fri, 26 Jun 2020 21:42:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/first-variable-not-working/m-p/665472#M198986</guid>
      <dc:creator>pdkc494949</dc:creator>
      <dc:date>2020-06-26T21:42:50Z</dc:date>
    </item>
  </channel>
</rss>

