<?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 to keep the previous value to the current one? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-keep-the-previous-value-to-the-current-one/m-p/907342#M358172</link>
    <description>&lt;P&gt;Besides abovementioned comments, please note that LAG function works as long as your data structure is '&lt;EM&gt;one record per visit&lt;/EM&gt;'.&amp;nbsp; If your data is already in SDTM structure which assumes (FINDINGS DOMAINS) to be one record per --TEST, per visit, then you might need to use RETAIN statement to perform LOCF properly.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Below is an example of lag function (prev_folder_lag) and retain statements(prev_folder_retain):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id $ vstestcd $ folder $;
cards;
100011 PULSE week1 
100011 TEMP week1 
100011 WEIGHT week1 
100011 PULSE week2 
100011 TEMP week2 
100011 WEIGHT week2 
100011 PULSE SRV 
100011 TEMP SRV 
100011 WEIGHT SRV 
;
data want;
	set have;
	prev_folder_lag= lag(folder);
	length prev_folder_retain $14;
	retain prev_folder_retain;
	if upcase(folder) ne 'SRV' then prev_folder_retain= folder;
run; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 11 Dec 2023 15:05:24 GMT</pubDate>
    <dc:creator>A_Kh</dc:creator>
    <dc:date>2023-12-11T15:05:24Z</dc:date>
    <item>
      <title>How to keep the previous value to the current one?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-keep-the-previous-value-to-the-current-one/m-p/907289#M358152</link>
      <description>&lt;P&gt;Hi guys! I am doing one sdtm project question,&lt;/P&gt;&lt;P&gt;there is one variable "folder" to keep the visiting week values like "week1" "screening" "week5";&amp;nbsp; also, we want to create a new variable "visitnum" to extract the numbers of folder, like if folder=week5, we keep 5 for the visitnum;&lt;/P&gt;&lt;P&gt;when the folder="SRV", we want to keep the previous value, like for observation80, we have folder="SRV", so we keep the previous obs(79) folder="week7", and we extract&amp;nbsp; 7 for "visitnum";&lt;/P&gt;&lt;P&gt;I am trying to use RETAIN function to keep the previous value, but still get error.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can you help me to fix it? Thank!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Screenshot 2023-12-11 at 3.59.04 AM.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/91151i3562ED152F908A5B/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Screenshot 2023-12-11 at 3.59.04 AM.png" alt="Screenshot 2023-12-11 at 3.59.04 AM.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt; &lt;/P&gt;</description>
      <pubDate>Mon, 11 Dec 2023 09:00:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-keep-the-previous-value-to-the-current-one/m-p/907289#M358152</guid>
      <dc:creator>Sikcion</dc:creator>
      <dc:date>2023-12-11T09:00:12Z</dc:date>
    </item>
    <item>
      <title>Re: How to keep the previous value to the current one?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-keep-the-previous-value-to-the-current-one/m-p/907297#M358155</link>
      <description>Please post the saslog from the start with code, warnings and errors.</description>
      <pubDate>Mon, 11 Dec 2023 10:19:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-keep-the-previous-value-to-the-current-one/m-p/907297#M358155</guid>
      <dc:creator>JosvanderVelden</dc:creator>
      <dc:date>2023-12-11T10:19:55Z</dc:date>
    </item>
    <item>
      <title>Re: How to keep the previous value to the current one?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-keep-the-previous-value-to-the-current-one/m-p/907314#M358160</link>
      <description>&lt;P&gt;General rule on this forum when asking about errors or requesting suggestions for code: Copy the text from the log including the code and any notes, messages, warnings or errors and then on the forum open a text box using the &amp;lt;/&amp;gt; icon that appears above the message window. Paste the text.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The text box means that we can copy text and edit it to make suggestions. The LOG means that we have all the text of the errors and any diagnostic information that SAS often supplies with errors or warnings and know more of what happens. Most of us are unwilling to retype from scratch large amounts of code to make small change to a program.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You have at least one logic error: you never assign a value to Prev_folder.&lt;/P&gt;
&lt;P&gt;I suspect you may have intended to have a :&lt;/P&gt;
&lt;P&gt;PREV_FOLDER = lag(Folder);&lt;/P&gt;
&lt;P&gt;immediately before the "If first.vetest" to actually get the value of the previous folder.&lt;/P&gt;
&lt;P&gt;Second, since you use Retain that way the variable Prev_folder has been defined as numeric by default. If you want it to hold character values then you want to assign the value as Character &lt;STRONG&gt;before&lt;/STRONG&gt; the Retain with something like:&lt;/P&gt;
&lt;P&gt;Length Prev_folder $ 15;&lt;/P&gt;
&lt;P&gt;Or look up the syntax for Retain to set a default value of Prev_folder that can be used to set the length.&lt;/P&gt;
&lt;P&gt;the number needs to be large enough to hold the longest expected value of Folder.&lt;/P&gt;</description>
      <pubDate>Mon, 11 Dec 2023 12:15:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-keep-the-previous-value-to-the-current-one/m-p/907314#M358160</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-12-11T12:15:35Z</dc:date>
    </item>
    <item>
      <title>Re: How to keep the previous value to the current one?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-keep-the-previous-value-to-the-current-one/m-p/907317#M358162</link>
      <description>A couple of issues stand out but there are probably many more.  So you will need to clean these up before (as others have rightfully suggested) posting the log.&lt;BR /&gt;&lt;BR /&gt;First, your program refers to first.vetest.  But nothing in your program creates first.vetest.  Where does it come from?&lt;BR /&gt;&lt;BR /&gt;And second, where does prev_folder come from?  What does it contain?</description>
      <pubDate>Mon, 11 Dec 2023 12:38:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-keep-the-previous-value-to-the-current-one/m-p/907317#M358162</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2023-12-11T12:38:39Z</dc:date>
    </item>
    <item>
      <title>Re: How to keep the previous value to the current one?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-keep-the-previous-value-to-the-current-one/m-p/907319#M358163</link>
      <description>&lt;P&gt;Hi ballardw! Thanks for your replay, I add&amp;nbsp;&lt;SPAN&gt;PREV_FOLDER = lag(Folder) and it worked!&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I used to think that we can only use lag for numerical variables.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 11 Dec 2023 12:52:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-keep-the-previous-value-to-the-current-one/m-p/907319#M358163</guid>
      <dc:creator>Sikcion</dc:creator>
      <dc:date>2023-12-11T12:52:36Z</dc:date>
    </item>
    <item>
      <title>Re: How to keep the previous value to the current one?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-keep-the-previous-value-to-the-current-one/m-p/907342#M358172</link>
      <description>&lt;P&gt;Besides abovementioned comments, please note that LAG function works as long as your data structure is '&lt;EM&gt;one record per visit&lt;/EM&gt;'.&amp;nbsp; If your data is already in SDTM structure which assumes (FINDINGS DOMAINS) to be one record per --TEST, per visit, then you might need to use RETAIN statement to perform LOCF properly.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Below is an example of lag function (prev_folder_lag) and retain statements(prev_folder_retain):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id $ vstestcd $ folder $;
cards;
100011 PULSE week1 
100011 TEMP week1 
100011 WEIGHT week1 
100011 PULSE week2 
100011 TEMP week2 
100011 WEIGHT week2 
100011 PULSE SRV 
100011 TEMP SRV 
100011 WEIGHT SRV 
;
data want;
	set have;
	prev_folder_lag= lag(folder);
	length prev_folder_retain $14;
	retain prev_folder_retain;
	if upcase(folder) ne 'SRV' then prev_folder_retain= folder;
run; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 11 Dec 2023 15:05:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-keep-the-previous-value-to-the-current-one/m-p/907342#M358172</guid>
      <dc:creator>A_Kh</dc:creator>
      <dc:date>2023-12-11T15:05:24Z</dc:date>
    </item>
    <item>
      <title>Re: How to keep the previous value to the current one?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-keep-the-previous-value-to-the-current-one/m-p/907470#M358202</link>
      <description>&lt;P&gt;Yes, LAG function can be applied to both numeric and character variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But the related DIF function can only be used with numeric variables (although SAS will try convert numeric text to numeric values for use by DIF).&lt;/P&gt;</description>
      <pubDate>Tue, 12 Dec 2023 05:32:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-keep-the-previous-value-to-the-current-one/m-p/907470#M358202</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2023-12-12T05:32:16Z</dc:date>
    </item>
  </channel>
</rss>

