<?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: Generate variable according to the order of same-id observations in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Generate-variable-according-to-the-order-of-same-id-observations/m-p/684643#M24216</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/345986"&gt;@Ginny_Han&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;(I used to use STATA and I wonder if SAS has things like "_n" and "_N" to denote the order and number of observations in a by-sort group (e.g. group id-date). Like in STATA there is "bysort id date" and "gen order=_n" or "gen x=x1(_n-1)". If there is similar concept is SAS, it would be great to know what it is.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;The SAS data step has an automatic varaible _n_ that counts the iteration of the data step. SAS automatic variables are not output to the data set but you can create a new variable with the value.&lt;/P&gt;
&lt;PRE&gt;data test;
   input id $ date $ order $ times;
   iteration = _n_;
datalines;
1 2010/09/10 1 1
2 2010/0912  1  2
2  2010/10/10 2 2
3 2010/04/03 1 3
3 2010/05/03 2 3
3 2010/05/29 3 3
;
run;&lt;/PRE&gt;
&lt;P&gt;With simple code such as this it returns a row number but with some of the more complex approaches possible the results can be bit unexpected.&lt;/P&gt;</description>
    <pubDate>Thu, 17 Sep 2020 14:52:24 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2020-09-17T14:52:24Z</dc:date>
    <item>
      <title>Generate variable according to the order of same-id observations</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Generate-variable-according-to-the-order-of-same-id-observations/m-p/684519#M24213</link>
      <description>&lt;P&gt;Dear all,&lt;/P&gt;&lt;P&gt;I have a question regarding how to generate a variable that reflect the order of the same-id observations. I don't want to delete any duplicated records.&lt;/P&gt;&lt;P&gt;I proposed a sample data here. Suppose id means patient and date is admission date and each observation is an admission record. I want to generate a "order" variable to reflect (after sorting by id and date) whether the observation is the 1st, 2nd... admission. I also want a "times" variable to reflect how many admissions (observations) of the patient there are in the dataset.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;&lt;BR /&gt;input id $ date $ order $ times;
datalines;
1 2010/09/10 1 1
2 2010/0912  1  2
2  2010/10/10 2 2
3 2010/04/03 1 3
3 2010/05/03 2 3
3 2010/05/29 3 3
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;So far, I get how to sort the variables by id and then date.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=test out=test;
by id date;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Please help me on generating the "order" and "times" variables.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;(I used to use STATA and I wonder if SAS has things like "_n" and "_N" to denote the order and number of observations in a by-sort group (e.g. group id-date). Like in STATA there is "bysort id date" and "gen order=_n" or "gen x=x1(_n-1)". If there is similar concept is SAS, it would be great to know what it is called.)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Ginny Han&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 17 Sep 2020 08:50:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Generate-variable-according-to-the-order-of-same-id-observations/m-p/684519#M24213</guid>
      <dc:creator>Ginny_Han</dc:creator>
      <dc:date>2020-09-17T08:50:26Z</dc:date>
    </item>
    <item>
      <title>Re: Generate variable according to the order of same-id observations</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Generate-variable-according-to-the-order-of-same-id-observations/m-p/684522#M24214</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;double DoW-loop looks like good candidate to help.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
input id $ date yymmdd10.;
format date yymmdd10.;
datalines;
1 2010/09/10
2 2010/09/12
2 2010/10/10
3 2010/04/03
3 2010/05/03
3 2010/05/29
;
run;

proc sort data=test out=test;
by id date;
run;

data want;

do until (last.id);
  set test;
  by id date;
  times + 1; 
end;

order = 0;
do until (last.id);
  set test;
  by id date;
  order + 1; 
  output;
end;

times = 0;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;All the best&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Thu, 17 Sep 2020 09:04:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Generate-variable-according-to-the-order-of-same-id-observations/m-p/684522#M24214</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2020-09-17T09:04:03Z</dc:date>
    </item>
    <item>
      <title>Re: Generate variable according to the order of same-id observations</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Generate-variable-according-to-the-order-of-same-id-observations/m-p/684548#M24215</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
input id $ date yymmdd10.;
format date yymmdd10.;
datalines;
1 2010/09/10
2 2010/09/12
2 2010/10/10
3 2010/04/03
3 2010/05/03
3 2010/05/29
;
run;
data want;
 set test;
 by id;
 if first.id then order=0;
 order+1;
 times+first.id;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 17 Sep 2020 11:24:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Generate-variable-according-to-the-order-of-same-id-observations/m-p/684548#M24215</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2020-09-17T11:24:06Z</dc:date>
    </item>
    <item>
      <title>Re: Generate variable according to the order of same-id observations</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Generate-variable-according-to-the-order-of-same-id-observations/m-p/684643#M24216</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/345986"&gt;@Ginny_Han&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;(I used to use STATA and I wonder if SAS has things like "_n" and "_N" to denote the order and number of observations in a by-sort group (e.g. group id-date). Like in STATA there is "bysort id date" and "gen order=_n" or "gen x=x1(_n-1)". If there is similar concept is SAS, it would be great to know what it is.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;The SAS data step has an automatic varaible _n_ that counts the iteration of the data step. SAS automatic variables are not output to the data set but you can create a new variable with the value.&lt;/P&gt;
&lt;PRE&gt;data test;
   input id $ date $ order $ times;
   iteration = _n_;
datalines;
1 2010/09/10 1 1
2 2010/0912  1  2
2  2010/10/10 2 2
3 2010/04/03 1 3
3 2010/05/03 2 3
3 2010/05/29 3 3
;
run;&lt;/PRE&gt;
&lt;P&gt;With simple code such as this it returns a row number but with some of the more complex approaches possible the results can be bit unexpected.&lt;/P&gt;</description>
      <pubDate>Thu, 17 Sep 2020 14:52:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Generate-variable-according-to-the-order-of-same-id-observations/m-p/684643#M24216</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-09-17T14:52:24Z</dc:date>
    </item>
    <item>
      <title>Re: Generate variable according to the order of same-id observations</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Generate-variable-according-to-the-order-of-same-id-observations/m-p/684845#M24246</link>
      <description>&lt;P&gt;Hi Bart &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/35763"&gt;@yabwon&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;&lt;P&gt;Thanks for the solution! It worked fine.&lt;/P&gt;&lt;P&gt;Can I ask a follow-up question? How does the command "-output-" work in the do loop? Because with it, the loop would produce an order of the observations. And without it, the times variables would be generated with every observation in the same by-sort group having the same value?&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;order = 0;
do until (last.id);
  set test;
  by id date;
  order + 1; 
  output;
end;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Thanks again!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Ginny&lt;/P&gt;</description>
      <pubDate>Fri, 18 Sep 2020 01:11:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Generate-variable-according-to-the-order-of-same-id-observations/m-p/684845#M24246</guid>
      <dc:creator>Ginny_Han</dc:creator>
      <dc:date>2020-09-18T01:11:36Z</dc:date>
    </item>
    <item>
      <title>Re: Generate variable according to the order of same-id observations</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Generate-variable-according-to-the-order-of-same-id-observations/m-p/684849#M24247</link>
      <description>It's good to know anyhow. Thanks.</description>
      <pubDate>Fri, 18 Sep 2020 01:25:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Generate-variable-according-to-the-order-of-same-id-observations/m-p/684849#M24247</guid>
      <dc:creator>Ginny_Han</dc:creator>
      <dc:date>2020-09-18T01:25:36Z</dc:date>
    </item>
    <item>
      <title>Re: Generate variable according to the order of same-id observations</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Generate-variable-according-to-the-order-of-same-id-observations/m-p/684889#M24250</link>
      <description>&lt;P&gt;Hi Ginny,&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/345986"&gt;@Ginny_Han&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;it is not exactly as you writing, I'll try to explain.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;First you have to remember thai _each_ `set` statement in your code (and we have two here) is a separate reading process. Here is a bit modified version of the original code with some additional `put` statements printing state of the PDV vector to the log. I also added two `curobs=` options to show which observation was read during which `set` statement execution (look at it in the log below).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
input id $ date yymmdd10.;
format date yymmdd10.;
datalines;
1 2010/09/10
2 2010/09/12
2 2010/10/10
3 2010/04/03
3 2010/05/03
3 2010/05/29
;
run;

proc sort data=test out=test;
by id date;
run;

options ls=max ps=max;
data want;

put "Before: " @20 _ALL_;

do until (last.id);
  set test curobs = curobs1;
  by id date;
  times + 1; 
  put "First loop: " @20 _ALL_;
end; /* here the TIMES variable is only acumulated */

put "After first loop: "  @20 _ALL_;

order = 0;
do until (last.id);
  set test curobs = curobs2;
  by id date;
  order + 1; 
  put "Second loop: " @20 _ALL_;
  output; /* here data are outputed to WANT dataset */
end;

put "After second loop: "  @20 _ALL_;
put ;
times = 0;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Here is the log:&lt;/P&gt;
&lt;PRE&gt;1    data test;
2    input id $ date yymmdd10.;
3    format date yymmdd10.;
4    datalines;

NOTE: The data set WORK.TEST has 6 observations and 2 variables.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds


11   ;
12   run;
13
14   proc sort data=test out=test;
15   by id date;
16   run;

NOTE: There were 6 observations read from the data set WORK.TEST.
NOTE: The data set WORK.TEST has 6 observations and 2 variables.
NOTE: PROCEDURE SORT used (Total process time):
      real time           0.00 seconds
      cpu time            0.01 seconds


17
18   options ls=max ps=max;
19   data want;
20
21   put "Before: " @20 _ALL_;
22
23   do until (last.id);
24     set test curobs = curobs1;
25     by id date;
26     times + 1;
27     put "First loop: " @20 _ALL_;
28   end; /* here the TIMES variable is only acumulated */
29
30   put "After first loop: "  @20 _ALL_;
31
32   order = 0;
33   do until (last.id);
34     set test curobs = curobs2;
35     by id date;
36     order + 1;
37     put "Second loop: " @20 _ALL_;
38     output; /* here data are outputed to WANT dataset */
39   end;
40
41   put "After second loop: "  @20 _ALL_;
42   put ;
43   times = 0;
44   run;

Before:            last.id=1 curobs1=. id=  date=. FIRST.id=1 FIRST.date=1 LAST.date=1 times=0 order=0 curobs2=. _ERROR_=0 _N_=1
First loop:        last.id=1 curobs1=1 id=1 date=2010-09-10 FIRST.id=1 FIRST.date=1 LAST.date=1 times=1 order=0 curobs2=. _ERROR_=0 _N_=1
After first loop:  last.id=1 curobs1=1 id=1 date=2010-09-10 FIRST.id=1 FIRST.date=1 LAST.date=1 times=1 order=0 curobs2=. _ERROR_=0 _N_=1
Second loop:       last.id=1 curobs1=1 id=1 date=2010-09-10 FIRST.id=1 FIRST.date=1 LAST.date=1 times=1 order=1 curobs2=1 _ERROR_=0 _N_=1
After second loop: last.id=1 curobs1=1 id=1 date=2010-09-10 FIRST.id=1 FIRST.date=1 LAST.date=1 times=1 order=1 curobs2=1 _ERROR_=0 _N_=1

Before:            last.id=1 curobs1=1 id=1 date=2010-09-10 FIRST.id=1 FIRST.date=1 LAST.date=1 times=0 order=1 curobs2=1 _ERROR_=0 _N_=2
First loop:        last.id=0 curobs1=2 id=2 date=2010-09-12 FIRST.id=1 FIRST.date=1 LAST.date=1 times=1 order=1 curobs2=1 _ERROR_=0 _N_=2
First loop:        last.id=1 curobs1=3 id=2 date=2010-10-10 FIRST.id=0 FIRST.date=1 LAST.date=1 times=2 order=1 curobs2=1 _ERROR_=0 _N_=2
After first loop:  last.id=1 curobs1=3 id=2 date=2010-10-10 FIRST.id=0 FIRST.date=1 LAST.date=1 times=2 order=1 curobs2=1 _ERROR_=0 _N_=2
Second loop:       last.id=0 curobs1=3 id=2 date=2010-09-12 FIRST.id=1 FIRST.date=1 LAST.date=1 times=2 order=1 curobs2=2 _ERROR_=0 _N_=2
Second loop:       last.id=1 curobs1=3 id=2 date=2010-10-10 FIRST.id=0 FIRST.date=1 LAST.date=1 times=2 order=2 curobs2=3 _ERROR_=0 _N_=2
After second loop: last.id=1 curobs1=3 id=2 date=2010-10-10 FIRST.id=0 FIRST.date=1 LAST.date=1 times=2 order=2 curobs2=3 _ERROR_=0 _N_=2

Before:            last.id=1 curobs1=3 id=2 date=2010-10-10 FIRST.id=0 FIRST.date=1 LAST.date=1 times=0 order=2 curobs2=3 _ERROR_=0 _N_=3
First loop:        last.id=0 curobs1=4 id=3 date=2010-04-03 FIRST.id=1 FIRST.date=1 LAST.date=1 times=1 order=2 curobs2=3 _ERROR_=0 _N_=3
First loop:        last.id=0 curobs1=5 id=3 date=2010-05-03 FIRST.id=0 FIRST.date=1 LAST.date=1 times=2 order=2 curobs2=3 _ERROR_=0 _N_=3
First loop:        last.id=1 curobs1=6 id=3 date=2010-05-29 FIRST.id=0 FIRST.date=1 LAST.date=1 times=3 order=2 curobs2=3 _ERROR_=0 _N_=3
After first loop:  last.id=1 curobs1=6 id=3 date=2010-05-29 FIRST.id=0 FIRST.date=1 LAST.date=1 times=3 order=2 curobs2=3 _ERROR_=0 _N_=3
Second loop:       last.id=0 curobs1=6 id=3 date=2010-04-03 FIRST.id=1 FIRST.date=1 LAST.date=1 times=3 order=1 curobs2=4 _ERROR_=0 _N_=3
Second loop:       last.id=0 curobs1=6 id=3 date=2010-05-03 FIRST.id=0 FIRST.date=1 LAST.date=1 times=3 order=2 curobs2=5 _ERROR_=0 _N_=3
Second loop:       last.id=1 curobs1=6 id=3 date=2010-05-29 FIRST.id=0 FIRST.date=1 LAST.date=1 times=3 order=3 curobs2=6 _ERROR_=0 _N_=3
After second loop: last.id=1 curobs1=6 id=3 date=2010-05-29 FIRST.id=0 FIRST.date=1 LAST.date=1 times=3 order=3 curobs2=6 _ERROR_=0 _N_=3

Before:            last.id=1 curobs1=6 id=3 date=2010-05-29 FIRST.id=0 FIRST.date=1 LAST.date=1 times=0 order=3 curobs2=6 _ERROR_=0 _N_=4
NOTE: There were 6 observations read from the data set WORK.TEST.
NOTE: There were 6 observations read from the data set WORK.TEST.
NOTE: The data set WORK.WANT has 6 observations and 4 variables.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.01 seconds
&lt;/PRE&gt;
&lt;P&gt;So basically first loop reads observation 1 from TEST [curobs1=1] (in this case it is also the last observation in the group) and increases `times` by 1 (with the `times + 1;` sum statement). when the first loop ends the PDV contains "accumulated" value of `times`. The second loop (since it contains separate `set` statement) it reads observation 1 from the TEST [curobs2=1] and increases order by 1, and executes `output` so the current state od PDV is moved as new observation into WANT. First iteration of data step ends but since there was the `set` statement in the code SAS runs next iteration of data step'a main loop.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In the second iteration of the data step's main loop the first until-loop reads observations 2 and 3 from TEST [curobs1=2, 3] and increases `times` by 1 two times so when the first loop ends the PDV contains "accumulated" value of `times` equal to 2. The second loop (since it contains separate `set` statement) it reads observation 2 from the TEST [curobs2=2] and increases order by 1, and executes `output` so the current state od PDV is moved as the second observation into WANT, and then&amp;nbsp;it reads observation 3 from the TEST [curobs2=3] and increases order by 1 (so the value is 2), and executes `output` so the current state od PDV is moved as third observation into WANT.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then the third main loop iteration starts, and it does the same what two before but for observations 4,5,and 6 to cumulate `times` and the again 4,5, and 6.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And eventually SAS starts fourth iteration of the main loop (_N_ = 4 in the log) , first until-loop starts and the `set` statement in it tries to read next observation (7th) but since there is nothing more to read it stops.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So that is more-or-less how it works. If you need some more details there is great papers by Paul Dorfman (&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/21262"&gt;@hashman&lt;/a&gt;) titled: "The Magnificent DO" and "The DoW-Loop Unrolled",&lt;/P&gt;
&lt;P&gt;links are here:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://support.sas.com/resources/papers/proceedings13/126-2013.pdf" target="_blank" rel="noopener"&gt;https://support.sas.com/resources/papers/proceedings13/126-2013.pdf&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://www.lexjansen.com/nesug/nesug08/hw/hw02.pdf" target="_blank" rel="noopener"&gt;https://www.lexjansen.com/nesug/nesug08/hw/hw02.pdf&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;All the best&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 18 Sep 2020 06:47:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Generate-variable-according-to-the-order-of-same-id-observations/m-p/684889#M24250</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2020-09-18T06:47:11Z</dc:date>
    </item>
    <item>
      <title>Re: Generate variable according to the order of same-id observations</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Generate-variable-according-to-the-order-of-same-id-observations/m-p/684891#M24251</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I like "short-ness" of the approach&amp;nbsp; but I think it won't work for the following data where `times` for the last group should be 5,&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
input id $ date yymmdd10.;
format date yymmdd10.;
datalines;
1 2010/09/10
2 2010/09/12
2 2010/10/10
3 2010/04/03
3 2010/05/03
3 2010/05/29
3 2010/05/30
3 2010/05/31
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;All the best&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Fri, 18 Sep 2020 06:50:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Generate-variable-according-to-the-order-of-same-id-observations/m-p/684891#M24251</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2020-09-18T06:50:47Z</dc:date>
    </item>
    <item>
      <title>Re: Generate variable according to the order of same-id observations</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Generate-variable-according-to-the-order-of-same-id-observations/m-p/685172#M24276</link>
      <description>&lt;P&gt;OK. I misunderstand the OP's question .&lt;/P&gt;</description>
      <pubDate>Sat, 19 Sep 2020 11:06:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Generate-variable-according-to-the-order-of-same-id-observations/m-p/685172#M24276</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2020-09-19T11:06:38Z</dc:date>
    </item>
  </channel>
</rss>

