<?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: Shifting values down in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Shifting-values-down/m-p/287478#M59160</link>
    <description>Thanks, that works too.</description>
    <pubDate>Wed, 27 Jul 2016 13:56:22 GMT</pubDate>
    <dc:creator>anu1999</dc:creator>
    <dc:date>2016-07-27T13:56:22Z</dc:date>
    <item>
      <title>Shifting values down</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Shifting-values-down/m-p/287339#M59098</link>
      <description>&lt;P&gt;Hi I am trying to get an output which is similar to&amp;nbsp;creating lags but lag functions doesn't give me desired output. Please suggest how to get the output like &amp;nbsp;shown below.&lt;/P&gt;
&lt;P&gt;Mainly I want to create a new variable "l_no" which looks back to last year value from "no" variable by id and yr variable (if no value available for last year then missing).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data sin_x;&lt;BR /&gt;input &lt;SPAN&gt;id yr no&lt;/SPAN&gt;;&lt;BR /&gt;datalines;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;1 2015 41 &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;1 2014 2 &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;2 2012 3 &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;2 2011 4 &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;3 2010 7&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;3 2009 0&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;3 2008 4&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;4 2007 0 &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;4 2006 7&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="4" color="#800000"&gt;&lt;STRONG&gt;Desired output:&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;id yr &amp;nbsp; &amp;nbsp; no &amp;nbsp; l_no&lt;/STRONG&gt;&lt;BR /&gt;1 &amp;nbsp;2015 &amp;nbsp;41 &amp;nbsp; &amp;nbsp;2&lt;BR /&gt;1 &amp;nbsp;2014 &amp;nbsp;2 &amp;nbsp; &amp;nbsp; &amp;nbsp;.&lt;BR /&gt;2 &amp;nbsp;2012 &amp;nbsp;3 &amp;nbsp; &amp;nbsp; 4&lt;BR /&gt;2 &amp;nbsp;2011 &amp;nbsp;4 &amp;nbsp; &amp;nbsp; .&lt;BR /&gt;3 &amp;nbsp;2010 &amp;nbsp;7 &amp;nbsp; &amp;nbsp;0&lt;BR /&gt;3 &amp;nbsp;2009 &amp;nbsp;0 &amp;nbsp; &amp;nbsp;4&lt;BR /&gt;3 &amp;nbsp;2008 &amp;nbsp;4 &amp;nbsp; &amp;nbsp;.&lt;BR /&gt;4 &amp;nbsp;2007 &amp;nbsp;0 &amp;nbsp; &amp;nbsp;7&lt;BR /&gt;4 &amp;nbsp;2006 &amp;nbsp;7 &amp;nbsp; &amp;nbsp;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Appreciate your help.&lt;/P&gt;</description>
      <pubDate>Tue, 26 Jul 2016 20:45:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Shifting-values-down/m-p/287339#M59098</guid>
      <dc:creator>anu1999</dc:creator>
      <dc:date>2016-07-26T20:45:37Z</dc:date>
    </item>
    <item>
      <title>Re: Shifting values down</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Shifting-values-down/m-p/287345#M59101</link>
      <description>&lt;P&gt;Go for a left join:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
proc sql;
create table desired as
select a.id, a.yr, b.no as l_no
from sin_x as a left join sin_x as b on a.id=b.id and a.yr=b.yr+1
order by id, yr desc;
select * from desired;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 26 Jul 2016 20:56:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Shifting-values-down/m-p/287345#M59101</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2016-07-26T20:56:20Z</dc:date>
    </item>
    <item>
      <title>Re: Shifting values down</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Shifting-values-down/m-p/287350#M59102</link>
      <description>&lt;P&gt;If you like a data step program here you go:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The maximum group size is set to 3. This can be changed to the actual max number. It can be programatically found.&lt;/P&gt;&lt;PRE&gt;data sin_x;
input id yr no;
datalines;
1 2015 41 
1 2014 2 
2 2012 3 
2 2011 4 
3 2010 7
3 2009 0
3 2008 4
4 2007 0 
4 2006 7
;
run;

%let groupMax = 3;

data want;
   array k[&amp;amp;groupMax] _temporary_;
   do i = 1 by 1 until(last.id);
      set sin_x;
      by id;
      k[i] = no;
   end;
   
   do j = 1 to dim(k) -1;
      l_no = k[j + 1];
      output;
   end;
   call missing(of k[*]);
drop i j;
run;

proc print data = want;
run;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 26 Jul 2016 21:19:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Shifting-values-down/m-p/287350#M59102</guid>
      <dc:creator>KachiM</dc:creator>
      <dc:date>2016-07-26T21:19:02Z</dc:date>
    </item>
    <item>
      <title>Re: Shifting values down</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Shifting-values-down/m-p/287354#M59104</link>
      <description>&lt;P&gt;Sort data by ID year.&lt;/P&gt;
&lt;P&gt;Use Lag, pay attention to id&lt;/P&gt;
&lt;P&gt;resort by id descending year&lt;/P&gt;</description>
      <pubDate>Tue, 26 Jul 2016 22:03:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Shifting-values-down/m-p/287354#M59104</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-07-26T22:03:21Z</dc:date>
    </item>
    <item>
      <title>Re: Shifting values down</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Shifting-values-down/m-p/287382#M59124</link>
      <description>&lt;PRE&gt;

data sin_x;
input id yr no;
datalines;
1 2015 41 
1 2014 2 
2 2012 3 
2 2011 4 
3 2010 7
3 2009 0
3 2008 4
4 2007 0 
4 2006 7
;
run;
data want;
 if _n_=1 then do;
  if 0 then set sin_x(rename=(yr=_yr no=i_no));
  declare hash h(dataset:'sin_x(rename=(yr=_yr no=i_no))');
  h.definekey('id','_yr');
  h.definedata('i_no');
  h.definedone();
 end;
call missing(of _all_);
set sin_x;
_yr=yr-1;
rc=h.find();
drop _: rc;
run;




&lt;/PRE&gt;</description>
      <pubDate>Wed, 27 Jul 2016 01:19:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Shifting-values-down/m-p/287382#M59124</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-07-27T01:19:22Z</dc:date>
    </item>
    <item>
      <title>Re: Shifting values down</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Shifting-values-down/m-p/287476#M59158</link>
      <description>Thanks that works.</description>
      <pubDate>Wed, 27 Jul 2016 13:53:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Shifting-values-down/m-p/287476#M59158</guid>
      <dc:creator>anu1999</dc:creator>
      <dc:date>2016-07-27T13:53:57Z</dc:date>
    </item>
    <item>
      <title>Re: Shifting values down</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Shifting-values-down/m-p/287477#M59159</link>
      <description>Thanks, that works.</description>
      <pubDate>Wed, 27 Jul 2016 13:54:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Shifting-values-down/m-p/287477#M59159</guid>
      <dc:creator>anu1999</dc:creator>
      <dc:date>2016-07-27T13:54:56Z</dc:date>
    </item>
    <item>
      <title>Re: Shifting values down</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Shifting-values-down/m-p/287478#M59160</link>
      <description>Thanks, that works too.</description>
      <pubDate>Wed, 27 Jul 2016 13:56:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Shifting-values-down/m-p/287478#M59160</guid>
      <dc:creator>anu1999</dc:creator>
      <dc:date>2016-07-27T13:56:22Z</dc:date>
    </item>
    <item>
      <title>Re: Shifting values down</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Shifting-values-down/m-p/287482#M59161</link>
      <description>&lt;P&gt;LAG does exactly what you want. &amp;nbsp;You just have your data sorted incorrectly. Also remember to call LAG() on every observation.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have; by id yr; run;
data want ;
  set have ;
  by id yr ;
  lag_no=lag(no);
  if not first.id then l_no=lag_no;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/4249iE344F5ECAB79D40E/image-size/original?v=v2&amp;amp;px=-1" border="0" alt="Capture.PNG" title="Capture.PNG" /&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 27 Jul 2016 14:02:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Shifting-values-down/m-p/287482#M59161</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2016-07-27T14:02:02Z</dc:date>
    </item>
  </channel>
</rss>

