<?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: Adding descending sequence number in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Adding-descending-sequence-number/m-p/533834#M6233</link>
    <description>&lt;P&gt;A double do until() loop will do that:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
do i = 1 by 1 until(last.per);
    set have; by per;
    if ty = 0 then pos = i;
    end;
do i = -pos + 1 by 1 until(last.per);
    set have; by per;
    ty = i;
    output;
    end;
drop i pos;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 08 Feb 2019 04:42:01 GMT</pubDate>
    <dc:creator>PGStats</dc:creator>
    <dc:date>2019-02-08T04:42:01Z</dc:date>
    <item>
      <title>Adding descending sequence number</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Adding-descending-sequence-number/m-p/533815#M6230</link>
      <description>&lt;P&gt;Hi,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to add a descending sequence number in column ty keeping the 0 intact in the given place. Can anyone help me with sas&amp;nbsp;code for this.&amp;nbsp;Sheet 2 contains what I want. By the way, I have a huge number of data by many groups. So, there is no fixed value for the lowest and the highest value.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 08 Feb 2019 03:37:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Adding-descending-sequence-number/m-p/533815#M6230</guid>
      <dc:creator>abdulla</dc:creator>
      <dc:date>2019-02-08T03:37:13Z</dc:date>
    </item>
    <item>
      <title>Re: Adding descending sequence number</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Adding-descending-sequence-number/m-p/533832#M6232</link>
      <description>&lt;P&gt;This is a simplistic way to do this.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1. Find the start of each counter&lt;/P&gt;
&lt;P&gt;2. Merge it with main data set&lt;/P&gt;
&lt;P&gt;3. Count back down&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="Screen Shot 2019-02-07 at 9.31.30 PM.png" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/26969i7EA2D630387E6AFA/image-size/large?v=v2&amp;amp;px=999" role="button" title="Screen Shot 2019-02-07 at 9.31.30 PM.png" alt="Screen Shot 2019-02-07 at 9.31.30 PM.png" /&gt;&lt;/span&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Screen Shot 2019-02-07 at 9.31.45 PM.png" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/26970i77752D4635216FB1/image-size/large?v=v2&amp;amp;px=999" role="button" title="Screen Shot 2019-02-07 at 9.31.45 PM.png" alt="Screen Shot 2019-02-07 at 9.31.45 PM.png" /&gt;&lt;/span&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Screen Shot 2019-02-07 at 9.32.11 PM.png" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/26971iA2C30C5FB75B2521/image-size/large?v=v2&amp;amp;px=999" role="button" title="Screen Shot 2019-02-07 at 9.32.11 PM.png" alt="Screen Shot 2019-02-07 at 9.32.11 PM.png" /&gt;&lt;/span&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/260675"&gt;@abdulla&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to add a descending sequence number in column ty keeping the 0 intact in the given place. Can anyone help me with sas&amp;nbsp;code for this.&amp;nbsp;Sheet 2 contains what I want. By the way, I have a huge number of data by many groups. So, there is no fixed value for the lowest and the highest value.&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 08 Feb 2019 04:34:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Adding-descending-sequence-number/m-p/533832#M6232</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-02-08T04:34:44Z</dc:date>
    </item>
    <item>
      <title>Re: Adding descending sequence number</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Adding-descending-sequence-number/m-p/533834#M6233</link>
      <description>&lt;P&gt;A double do until() loop will do that:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
do i = 1 by 1 until(last.per);
    set have; by per;
    if ty = 0 then pos = i;
    end;
do i = -pos + 1 by 1 until(last.per);
    set have; by per;
    ty = i;
    output;
    end;
drop i pos;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 08 Feb 2019 04:42:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Adding-descending-sequence-number/m-p/533834#M6233</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2019-02-08T04:42:01Z</dc:date>
    </item>
    <item>
      <title>Re: Adding descending sequence number</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Adding-descending-sequence-number/m-p/533836#M6234</link>
      <description>&lt;P&gt;sql way. I just made the column name&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile datalines missover;
input id mes val;
datalines;
12 0.4
12 0.04
12 0.07
12 0.09
12 0.01
12 0.09  0
12 0.01
12 0.07
13 0.01
13 0.09
13 0.07
13 0.08
13 0.01	 0
13 0.02
13 0.03
14 0.07
14 0.06
14 0.07
14 0.09
14 0.08
14 0.09	 0
14 0.01
14 0.02
14 0.03
;

data have1/view=have1;
set have;
col =_n_;
run;

proc sql;
create table want as 
select a.id, 
      mes,
     col-ref as val
from have1 a 
left join
(select id, val, col as ref
from have1
where val = 0)b
on a.id=b.id
order by col;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 08 Feb 2019 04:44:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Adding-descending-sequence-number/m-p/533836#M6234</guid>
      <dc:creator>kiranv_</dc:creator>
      <dc:date>2019-02-08T04:44:32Z</dc:date>
    </item>
    <item>
      <title>Re: Adding descending sequence number</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Adding-descending-sequence-number/m-p/533841#M6235</link>
      <description>That's really awesome. Very simple and easy solution. Thanks a lot</description>
      <pubDate>Fri, 08 Feb 2019 04:57:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Adding-descending-sequence-number/m-p/533841#M6235</guid>
      <dc:creator>abdulla</dc:creator>
      <dc:date>2019-02-08T04:57:45Z</dc:date>
    </item>
    <item>
      <title>Re: Adding descending sequence number</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Adding-descending-sequence-number/m-p/534007#M6255</link>
      <description>&lt;P&gt;While it is known prodigy genius status aka&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/462"&gt;@PGStats&lt;/a&gt;&amp;nbsp;is obvious, I felt this is a fine question to practice the concept of &lt;STRONG&gt;find,reread, replace&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile cards truncover;
input Per	trn	ty;
cards;
12	0.4	
12	0.004	
12	0.007	
12	0.012	
12	0.089	0
12	-0.32	
12	-0.657	
13	0.25	
13	0.089	
13	0.29	
13	0.37	
13	0.96	
13	-0.21	0
13	-0.128	
13	0.84	
13	0.075	
14	-0.008	
14	0.008	
14	0.089	
14	0.089	
14	0.089	
14	0.9	
14	0.5	
14	0.34	
14	-0.21	0
14	-0.12	
14	0.001	
;

data want;
set have;
by per;
retain _k _f;
if first.per then do; _k=_n_;call missing(_j,_f);end;
if ty=0 then 
	do;
		do i=_k to _n_;
			set have point=i;
			_j+1;
			ty=_j-(_n_-_k+1);
			output;
		end;
		_f=1;
	end;
else if _f then do; ty=_f;output;_f+1;end;
drop _:;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 08 Feb 2019 16:53:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Adding-descending-sequence-number/m-p/534007#M6255</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-02-08T16:53:09Z</dc:date>
    </item>
    <item>
      <title>Re: Adding descending sequence number</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Adding-descending-sequence-number/m-p/534011#M6257</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt; &amp;nbsp; If for anything or anything at all you draw/will draw admiration for is your incredible understanding that is noticeable in many of your responses. The speed at which you have progressed doesn't seem like somebody learned so much so quickly. Very nice!!!&lt;/P&gt;</description>
      <pubDate>Fri, 08 Feb 2019 17:04:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Adding-descending-sequence-number/m-p/534011#M6257</guid>
      <dc:creator>MarkWik</dc:creator>
      <dc:date>2019-02-08T17:04:02Z</dc:date>
    </item>
    <item>
      <title>Re: Adding descending sequence number</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Adding-descending-sequence-number/m-p/534025#M6260</link>
      <description>&lt;P&gt;Thank you. But this is not a big problem. It's just a matter of using the case for yet another operation of CRUD(&lt;SPAN&gt;Create, Read, Update, and Delete). The idea is to figure what other/various functionality SAS provides to accomplish that. To be honest, PG's solution did click my mind too of course acknowledging my knowledge is not even a drop in PG's ocean of knowledge, however as I scrolled down and noticed that, my thoughts went to finding other ways of accomplishing&amp;nbsp;the task. It's all trial and error experiments.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;So further to your nice encouraging comment, I wanted to try using hash. Again same in principle&amp;nbsp;&lt;STRONG&gt;find,reread, replace. &lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;And here it is--&amp;gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile cards truncover;
input Per	trn	ty;
cards;
12	0.4	
12	0.004	
12	0.007	
12	0.012	
12	0.089	0
12	-0.32	
12	-0.657	
13	0.25	
13	0.089	
13	0.29	
13	0.37	
13	0.96	
13	-0.21	0
13	-0.128	
13	0.84	
13	0.075	
14	-0.008	
14	0.008	
14	0.089	
14	0.089	
14	0.089	
14	0.9	
14	0.5	
14	0.34	
14	-0.21	0
14	-0.12	
14	0.001	
;



data _null_;
if _n_=1 then do;
 dcl hash H (ordered: "A",multidata:'y') ;
   h.definekey  ("per") ;
   h.definedata ("per","trn", "ty") ;
   h.definedone () ;
end;
do _n_=1 by 1 until(last.per);
	set have end=lr;
	by per;
	if not _f then rc=h.add();
	if ty=0 then do;
		do n=1 by 1 while(h.do_over() eq 0);
		ty=n-_n_;
		RC_updt = H.replacedup();
  		end;
		_f=1;
	end;
	else if _f then do; ty=_f;rc=h.add();_f=sum(_f,1);end;
end;
if lr then h.output(dataset:'want');
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Results:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;Per trn ty 
12 0.400 -4 
12 0.004 -3 
12 0.007 -2 
12 0.012 -1 
12 0.089 0 
12 -0.320 1 
12 -0.657 2 
13 0.250 -5 
13 0.089 -4 
13 0.290 -3 
13 0.370 -2 
13 0.960 -1 
13 -0.210 0 
13 -0.128 1 
13 0.840 2 
13 0.075 3 
14 -0.008 -8 
14 0.008 -7 
14 0.089 -6 
14 0.089 -5 
14 0.089 -4 
14 0.900 -3 
14 0.500 -2 
14 0.340 -1 
14 -0.210 0 
14 -0.120 1 
14 0.001 2 

&lt;/PRE&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, 08 Feb 2019 18:19:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Adding-descending-sequence-number/m-p/534025#M6260</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-02-08T18:19:08Z</dc:date>
    </item>
  </channel>
</rss>

