<?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: How do I RETAIN/LAG a String variable by ID? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-RETAIN-LAG-a-String-variable-by-ID/m-p/781113#M248945</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA WORK.have; 
Infile datalines delimiter=','; 
INPUT ID $	CHANNEL $;
CARDS;
123ABC,Service,
123ABC,Online,
123ABC,Retail,
456EFG,Stores,
456EFG,Retail,
789HIJ,Service,
789HIJ,Other,
789HIJ,Retail,
012KLM,Online,
012KLM,Retail,
;

data want;
    set have;
    by id notsorted;
    prev_channel=lag(channel);
    if first.id then desired_values=channel;
    else desired_values=prev_channel;
    drop prev_channel;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 18 Nov 2021 19:39:02 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2021-11-18T19:39:02Z</dc:date>
    <item>
      <title>How do I RETAIN/LAG a String variable by ID?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-RETAIN-LAG-a-String-variable-by-ID/m-p/781111#M248943</link>
      <description>&lt;P&gt;&lt;FONT face="arial,helvetica,sans-serif" size="3"&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;How should I Write the code to get the desired output like WORK.want?&lt;/P&gt;
&lt;P&gt;IF FIRST.ID THEN&amp;nbsp;Desired_values =&amp;nbsp;CHANNEL;&lt;/P&gt;
&lt;P&gt;ELSE&amp;nbsp;Desired_values = lag(CHANNEL);&lt;BR /&gt;BY ID;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="arial,helvetica,sans-serif" size="3"&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="arial,helvetica,sans-serif" size="3"&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="arial,helvetica,sans-serif" size="3"&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="arial,helvetica,sans-serif" size="3"&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="arial,helvetica,sans-serif" size="3"&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA WORK.have; 
Infile datalines delimiter=','; 
INPUT ID $	CHANNEL $;
;
LENGTH CHANNEL $50;
CARDS;
123ABC,Service,
123ABC,Online,
123ABC,Retail,
456EFG,Stores,
456EFG,Retail,
789HIJ,Service,
789HIJ,Other,
789HIJ,Retail,
012KLM,Online,
012KLM,Retail,
;
RUN;
DATA work.want;
Infile datalines delimiter=','; 
INPUT ID $	CHANNEL $ Desired_values $;
;
LENGTH CHANNEL Desired_values $50;
CARDS;
123ABC,Service,Service
123ABC,Online,Service
123ABC,Retail,Online
456EFG,Stores,Stores
456EFG,Retail,Stores
789HIJ,Service,Service
789HIJ,Other,Service
789HIJ,Retail,Other
012KLM,Online,Online
012KLM,Retail,Online
;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 18 Nov 2021 19:26:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-RETAIN-LAG-a-String-variable-by-ID/m-p/781111#M248943</guid>
      <dc:creator>Pili1100</dc:creator>
      <dc:date>2021-11-18T19:26:05Z</dc:date>
    </item>
    <item>
      <title>Re: How do I RETAIN/LAG a String variable by ID?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-RETAIN-LAG-a-String-variable-by-ID/m-p/781113#M248945</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA WORK.have; 
Infile datalines delimiter=','; 
INPUT ID $	CHANNEL $;
CARDS;
123ABC,Service,
123ABC,Online,
123ABC,Retail,
456EFG,Stores,
456EFG,Retail,
789HIJ,Service,
789HIJ,Other,
789HIJ,Retail,
012KLM,Online,
012KLM,Retail,
;

data want;
    set have;
    by id notsorted;
    prev_channel=lag(channel);
    if first.id then desired_values=channel;
    else desired_values=prev_channel;
    drop prev_channel;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 18 Nov 2021 19:39:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-RETAIN-LAG-a-String-variable-by-ID/m-p/781113#M248945</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-11-18T19:39:02Z</dc:date>
    </item>
    <item>
      <title>Re: How do I RETAIN/LAG a String variable by ID?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-RETAIN-LAG-a-String-variable-by-ID/m-p/781115#M248946</link>
      <description>&lt;P&gt;Probably simplest to lag and then overwrite for the first one:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   set have;
   by id notsorted;
   desired_values = lag(channel);
   if first.id then desired_values = channel;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 18 Nov 2021 20:06:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-RETAIN-LAG-a-String-variable-by-ID/m-p/781115#M248946</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2021-11-18T20:06:27Z</dc:date>
    </item>
    <item>
      <title>Re: How do I RETAIN/LAG a String variable by ID?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-RETAIN-LAG-a-String-variable-by-ID/m-p/781194#M248982</link>
      <description>&lt;P&gt;NEVER call the LAG function in a conditional branch, unless you perfectly know what you are doing.&lt;/P&gt;
&lt;P&gt;The LAG function creates a FIFO queue which is filled only when the function is called (not automatically with every data step iteration!).&lt;/P&gt;
&lt;P&gt;One method to make sure that a new value is entered into the queue is the use of IFC or IFN:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
by id;
desired_values = ifc(first.id,channel,lag(channel));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;because these functions evaluate ALL arguments before returning a value.&lt;/P&gt;</description>
      <pubDate>Fri, 19 Nov 2021 07:53:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-RETAIN-LAG-a-String-variable-by-ID/m-p/781194#M248982</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-11-19T07:53:26Z</dc:date>
    </item>
    <item>
      <title>Re: How do I RETAIN/LAG a String variable by ID?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-RETAIN-LAG-a-String-variable-by-ID/m-p/781895#M249212</link>
      <description>&lt;P&gt;Thx for solution &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 23 Nov 2021 10:00:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-RETAIN-LAG-a-String-variable-by-ID/m-p/781895#M249212</guid>
      <dc:creator>Pili1100</dc:creator>
      <dc:date>2021-11-23T10:00:20Z</dc:date>
    </item>
    <item>
      <title>Re: How do I RETAIN/LAG a String variable by ID?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-RETAIN-LAG-a-String-variable-by-ID/m-p/781896#M249213</link>
      <description>&lt;P&gt;Thx for solution&amp;nbsp; and for the heads up&amp;nbsp; when using LAG &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 23 Nov 2021 10:01:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-RETAIN-LAG-a-String-variable-by-ID/m-p/781896#M249213</guid>
      <dc:creator>Pili1100</dc:creator>
      <dc:date>2021-11-23T10:01:23Z</dc:date>
    </item>
  </channel>
</rss>

