<?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: Assign CNT by obs? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Assign-CNT-by-obs/m-p/666695#M199521</link>
    <description>&lt;P&gt;You are using the subsetting IF statement.&amp;nbsp; This means that every observation in the dataset gets passed on by the data engine to the data step. The data step will increment _N_ for each such incoming record, PRIOR to the filtering by IF.&amp;nbsp;&amp;nbsp; Apparently the first 37 records don't satisfy the condition, but that's only after _N_ is incremented.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But if you replace the IF statement with a WHERE statement &lt;EM&gt;&lt;STRONG&gt;the filtering task is offloaded to the data engine&lt;/STRONG&gt;&lt;/EM&gt;.&amp;nbsp; Those 37 records don't even make it as far as the data step processing, and therefore _N_ is not incremented for them.&amp;nbsp;&amp;nbsp; So using WHERE will allow your first record satisfying the conditions to have _N_=1, no matter how many non-qualifying observatinos precede it.&lt;/P&gt;</description>
    <pubDate>Thu, 02 Jul 2020 20:09:34 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2020-07-02T20:09:34Z</dc:date>
    <item>
      <title>Assign CNT by obs?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assign-CNT-by-obs/m-p/666686#M199512</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;I would like to assign 1,2,3,4... by counting obs, I use the code below.&amp;nbsp; I found the repeater_ID starting with 38 instead of 1, how could this happened?&amp;nbsp; Thanks.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;repeat_ID=_N_+0;&lt;/PRE&gt;</description>
      <pubDate>Thu, 02 Jul 2020 19:35:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assign-CNT-by-obs/m-p/666686#M199512</guid>
      <dc:creator>ybz12003</dc:creator>
      <dc:date>2020-07-02T19:35:38Z</dc:date>
    </item>
    <item>
      <title>Re: Assign CNT by obs?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assign-CNT-by-obs/m-p/666689#M199515</link>
      <description>_n_ starts at 38 somehow...what does the rest of your code look like?&lt;BR /&gt;Do you have a WHERE/subsetting IF?&lt;BR /&gt;</description>
      <pubDate>Thu, 02 Jul 2020 19:41:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assign-CNT-by-obs/m-p/666689#M199515</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-07-02T19:41:09Z</dc:date>
    </item>
    <item>
      <title>Re: Assign CNT by obs?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assign-CNT-by-obs/m-p/666691#M199517</link>
      <description>&lt;P&gt;I have if statement in front of +0.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;if caseid_1 ^=' ' or caseid_2 ^=' ' or caseid_3 ^=' ' or caseid_4 ^=' ' or caseid_5 ^=' ';&lt;/PRE&gt;</description>
      <pubDate>Thu, 02 Jul 2020 19:53:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assign-CNT-by-obs/m-p/666691#M199517</guid>
      <dc:creator>ybz12003</dc:creator>
      <dc:date>2020-07-02T19:53:05Z</dc:date>
    </item>
    <item>
      <title>Re: Assign CNT by obs?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assign-CNT-by-obs/m-p/666692#M199518</link>
      <description>&lt;P&gt;So you are only accessing the _n_ conditional and it increments in between. Either change your logic so you don't use the automatic _n_ variable or change it to be an unconditional execution.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://stats.idre.ucla.edu/sas/faq/how-can-i-create-an-enumeration-variable-by-groups/" target="_blank"&gt;https://stats.idre.ucla.edu/sas/faq/how-can-i-create-an-enumeration-variable-by-groups/&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/67134"&gt;@ybz12003&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I have if statement in front of +0.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;if caseid_1 ^=' ' or caseid_2 ^=' ' or caseid_3 ^=' ' or caseid_4 ^=' ' or caseid_5 ^=' ';&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 02 Jul 2020 20:00:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assign-CNT-by-obs/m-p/666692#M199518</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-07-02T20:00:59Z</dc:date>
    </item>
    <item>
      <title>Re: Assign CNT by obs?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assign-CNT-by-obs/m-p/666695#M199521</link>
      <description>&lt;P&gt;You are using the subsetting IF statement.&amp;nbsp; This means that every observation in the dataset gets passed on by the data engine to the data step. The data step will increment _N_ for each such incoming record, PRIOR to the filtering by IF.&amp;nbsp;&amp;nbsp; Apparently the first 37 records don't satisfy the condition, but that's only after _N_ is incremented.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But if you replace the IF statement with a WHERE statement &lt;EM&gt;&lt;STRONG&gt;the filtering task is offloaded to the data engine&lt;/STRONG&gt;&lt;/EM&gt;.&amp;nbsp; Those 37 records don't even make it as far as the data step processing, and therefore _N_ is not incremented for them.&amp;nbsp;&amp;nbsp; So using WHERE will allow your first record satisfying the conditions to have _N_=1, no matter how many non-qualifying observatinos precede it.&lt;/P&gt;</description>
      <pubDate>Thu, 02 Jul 2020 20:09:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assign-CNT-by-obs/m-p/666695#M199521</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-07-02T20:09:34Z</dc:date>
    </item>
    <item>
      <title>Re: Assign CNT by obs?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assign-CNT-by-obs/m-p/666709#M199530</link>
      <description>&lt;P&gt;&amp;nbsp;I changed if to where, I still have the same result.&amp;nbsp; The repeat_ID started with 38.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;where caseid_1 ^=' ' or caseid_2 ^=' ' or caseid_3 ^=' ' or caseid_4 ^=' ' or caseid_5 ^=' ';&lt;/PRE&gt;</description>
      <pubDate>Thu, 02 Jul 2020 21:19:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assign-CNT-by-obs/m-p/666709#M199530</guid>
      <dc:creator>ybz12003</dc:creator>
      <dc:date>2020-07-02T21:19:18Z</dc:date>
    </item>
    <item>
      <title>Re: Assign CNT by obs?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assign-CNT-by-obs/m-p/666711#M199531</link>
      <description>&lt;P&gt;The time has come to show the data step code, and the log.&lt;/P&gt;</description>
      <pubDate>Thu, 02 Jul 2020 21:34:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assign-CNT-by-obs/m-p/666711#M199531</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-07-02T21:34:37Z</dc:date>
    </item>
    <item>
      <title>Re: Assign CNT by obs?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assign-CNT-by-obs/m-p/666716#M199536</link>
      <description>&lt;P&gt;_N_ is NOT an observation counter. It is a data step iteration counter.&lt;/P&gt;
&lt;P&gt;The easiest way to create a counter variable is to use a SUM statement.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   set have;
   obs_no+1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;From the name of your variable however it looks like you are instead trying to count the observations by some grouping variable.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   set have;
   by id;
   repeat_id+1;
   if first.id then repeat_id=1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note: Make sure that you use this to make a NEW variable.&amp;nbsp; If the variable already exists in the source dataset then the counting will not work right since the value from the previous iteration will be overwritten by the value read in from the source dataset.&lt;/P&gt;</description>
      <pubDate>Thu, 02 Jul 2020 22:25:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assign-CNT-by-obs/m-p/666716#M199536</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-07-02T22:25:19Z</dc:date>
    </item>
    <item>
      <title>Re: Assign CNT by obs?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assign-CNT-by-obs/m-p/669538#M200864</link>
      <description>It's strange that when I use where to ran today, it works!</description>
      <pubDate>Wed, 15 Jul 2020 15:51:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assign-CNT-by-obs/m-p/669538#M200864</guid>
      <dc:creator>ybz12003</dc:creator>
      <dc:date>2020-07-15T15:51:14Z</dc:date>
    </item>
  </channel>
</rss>

