<?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: Retain Last.var value in new variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Retain-Last-var-value-in-new-variable/m-p/72490#M15620</link>
    <description>Hi Ksharp&lt;BR /&gt;
But with a "hashexp:16" you're actually expecting a very large table &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;BR /&gt;
Cheers&lt;BR /&gt;
Patrick</description>
    <pubDate>Fri, 03 Jun 2011 11:49:47 GMT</pubDate>
    <dc:creator>Patrick</dc:creator>
    <dc:date>2011-06-03T11:49:47Z</dc:date>
    <item>
      <title>Retain Last.var value in new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-Last-var-value-in-new-variable/m-p/72485#M15615</link>
      <description>THE INITIAL DATA IS LIKE THIS.&lt;BR /&gt;
A 1&lt;BR /&gt;
A 2&lt;BR /&gt;
B 1&lt;BR /&gt;
B 2&lt;BR /&gt;
B 3&lt;BR /&gt;
B 4&lt;BR /&gt;
C 1&lt;BR /&gt;
D 1&lt;BR /&gt;
D 2&lt;BR /&gt;
D 3&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
I WANT TO HAVE THE LAST.NUM VALUSE TO BE MY NEW COLUMN FOR EACH CUSTOMERS.&lt;BR /&gt;
A 1 2&lt;BR /&gt;
A 2 2&lt;BR /&gt;
B 1 4&lt;BR /&gt;
B 2 4&lt;BR /&gt;
B 3 4&lt;BR /&gt;
B 4 4&lt;BR /&gt;
C 1 1&lt;BR /&gt;
D 1 3&lt;BR /&gt;
D 2 3&lt;BR /&gt;
D 3 3&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
I KNOW THAT I CAN CREATE SEPERATE DATASET WITH LAST.NUM AND MERGE AGAIN WITH THIS DAT, I'M JUST CURIOUS TO KNOW IF THERE IS ANY OTHER WAY WE CAN DO IN SINGLE DATASTEP. BECAUSE I'M CREATING "NUM" ALSO IN THE SAME DATA STEP.&lt;BR /&gt;
&lt;BR /&gt;
THANKS IN ADVANCE,&lt;BR /&gt;
ALANKAR&lt;BR /&gt;
CITI</description>
      <pubDate>Fri, 03 Jun 2011 02:36:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-Last-var-value-in-new-variable/m-p/72485#M15615</guid>
      <dc:creator>Alankar</dc:creator>
      <dc:date>2011-06-03T02:36:45Z</dc:date>
    </item>
    <item>
      <title>Re: Retain Last.var value in new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-Last-var-value-in-new-variable/m-p/72486#M15616</link>
      <description>If you are assigning a NUM variable value, then you are not able to use a BY statement.  Also, anyway, there is no look-forward called function with the SAS DATA step approach.&lt;BR /&gt;
&lt;BR /&gt;
Given the scenario you suggest and limitations, I would say the answer is no.  Why the "same DATA step" requirement anyway?  Suggest you exploit the SAS facilities without such strange limitations and control/processing specifications.&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.

Message was edited by: sbb</description>
      <pubDate>Fri, 03 Jun 2011 03:51:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-Last-var-value-in-new-variable/m-p/72486#M15616</guid>
      <dc:creator>sbb</dc:creator>
      <dc:date>2011-06-03T03:51:37Z</dc:date>
    </item>
    <item>
      <title>Re: Retain Last.var value in new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-Last-var-value-in-new-variable/m-p/72487#M15617</link>
      <description>Thanks Scott, Got it!!</description>
      <pubDate>Fri, 03 Jun 2011 05:35:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-Last-var-value-in-new-variable/m-p/72487#M15617</guid>
      <dc:creator>Alankar</dc:creator>
      <dc:date>2011-06-03T05:35:01Z</dc:date>
    </item>
    <item>
      <title>Re: Retain Last.var value in new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-Last-var-value-in-new-variable/m-p/72488#M15618</link>
      <description>Emmmm.Let me try it.&lt;BR /&gt;
I think SAS has this power.But this method is not suited for large table which will very slowly.&lt;BR /&gt;
[pre]&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
data temp;&lt;BR /&gt;
input id $ _num;&lt;BR /&gt;
cards;&lt;BR /&gt;
A 1&lt;BR /&gt;
A 2&lt;BR /&gt;
B 1&lt;BR /&gt;
B 2&lt;BR /&gt;
B 3&lt;BR /&gt;
B 4&lt;BR /&gt;
C 1&lt;BR /&gt;
D 1&lt;BR /&gt;
D 2&lt;BR /&gt;
D 3&lt;BR /&gt;
;&lt;BR /&gt;
run;&lt;BR /&gt;
data want(drop=_num rc count);&lt;BR /&gt;
 set temp end=last;&lt;BR /&gt;
 by id notsorted;&lt;BR /&gt;
 count+1;&lt;BR /&gt;
 num=_num*2 +1;&lt;BR /&gt;
 if _n_ eq 1 then do;&lt;BR /&gt;
                    declare hash _hh(hashexp:16);&lt;BR /&gt;
                     _hh.definekey('id','count');&lt;BR /&gt;
                     _hh.definedata('num');&lt;BR /&gt;
                     _hh.definedone();&lt;BR /&gt;
                    declare hash hh(hashexp:10);&lt;BR /&gt;
                     hh.definekey('id');&lt;BR /&gt;
                     hh.definedata('last_num');&lt;BR /&gt;
                     hh.definedone();&lt;BR /&gt;
                   end;&lt;BR /&gt;
&lt;BR /&gt;
 rc=_hh.add();&lt;BR /&gt;
 if last.id then  do;last_num=num; rc=hh.add();  end;&lt;BR /&gt;
 if last then do;&lt;BR /&gt;
               count=0;&lt;BR /&gt;
               do until(_last);&lt;BR /&gt;
                set temp end=_last;&lt;BR /&gt;
                call missing(last_num);&lt;BR /&gt;
                count+1;&lt;BR /&gt;
                _hh.find();&lt;BR /&gt;
                hh.find();&lt;BR /&gt;
                output;&lt;BR /&gt;
               end;&lt;BR /&gt;
              end;&lt;BR /&gt;
run;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Ksharp</description>
      <pubDate>Fri, 03 Jun 2011 06:05:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-Last-var-value-in-new-variable/m-p/72488#M15618</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2011-06-03T06:05:05Z</dc:date>
    </item>
    <item>
      <title>Re: Retain Last.var value in new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-Last-var-value-in-new-variable/m-p/72489#M15619</link>
      <description>Emmmm.Let me try it.&lt;BR /&gt;
I think SAS has this power.But this method is not suited for large table which will very slowly.&lt;BR /&gt;
[pre]&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
data temp;&lt;BR /&gt;
input id $ _num;&lt;BR /&gt;
cards;&lt;BR /&gt;
A 1&lt;BR /&gt;
A 2&lt;BR /&gt;
B 1&lt;BR /&gt;
B 2&lt;BR /&gt;
B 3&lt;BR /&gt;
B 4&lt;BR /&gt;
C 1&lt;BR /&gt;
D 1&lt;BR /&gt;
D 2&lt;BR /&gt;
D 3&lt;BR /&gt;
;&lt;BR /&gt;
run;&lt;BR /&gt;
data want(drop=_num rc count);&lt;BR /&gt;
 set temp end=last;&lt;BR /&gt;
 by id notsorted;&lt;BR /&gt;
 count+1;&lt;BR /&gt;
 num=_num*2 +1;&lt;BR /&gt;
 if _n_ eq 1 then do;&lt;BR /&gt;
                    declare hash _hh(hashexp:16);&lt;BR /&gt;
                     _hh.definekey('id','count');&lt;BR /&gt;
                     _hh.definedata('num');&lt;BR /&gt;
                     _hh.definedone();&lt;BR /&gt;
                    declare hash hh(hashexp:10);&lt;BR /&gt;
                     hh.definekey('id');&lt;BR /&gt;
                     hh.definedata('last_num');&lt;BR /&gt;
                     hh.definedone();&lt;BR /&gt;
                   end;&lt;BR /&gt;
&lt;BR /&gt;
 rc=_hh.add();&lt;BR /&gt;
 if last.id then  do;last_num=num; rc=hh.add();  end;&lt;BR /&gt;
 if last then do;&lt;BR /&gt;
               count=0;&lt;BR /&gt;
               do until(_last);&lt;BR /&gt;
                set temp end=_last;&lt;BR /&gt;
                call missing(last_num);&lt;BR /&gt;
                count+1;&lt;BR /&gt;
                _hh.find();&lt;BR /&gt;
                hh.find();&lt;BR /&gt;
                output;&lt;BR /&gt;
               end;&lt;BR /&gt;
              end;&lt;BR /&gt;
run;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Ksharp</description>
      <pubDate>Fri, 03 Jun 2011 06:09:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-Last-var-value-in-new-variable/m-p/72489#M15619</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2011-06-03T06:09:49Z</dc:date>
    </item>
    <item>
      <title>Re: Retain Last.var value in new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-Last-var-value-in-new-variable/m-p/72490#M15620</link>
      <description>Hi Ksharp&lt;BR /&gt;
But with a "hashexp:16" you're actually expecting a very large table &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;BR /&gt;
Cheers&lt;BR /&gt;
Patrick</description>
      <pubDate>Fri, 03 Jun 2011 11:49:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-Last-var-value-in-new-variable/m-p/72490#M15620</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2011-06-03T11:49:47Z</dc:date>
    </item>
    <item>
      <title>Re: Retain Last.var value in new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-Last-var-value-in-new-variable/m-p/72491#M15621</link>
      <description>Basically same as merging with summary data but in one step.&lt;BR /&gt;
&lt;BR /&gt;
[pre]&lt;BR /&gt;
data have;&lt;BR /&gt;
   input a $ b @@;&lt;BR /&gt;
   cards;&lt;BR /&gt;
A 1 A 2 &lt;BR /&gt;
B 1 B 2 B 3 B 4&lt;BR /&gt;
C 1&lt;BR /&gt;
D 1 D 2 D 3&lt;BR /&gt;
;;;;&lt;BR /&gt;
   run;&lt;BR /&gt;
data need;&lt;BR /&gt;
   do _n_ = 1 by 1 until(last.a);&lt;BR /&gt;
      set have(rename=(b=c));&lt;BR /&gt;
      by a;&lt;BR /&gt;
      end;&lt;BR /&gt;
   do _n_ = 1 to _n_;&lt;BR /&gt;
      set have;&lt;BR /&gt;
      output;&lt;BR /&gt;
      end;&lt;BR /&gt;
   run;&lt;BR /&gt;
proc print;&lt;BR /&gt;
   run;&lt;BR /&gt;
[/pre]</description>
      <pubDate>Fri, 03 Jun 2011 13:04:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-Last-var-value-in-new-variable/m-p/72491#M15621</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2011-06-03T13:04:13Z</dc:date>
    </item>
    <item>
      <title>Re: Retain Last.var value in new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-Last-var-value-in-new-variable/m-p/72492#M15622</link>
      <description>Hi:&lt;BR /&gt;
  If you have the resources to do 2 sorts, then you could use BY group processing and RETAIN. This program is more simple than using a HASH table, but does require that you sort the _NUM variable in descending order before the DATA step and then sort again after the DATA step.&lt;BR /&gt;
 &lt;BR /&gt;
cynthia&lt;BR /&gt;
         &lt;BR /&gt;
This output:&lt;BR /&gt;
[pre]&lt;BR /&gt;
Obs    id    _num    holdnum&lt;BR /&gt;
                               &lt;BR /&gt;
  1    A       1        2&lt;BR /&gt;
  2    A       2        2&lt;BR /&gt;
  3    B       1        4&lt;BR /&gt;
  4    B       2        4&lt;BR /&gt;
  5    B       3        4&lt;BR /&gt;
  6    B       4        4&lt;BR /&gt;
  7    C       1        1&lt;BR /&gt;
  8    D       1        3&lt;BR /&gt;
  9    D       2        3&lt;BR /&gt;
 10    D       3        3&lt;BR /&gt;
&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
from this program:&lt;BR /&gt;
[pre]&lt;BR /&gt;
data temp;&lt;BR /&gt;
  input id $ _num;&lt;BR /&gt;
return;&lt;BR /&gt;
datalines;&lt;BR /&gt;
A 1&lt;BR /&gt;
A 2&lt;BR /&gt;
B 1&lt;BR /&gt;
B 2&lt;BR /&gt;
B 3&lt;BR /&gt;
B 4&lt;BR /&gt;
C 1&lt;BR /&gt;
D 1&lt;BR /&gt;
D 2&lt;BR /&gt;
D 3&lt;BR /&gt;
;&lt;BR /&gt;
run;&lt;BR /&gt;
                      &lt;BR /&gt;
proc sort data=temp out=temp;&lt;BR /&gt;
by id descending _num;&lt;BR /&gt;
run;&lt;BR /&gt;
                &lt;BR /&gt;
data new;&lt;BR /&gt;
  set temp;&lt;BR /&gt;
  by id descending _num;&lt;BR /&gt;
  retain holdnum;&lt;BR /&gt;
  if first.id then holdnum = _num;&lt;BR /&gt;
          &lt;BR /&gt;
  putlog id= _num= holdnum=;&lt;BR /&gt;
  output;&lt;BR /&gt;
run;&lt;BR /&gt;
               &lt;BR /&gt;
proc sort data=new out=new;&lt;BR /&gt;
  by id _num;&lt;BR /&gt;
run;&lt;BR /&gt;
             &lt;BR /&gt;
proc print data=new;&lt;BR /&gt;
title 'After sorting new data';&lt;BR /&gt;
run;&lt;BR /&gt;
[/pre]</description>
      <pubDate>Fri, 03 Jun 2011 14:48:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-Last-var-value-in-new-variable/m-p/72492#M15622</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2011-06-03T14:48:32Z</dc:date>
    </item>
    <item>
      <title>Re: Retain Last.var value in new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-Last-var-value-in-new-variable/m-p/72493#M15623</link>
      <description>first.var and last.var processing applies to var coming through by-processing. So you cannot use this type of handling on variables you create in the step.&lt;BR /&gt;
If the column already exists in a table, you cannot just add another column, without writing out the data again, unless:&lt;BR /&gt;
you use data step views.&lt;BR /&gt;
* set creating NUM ;&lt;BR /&gt;
data something /view = something ;&lt;BR /&gt;
  keep ID num other stuff ;&lt;BR /&gt;
  * neccessary processing ;&lt;BR /&gt;
run ;&lt;BR /&gt;
data lastnum /view=lastnum ;&lt;BR /&gt;
  set something ;&lt;BR /&gt;
  by id num ;&lt;BR /&gt;
  if last.id ;&lt;BR /&gt;
 keep id num ;&lt;BR /&gt;
run ;&lt;BR /&gt;
data finally ;&lt;BR /&gt;
   merge something lastnum( rename= num=finalNum );&lt;BR /&gt;
   by ID ;&lt;BR /&gt;
run ;&lt;BR /&gt;
 &lt;BR /&gt;
of course, the second VIEW depends on having the first VIEW create its output in by-group order = ID  NUM . When this is not possible, it could be replaced with an SQL view - but that is just sorting under the covers. &lt;BR /&gt;
PROC SORT when you need to.&lt;BR /&gt;
 &lt;BR /&gt;
peterC</description>
      <pubDate>Fri, 03 Jun 2011 15:32:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-Last-var-value-in-new-variable/m-p/72493#M15623</guid>
      <dc:creator>Peter_C</dc:creator>
      <dc:date>2011-06-03T15:32:37Z</dc:date>
    </item>
    <item>
      <title>Re: Retain Last.var value in new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-Last-var-value-in-new-variable/m-p/72494#M15624</link>
      <description>Or, the self-interleave technique:&lt;BR /&gt;
&lt;BR /&gt;
[pre]&lt;BR /&gt;
data have ;&lt;BR /&gt;
input a $ @@ ;&lt;BR /&gt;
cards ;&lt;BR /&gt;
A A &lt;BR /&gt;
B B B B&lt;BR /&gt;
C &lt;BR /&gt;
D D D&lt;BR /&gt;
;&lt;BR /&gt;
&lt;BR /&gt;
data need ;&lt;BR /&gt;
set have(in=pass1)&lt;BR /&gt;
    have(in=pass2) ;&lt;BR /&gt;
by a ;&lt;BR /&gt;
if first.a then call missing(b,c) ;&lt;BR /&gt;
if pass1 then      c + 1 ;&lt;BR /&gt;
if pass2 then do ; b + 1 ; output ; end ;&lt;BR /&gt;
run ;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
&amp;gt; Basically same as merging with summary data but in&lt;BR /&gt;
&amp;gt; one step.&lt;BR /&gt;
&amp;gt; &lt;BR /&gt;
&amp;gt; [pre]&lt;BR /&gt;
&amp;gt; data have;&lt;BR /&gt;
&amp;gt;    input a $ b @@;&lt;BR /&gt;
&amp;gt; cards;&lt;BR /&gt;
&amp;gt; A 1 A 2 &lt;BR /&gt;
&amp;gt; B 1 B 2 B 3 B 4&lt;BR /&gt;
&amp;gt; C 1&lt;BR /&gt;
&amp;gt; D 1 D 2 D 3&lt;BR /&gt;
&amp;gt; ;;;;&lt;BR /&gt;
&amp;gt;    run;&lt;BR /&gt;
&amp;gt; a need;&lt;BR /&gt;
&amp;gt;    do _n_ = 1 by 1 until(last.a);&lt;BR /&gt;
&amp;gt;    set have(rename=(b=c));&lt;BR /&gt;
&amp;gt;    by a;&lt;BR /&gt;
&amp;gt;    end;&lt;BR /&gt;
&amp;gt; do _n_ = 1 to _n_;&lt;BR /&gt;
&amp;gt;       set have;&lt;BR /&gt;
&amp;gt; output;&lt;BR /&gt;
&amp;gt;       end;&lt;BR /&gt;
&amp;gt; ;&lt;BR /&gt;
&amp;gt; proc print;&lt;BR /&gt;
&amp;gt;    run;&lt;BR /&gt;
&amp;gt; re]</description>
      <pubDate>Sat, 04 Jun 2011 22:00:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-Last-var-value-in-new-variable/m-p/72494#M15624</guid>
      <dc:creator>Howles</dc:creator>
      <dc:date>2011-06-04T22:00:06Z</dc:date>
    </item>
    <item>
      <title>Re: Retain Last.var value in new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-Last-var-value-in-new-variable/m-p/72495#M15625</link>
      <description>Hi.Patrick.&lt;BR /&gt;
Yes.But as we know HashTable has some limitation with memory.&lt;BR /&gt;
And My method is to read original dataset twice.So it will yield low efficiency for large table.&lt;BR /&gt;
hashexp:16 just want to let it faster.&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Cheers!&lt;BR /&gt;
Ksharp</description>
      <pubDate>Tue, 07 Jun 2011 02:30:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-Last-var-value-in-new-variable/m-p/72495#M15625</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2011-06-07T02:30:43Z</dc:date>
    </item>
    <item>
      <title>Re: Retain Last.var value in new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-Last-var-value-in-new-variable/m-p/72496#M15626</link>
      <description>Hi.&lt;BR /&gt;
But the column OP want is the calculated column in the same data step.&lt;BR /&gt;
That is the problem where it is.&lt;BR /&gt;
&lt;BR /&gt;
Ksharp</description>
      <pubDate>Tue, 07 Jun 2011 02:47:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-Last-var-value-in-new-variable/m-p/72496#M15626</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2011-06-07T02:47:54Z</dc:date>
    </item>
  </channel>
</rss>

