<?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: Create a variable with count of  not missong obs in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-with-count-of-not-missong-obs/m-p/768722#M243858</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
by var1 notsorted;
retain v2 0;
if first.var1 ne last.var1
then do;
  v2 + 1;
  var2 = v2;
end;
else var2 = .;
drop v2;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 21 Sep 2021 09:47:04 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2021-09-21T09:47:04Z</dc:date>
    <item>
      <title>Create a variable with count of  not missong obs</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-with-count-of-not-missong-obs/m-p/768593#M243793</link>
      <description>&lt;P&gt;&lt;SPAN&gt;Hi&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;I'm a bit upset about myself cause i can find the solution of my problem.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;I have a dataset test1 with one variable Var1 and i need to create a second variable Var2with number 1, 2,3 etc.. except that each time i have two identical value in the first column, the value in the second column will be empty.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;i tried the code below and nothing works:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test2;
&amp;nbsp; &amp;nbsp;set test1;
&amp;nbsp; &amp;nbsp; &amp;nbsp; by Var1;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if first.Var1 then Var2 = _N_;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;else Var2 =.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Regards&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="First_second.PNG" style="width: 247px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/63746i745AF131F67DCEF0/image-size/large?v=v2&amp;amp;px=999" role="button" title="First_second.PNG" alt="First_second.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 20 Sep 2021 15:54:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-with-count-of-not-missong-obs/m-p/768593#M243793</guid>
      <dc:creator>foxrol94</dc:creator>
      <dc:date>2021-09-20T15:54:06Z</dc:date>
    </item>
    <item>
      <title>Re: Create a variable with count of  not missong obs</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-with-count-of-not-missong-obs/m-p/768600#M243797</link>
      <description>&lt;P&gt;I suspect you have a note in your log about having unsorted data?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;BY processing and FIRST only work if records are in order and in this case they're not in order. Does the order matter to the value assigned in VAR2 or do you just need to map then to a number?&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/5599"&gt;@foxrol94&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&lt;SPAN&gt;Hi&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;I'm a bit upset about myself cause i can find the solution of my problem.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;I have a dataset test1 with one variable Var1 and i need to create a second variable Var2with number 1, 2,3 etc.. except that each time i have two identical value in the first column, the value in the second column will be empty.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;i tried the code below and nothing works:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test2;
&amp;nbsp; &amp;nbsp;set test1;
&amp;nbsp; &amp;nbsp; &amp;nbsp; by Var1;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if first.Var1 then Var2 = _N_;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;else Var2 =.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Regards&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="First_second.PNG" style="width: 247px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/63746i745AF131F67DCEF0/image-size/large?v=v2&amp;amp;px=999" role="button" title="First_second.PNG" alt="First_second.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&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>Mon, 20 Sep 2021 16:13:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-with-count-of-not-missong-obs/m-p/768600#M243797</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-09-20T16:13:50Z</dc:date>
    </item>
    <item>
      <title>Re: Create a variable with count of  not missong obs</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-with-count-of-not-missong-obs/m-p/768607#M243801</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/5599"&gt;@foxrol94&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So,&amp;nbsp;in order to set &lt;FONT face="courier new,courier"&gt;Var2&lt;/FONT&gt; correctly, you need to have SAS remember all &lt;FONT face="courier new,courier"&gt;Var1&lt;/FONT&gt; values it has already seen while reading through dataset TEST1? This is a typical application of the &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lecompobjref/n1ieochmz5vlm9n14zy0qnp3syvm.htm" target="_blank" rel="noopener"&gt;hash object&lt;/A&gt;.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want(drop=_c);
if _n_=1 then do;
  dcl hash h();
  h.definekey('Var1');
  h.definedone();
end;
set test1;
if Var1 ne ' ' &amp;amp; h.check() then do;
  _c+1;
  Var2=_c;
  h.add();
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If a new non-missing*&amp;nbsp;&lt;FONT face="courier new,courier"&gt;Var1&lt;/FONT&gt; value is encountered, the return code of the &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lecompobjref/p05xq2t4hac4ivn187tai050d5c2.htm" target="_blank" rel="noopener"&gt;CHECK method&lt;/A&gt; is not zero and the DO-END block is executed: A counter &lt;FONT face="courier new,courier"&gt;_c&lt;/FONT&gt;, initialized to zero, is incremented (and automatically retained), &lt;FONT face="courier new,courier"&gt;Var2&lt;/FONT&gt;&amp;nbsp;(which otherwise would be missing) is set to the new value of &lt;FONT face="courier new,courier"&gt;_c&lt;/FONT&gt; and the value of &lt;FONT face="courier new,courier"&gt;Var1&lt;/FONT&gt; is added to the hash object ("lookup table").&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;*Edit: inserted IF condition &lt;FONT face="courier new,courier"&gt;Var1 ne ' '&lt;/FONT&gt; to exclude missing values from the count. Alternatively, you could insert an additional &lt;FONT face="courier new,courier"&gt;h.add();&lt;/FONT&gt; after &lt;FONT face="courier new,courier"&gt;h.definedone();&lt;/FONT&gt;.&lt;/P&gt;</description>
      <pubDate>Mon, 20 Sep 2021 17:43:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-with-count-of-not-missong-obs/m-p/768607#M243801</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2021-09-20T17:43:20Z</dc:date>
    </item>
    <item>
      <title>Re: Create a variable with count of  not missong obs</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-with-count-of-not-missong-obs/m-p/768722#M243858</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
by var1 notsorted;
retain v2 0;
if first.var1 ne last.var1
then do;
  v2 + 1;
  var2 = v2;
end;
else var2 = .;
drop v2;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 21 Sep 2021 09:47:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-with-count-of-not-missong-obs/m-p/768722#M243858</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-09-21T09:47:04Z</dc:date>
    </item>
  </channel>
</rss>

