<?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 and first functions to create a new variable in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Lag-and-first-functions-to-create-a-new-variable/m-p/504854#M1038</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;

input id old_varaible;

datalines;
1 1
1 1
1 2
1 3
1 3
1 99
2 99
2 99
2 99
3 3
3 3
3 2
3 3
3 99
4 1
4 1
4 1
4 2
4 2
4 2
4 2
4 2
4 2
;

run;

data want;
set have;
by id;
new=ifn(first.id,9999,lag(old_varaible));
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 16 Oct 2018 19:42:24 GMT</pubDate>
    <dc:creator>novinosrin</dc:creator>
    <dc:date>2018-10-16T19:42:24Z</dc:date>
    <item>
      <title>Lag and first functions to create a new variable</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Lag-and-first-functions-to-create-a-new-variable/m-p/504827#M1033</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need to code a new_variable that has a first value that I determine and the others values use the information found one line above for another variable (old_variable) for the same ID variable. My dataset is organized&amp;nbsp;in a longitudinal way.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here it is an example of the structure of my dataset:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data have;&lt;/P&gt;&lt;P&gt;input id old_varaible;&lt;/P&gt;&lt;P&gt;datalines;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1 1&lt;/P&gt;&lt;P&gt;1 1&lt;/P&gt;&lt;P&gt;1 2&lt;/P&gt;&lt;P&gt;1 3&lt;/P&gt;&lt;P&gt;1 3&lt;/P&gt;&lt;P&gt;1 99&lt;/P&gt;&lt;P&gt;2 99&lt;/P&gt;&lt;P&gt;2 99&lt;/P&gt;&lt;P&gt;2 99&lt;/P&gt;&lt;P&gt;3 3&lt;/P&gt;&lt;P&gt;3 3&lt;/P&gt;&lt;P&gt;3 2&lt;/P&gt;&lt;P&gt;3 3&lt;/P&gt;&lt;P&gt;3 99&lt;/P&gt;&lt;P&gt;4 1&lt;/P&gt;&lt;P&gt;4 1&lt;/P&gt;&lt;P&gt;4 1&lt;/P&gt;&lt;P&gt;4 2&lt;/P&gt;&lt;P&gt;4 2&lt;/P&gt;&lt;P&gt;4 2&lt;/P&gt;&lt;P&gt;4 2&lt;/P&gt;&lt;P&gt;4 2&lt;/P&gt;&lt;P&gt;4 2&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have tried to code like this, but it does not work:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data want;&lt;/P&gt;&lt;P&gt;set have;&lt;/P&gt;&lt;P&gt;new_variable = .;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;if first.id and first.old_variable then new_variable = 9999;&lt;/P&gt;&lt;P&gt;if id = lag(id) then new_variable = lag(old_variable);&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I want as a dataset would be:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data want;&lt;/P&gt;&lt;P&gt;set have;&lt;/P&gt;&lt;P&gt;input id old_varaible new_variable;&lt;/P&gt;&lt;P&gt;datalines;&lt;/P&gt;&lt;P&gt;1 1 9999&lt;/P&gt;&lt;P&gt;1 1 1&lt;/P&gt;&lt;P&gt;1 2 1&lt;/P&gt;&lt;P&gt;1 3 2&lt;/P&gt;&lt;P&gt;1 3 3&lt;/P&gt;&lt;P&gt;1 99 3&lt;/P&gt;&lt;P&gt;2 99 9999&lt;/P&gt;&lt;P&gt;2 99 99&lt;/P&gt;&lt;P&gt;2 99 99&lt;/P&gt;&lt;P&gt;3 3 9999&lt;/P&gt;&lt;P&gt;3 3 3&lt;/P&gt;&lt;P&gt;3 2 3&lt;/P&gt;&lt;P&gt;3 3 2&lt;/P&gt;&lt;P&gt;3 99 3&lt;/P&gt;&lt;P&gt;4 1 9999&lt;/P&gt;&lt;P&gt;4 1 1&lt;/P&gt;&lt;P&gt;4 1 1&lt;/P&gt;&lt;P&gt;4 2 1&lt;/P&gt;&lt;P&gt;4 2 2&lt;/P&gt;&lt;P&gt;4 2 2&lt;/P&gt;&lt;P&gt;4 2 2&lt;/P&gt;&lt;P&gt;4 2 2&lt;/P&gt;&lt;P&gt;4 2 2&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Does anyone have any clue ?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you!&lt;/P&gt;</description>
      <pubDate>Tue, 16 Oct 2018 18:58:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Lag-and-first-functions-to-create-a-new-variable/m-p/504827#M1033</guid>
      <dc:creator>MFraga</dc:creator>
      <dc:date>2018-10-16T18:58:03Z</dc:date>
    </item>
    <item>
      <title>Re: Lag and first functions to create a new variable</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Lag-and-first-functions-to-create-a-new-variable/m-p/504848#M1036</link>
      <description>&lt;P&gt;Over 90 percent of the time attempts to use LAG variables after IF do not work. Read up on the separate queue nature of the lagged variable values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Since your data is not sorted by ID and Old_variable your shown result does not match a requirement to use first.old_variable&lt;/P&gt;
&lt;P&gt;(the ID=3 Old_variable=2)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Try RETAIN to keep a current value available for the next.&lt;/P&gt;
&lt;PRE&gt;data want;
   set have;
   by id ;
   retain rvar;
   if first.id  then new_variable = 9999;
   Else  new_variable=rvar;
   rvar=old_variable;
   drop rvar;
run;&lt;/PRE&gt;</description>
      <pubDate>Tue, 16 Oct 2018 19:35:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Lag-and-first-functions-to-create-a-new-variable/m-p/504848#M1036</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-10-16T19:35:03Z</dc:date>
    </item>
    <item>
      <title>Re: Lag and first functions to create a new variable</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Lag-and-first-functions-to-create-a-new-variable/m-p/504850#M1037</link>
      <description>&lt;P&gt;something like this.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
by id;
new_variable = lag(old_varaible);
if first.id then new_variable =9999;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 16 Oct 2018 19:37:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Lag-and-first-functions-to-create-a-new-variable/m-p/504850#M1037</guid>
      <dc:creator>kiranv_</dc:creator>
      <dc:date>2018-10-16T19:37:11Z</dc:date>
    </item>
    <item>
      <title>Re: Lag and first functions to create a new variable</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Lag-and-first-functions-to-create-a-new-variable/m-p/504854#M1038</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;

input id old_varaible;

datalines;
1 1
1 1
1 2
1 3
1 3
1 99
2 99
2 99
2 99
3 3
3 3
3 2
3 3
3 99
4 1
4 1
4 1
4 2
4 2
4 2
4 2
4 2
4 2
;

run;

data want;
set have;
by id;
new=ifn(first.id,9999,lag(old_varaible));
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 16 Oct 2018 19:42:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Lag-and-first-functions-to-create-a-new-variable/m-p/504854#M1038</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-10-16T19:42:24Z</dc:date>
    </item>
    <item>
      <title>Re: Lag and first functions to create a new variable</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Lag-and-first-functions-to-create-a-new-variable/m-p/504858#M1039</link>
      <description>&lt;P&gt;very sleek answer&lt;/P&gt;</description>
      <pubDate>Tue, 16 Oct 2018 19:52:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Lag-and-first-functions-to-create-a-new-variable/m-p/504858#M1039</guid>
      <dc:creator>kiranv_</dc:creator>
      <dc:date>2018-10-16T19:52:46Z</dc:date>
    </item>
    <item>
      <title>Re: Lag and first functions to create a new variable</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Lag-and-first-functions-to-create-a-new-variable/m-p/504859#M1040</link>
      <description>&lt;P&gt;Thanks:)&lt;/P&gt;</description>
      <pubDate>Tue, 16 Oct 2018 19:53:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Lag-and-first-functions-to-create-a-new-variable/m-p/504859#M1040</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-10-16T19:53:08Z</dc:date>
    </item>
  </channel>
</rss>

