<?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 variable that counts progressively how many times variable repeated in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Create-variable-that-counts-progressively-how-many-times/m-p/867496#M342627</link>
    <description>&lt;P&gt;Its usually unhelpful to show us the wrong answer, and not show us the correct answer. So show us the answer you want for this data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you need values that are incremented on each row subject to some conditions, SQL is not the tool.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 31 Mar 2023 16:23:32 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2023-03-31T16:23:32Z</dc:date>
    <item>
      <title>Create variable that counts progressively how many times variable repeated</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-variable-that-counts-progressively-how-many-times/m-p/867493#M342624</link>
      <description>&lt;P&gt;Hi,&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need to find how many times account_id is repeating along with department.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tried to use first. procedure for this problem. I was able to find how many times account_id is repeating; however, I was not able to get the desire value for department.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Also, do we have proc sql procedure to do something similar in SAS?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data have;
input Account_Id Dept Salary Emp_Status $ Projects $ Rating;
cards;
111         123  7000  Perm       A        5
111         123  6000  Perm       B        4
111         123  7000  Perm       C        5
222         124  6000  Perm       A        5
333         125  7000  Perm       B        4
333         125  7000  Perm       C        5
;
run;

proc sort data=have;
by account_id dept;
run;

data want ;
set have;
by account_id;
if first.account_id then do;
flag = 0;
if first.dept then flag_1 = 0;
end;
flag_1 + 1;

flag + 1;
retain flag_1;
retain flag;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="dht115_0-1680279314903.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/82236i571709B45D234F47/image-size/medium?v=v2&amp;amp;px=400" role="button" title="dht115_0-1680279314903.png" alt="dht115_0-1680279314903.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 31 Mar 2023 16:17:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-variable-that-counts-progressively-how-many-times/m-p/867493#M342624</guid>
      <dc:creator>dht115</dc:creator>
      <dc:date>2023-03-31T16:17:23Z</dc:date>
    </item>
    <item>
      <title>Re: Create variable that counts progressively how many times variable repeated</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-variable-that-counts-progressively-how-many-times/m-p/867495#M342626</link>
      <description>&lt;P&gt;You get FIRST. variables only for variables included in the BY statement, so you must have dept there. Add the NOTSORTED option if "smaller" values can follow "larger" values.&lt;/P&gt;</description>
      <pubDate>Fri, 31 Mar 2023 16:22:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-variable-that-counts-progressively-how-many-times/m-p/867495#M342626</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-03-31T16:22:48Z</dc:date>
    </item>
    <item>
      <title>Re: Create variable that counts progressively how many times variable repeated</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-variable-that-counts-progressively-how-many-times/m-p/867496#M342627</link>
      <description>&lt;P&gt;Its usually unhelpful to show us the wrong answer, and not show us the correct answer. So show us the answer you want for this data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you need values that are incremented on each row subject to some conditions, SQL is not the tool.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 31 Mar 2023 16:23:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-variable-that-counts-progressively-how-many-times/m-p/867496#M342627</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-03-31T16:23:32Z</dc:date>
    </item>
    <item>
      <title>Re: Create variable that counts progressively how many times variable repeated</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-variable-that-counts-progressively-how-many-times/m-p/867500#M342630</link>
      <description>&lt;P&gt;I don't understand what you are counting.&amp;nbsp; Can you share the expected output for the given input?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also I don't understand how your data is organized.&amp;nbsp; Are departments defined within accounts? Or are accounts defined within departments?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want to count how many times the combination of account and department changes and number each record within those combinations you could use something like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  by Account_Id Dept ;
  department_no + first.dept;
  record_no+1;
  if first.dept then record_no=1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;PRE&gt;       Account_                       Emp_                           department_    record_
Obs       Id       Dept    Salary    Status    Projects    Rating         no           no

 1        111       123     7000      Perm        A           5           1            1
 2        111       123     6000      Perm        B           4           1            2
 3        111       123     7000      Perm        C           5           1            3
 4        222       124     6000      Perm        A           5           2            1
 5        333       125     7000      Perm        B           4           3            1
 6        333       125     7000      Perm        C           5           3            2
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 31 Mar 2023 16:29:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-variable-that-counts-progressively-how-many-times/m-p/867500#M342630</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-03-31T16:29:28Z</dc:date>
    </item>
  </channel>
</rss>

