<?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: LAG function in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/LAG-function/m-p/416646#M102320</link>
    <description>&lt;P&gt;Determine the number of needed variables first, and then use array processing:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data one;
input ID $ number ;
datalines;
a1 100
a1 150
a1 200
a2 125
a2 150
a2 300
a2 400
a3 3
a3 4
a3 5
a3 6
a3 7
a3 8
a3 9
;
run;

proc sql noprint;
select max(counter) - 1 into :counter from (select count(*) as counter from one group by id);
quit;

%let counter=&amp;amp;counter; *removes blanks;

data want;
set one;
by id;
array targets {&amp;amp;counter} c1-c&amp;amp;counter;
retain c1-c&amp;amp;counter;
if first.id
then do;
  i = 1;
  do i1 = 1 to &amp;amp;counter;
    targets{i1} = .;
  end;
end;
else i + 1;
do i1 = i-1 to 2 by -1;
  targets{i1} = targets{i1-1};
end;
targets{1} = lag(number);
if first.id then targets{1} = .;
drop i i1;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I added your second data example as group "a3".&lt;/P&gt;</description>
    <pubDate>Tue, 28 Nov 2017 11:56:16 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2017-11-28T11:56:16Z</dc:date>
    <item>
      <title>LAG function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/LAG-function/m-p/416628#M102313</link>
      <description>&lt;P&gt;data one;&lt;BR /&gt;input ID $ number ;&lt;BR /&gt;datalines;&lt;BR /&gt;a1 100&lt;BR /&gt;a1 150&lt;BR /&gt;a1 200&lt;BR /&gt;a2 125&lt;BR /&gt;a2 150&lt;BR /&gt;a2 300&lt;BR /&gt;a2 400&lt;/P&gt;&lt;P&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can anyone help me how can I get the&amp;nbsp; below output .&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Output :&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID number&amp;nbsp; c1&amp;nbsp; &amp;nbsp;c2&amp;nbsp; &amp;nbsp;c3&lt;BR /&gt;a1 100&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&amp;nbsp;&amp;nbsp;&lt;BR /&gt;a1 150&amp;nbsp; &amp;nbsp; &amp;nbsp;100&lt;BR /&gt;a1 200&amp;nbsp; &amp;nbsp; &amp;nbsp;150 100&lt;BR /&gt;a2 125&amp;nbsp; &amp;nbsp; &amp;nbsp; .&lt;BR /&gt;a2 150&amp;nbsp; &amp;nbsp; 125&lt;BR /&gt;a2 300&amp;nbsp; &amp;nbsp; 150 125&lt;BR /&gt;a2 400&amp;nbsp; &amp;nbsp; 300 150 125&lt;/P&gt;</description>
      <pubDate>Tue, 28 Nov 2017 10:19:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/LAG-function/m-p/416628#M102313</guid>
      <dc:creator>Manikanta</dc:creator>
      <dc:date>2017-11-28T10:19:06Z</dc:date>
    </item>
    <item>
      <title>Re: LAG function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/LAG-function/m-p/416631#M102314</link>
      <description>this can be achieved by using lag function and first.&lt;BR /&gt;some thing like this&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;data two;&lt;BR /&gt;set one;&lt;BR /&gt;by id;&lt;BR /&gt;c1=lag(number);&lt;BR /&gt;if first.id then c1=.;&lt;BR /&gt;run;&lt;BR /&gt;proc sort data=two; by id number c1;run;&lt;BR /&gt;data two;&lt;BR /&gt;set two;&lt;BR /&gt;by id number c1;&lt;BR /&gt;c2=lag(c1);&lt;BR /&gt;if first.id then c2=.;&lt;BR /&gt;run;&lt;BR /&gt;proc sort data=two; by id number c1 c2;run;&lt;BR /&gt;data two;&lt;BR /&gt;set two;&lt;BR /&gt;by id number c1 c2;&lt;BR /&gt;c3=lag(c2);&lt;BR /&gt;if first.id then c3=.;&lt;BR /&gt;run;</description>
      <pubDate>Tue, 28 Nov 2017 10:40:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/LAG-function/m-p/416631#M102314</guid>
      <dc:creator>RM6</dc:creator>
      <dc:date>2017-11-28T10:40:16Z</dc:date>
    </item>
    <item>
      <title>Re: LAG function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/LAG-function/m-p/416640#M102315</link>
      <description>&lt;P&gt;How do you know the maximum number of rows per id in dataset one?&lt;/P&gt;</description>
      <pubDate>Tue, 28 Nov 2017 11:33:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/LAG-function/m-p/416640#M102315</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-11-28T11:33:44Z</dc:date>
    </item>
    <item>
      <title>Re: LAG function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/LAG-function/m-p/416641#M102316</link>
      <description>Thanks for your reply .&lt;BR /&gt;But what if we have more data .&lt;BR /&gt;For example :&lt;BR /&gt;a1 3&lt;BR /&gt;a1 4&lt;BR /&gt;a1 5&lt;BR /&gt;a1 6&lt;BR /&gt;a1 7&lt;BR /&gt;a1 8&lt;BR /&gt;a1 9.&lt;BR /&gt;.&lt;BR /&gt;.&lt;BR /&gt;.&lt;BR /&gt;N&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Tue, 28 Nov 2017 11:36:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/LAG-function/m-p/416641#M102316</guid>
      <dc:creator>Manikanta</dc:creator>
      <dc:date>2017-11-28T11:36:07Z</dc:date>
    </item>
    <item>
      <title>Re: LAG function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/LAG-function/m-p/416646#M102320</link>
      <description>&lt;P&gt;Determine the number of needed variables first, and then use array processing:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data one;
input ID $ number ;
datalines;
a1 100
a1 150
a1 200
a2 125
a2 150
a2 300
a2 400
a3 3
a3 4
a3 5
a3 6
a3 7
a3 8
a3 9
;
run;

proc sql noprint;
select max(counter) - 1 into :counter from (select count(*) as counter from one group by id);
quit;

%let counter=&amp;amp;counter; *removes blanks;

data want;
set one;
by id;
array targets {&amp;amp;counter} c1-c&amp;amp;counter;
retain c1-c&amp;amp;counter;
if first.id
then do;
  i = 1;
  do i1 = 1 to &amp;amp;counter;
    targets{i1} = .;
  end;
end;
else i + 1;
do i1 = i-1 to 2 by -1;
  targets{i1} = targets{i1-1};
end;
targets{1} = lag(number);
if first.id then targets{1} = .;
drop i i1;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I added your second data example as group "a3".&lt;/P&gt;</description>
      <pubDate>Tue, 28 Nov 2017 11:56:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/LAG-function/m-p/416646#M102320</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-11-28T11:56:16Z</dc:date>
    </item>
    <item>
      <title>Re: LAG function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/LAG-function/m-p/416651#M102323</link>
      <description>Many Thanks.</description>
      <pubDate>Tue, 28 Nov 2017 12:03:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/LAG-function/m-p/416651#M102323</guid>
      <dc:creator>Manikanta</dc:creator>
      <dc:date>2017-11-28T12:03:55Z</dc:date>
    </item>
    <item>
      <title>Re: LAG function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/LAG-function/m-p/416673#M102331</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data one;
input ID $ number ;
datalines;
a1 100
a1 150
a1 200
a2 125
a2 150
a2 300
a2 400
;
run;

data want;
 set one;
 lag=lag(number);
 lag2=lag2(number);
 if id ne lag(id) then call missing(lag);
 if id ne lag2(id) then call missing(lag2);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 28 Nov 2017 13:16:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/LAG-function/m-p/416673#M102331</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2017-11-28T13:16:51Z</dc:date>
    </item>
  </channel>
</rss>

