<?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: LOCF and retain statement in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/LOCF-and-retain-statement/m-p/758936#M239714</link>
    <description>The way you used DROP statement before set statement is something new that i learnt today. &lt;span class="lia-unicode-emoji" title=":grinning_face_with_big_eyes:"&gt;😃&lt;/span&gt;&lt;BR /&gt;I usually use RETAIN before SET statement,  DROP - at the very end or as a dataset options..  My confusion about intermediate variables started from there and I thought _: as a temporary (automatic) variable...  &lt;BR /&gt;&lt;BR /&gt;#1 and #2 are familiar to me.</description>
    <pubDate>Tue, 03 Aug 2021 02:41:32 GMT</pubDate>
    <dc:creator>A_Kh</dc:creator>
    <dc:date>2021-08-03T02:41:32Z</dc:date>
    <item>
      <title>LOCF and retain statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/LOCF-and-retain-statement/m-p/758373#M239451</link>
      <description>&lt;P&gt;Hi all,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I need your assistance in resolving following issue:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Dataset given:&lt;/P&gt;
&lt;P&gt;SUBJID&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;OUTCOME&lt;/P&gt;
&lt;P&gt;111&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;negative&lt;/P&gt;
&lt;P&gt;111&lt;/P&gt;
&lt;P&gt;111&lt;/P&gt;
&lt;P&gt;222&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; positive&lt;/P&gt;
&lt;P&gt;222&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Expected result:&lt;/P&gt;
&lt;P&gt;SUBJID&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;OUTCOME&lt;/P&gt;
&lt;P&gt;111&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;negative&lt;/P&gt;
&lt;P&gt;111&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;negative&lt;/P&gt;
&lt;P&gt;111&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;negative&lt;/P&gt;
&lt;P&gt;222&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; positive&lt;/P&gt;
&lt;P&gt;222&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; positive&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I've been trying it with RETAIN and FIRST and LAST VARIABLES combination, but somehow could not get the desired result.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please share your experience and provide some guidance. Your support is highly appreciated!&lt;/P&gt;
&lt;P&gt;Thank you!&lt;/P&gt;</description>
      <pubDate>Fri, 30 Jul 2021 12:53:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/LOCF-and-retain-statement/m-p/758373#M239451</guid>
      <dc:creator>A_Kh</dc:creator>
      <dc:date>2021-07-30T12:53:09Z</dc:date>
    </item>
    <item>
      <title>Re: LOCF and retain statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/LOCF-and-retain-statement/m-p/758409#M239459</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data  have;
input SUBJID $ OUTCOME :$20.;
infile cards truncover;
cards;
111 negative
111
111
222 positive
222
;
run;


data want;
update have(obs=0) have;
by subjid;
output;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 30 Jul 2021 14:19:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/LOCF-and-retain-statement/m-p/758409#M239459</guid>
      <dc:creator>r_behata</dc:creator>
      <dc:date>2021-07-30T14:19:45Z</dc:date>
    </item>
    <item>
      <title>Re: LOCF and retain statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/LOCF-and-retain-statement/m-p/758436#M239476</link>
      <description>&lt;P&gt;Do you have any cases where you have multiple values of outcome for any given Subjid such as this?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Dataset given:&lt;/P&gt;
&lt;P&gt;SUBJID OUTCOME&lt;/P&gt;
&lt;P&gt;111 negative&lt;/P&gt;
&lt;P&gt;111&lt;/P&gt;
&lt;P&gt;111&amp;nbsp; positive&lt;/P&gt;
&lt;P&gt;111&lt;/P&gt;
&lt;P&gt;111&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If so what would be the desired result?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 30 Jul 2021 16:52:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/LOCF-and-retain-statement/m-p/758436#M239476</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-07-30T16:52:00Z</dc:date>
    </item>
    <item>
      <title>Re: LOCF and retain statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/LOCF-and-retain-statement/m-p/758498#M239492</link>
      <description>&lt;P&gt;The problem with RETAIN if used on Outcome is that when SAS reads the next Dataline, the prior value will be wiped out -- even if the new value is Missing (blank).&amp;nbsp; Therefore an intermediate/work variable needs to be used that will hold only non-blank values.&amp;nbsp; See example, below.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;'s question is important to consider though.&amp;nbsp; The example below will&amp;nbsp;&lt;EM&gt;replace&lt;/EM&gt; the previous Outcome if a different Outcome is read in that has the same SubjID.&amp;nbsp; This is the typical case.&amp;nbsp; However, if you do NOT want this behavior, you would have to modify the code below, probably with FIRST./LAST. type processing.&amp;nbsp; The code below assumes that the first occurrence of a given SubjID will have a non-blank Outcome.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Jim&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data  have;
	input SUBJID $ OUTCOME : $20.;
	infile cards truncover;
datalines;
111 negative
111
111
222 positive
222
;
run;


DATA	Want;
	DROP	_:;
	RETAIN	_Prior_Outcome;
	SET	Have;
	IF	MISSING(Outcome)	THEN
		Outcome			=	_Prior_Outcome;
	ELSE
		_Prior_Outcome	=	Outcome;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 30 Jul 2021 20:02:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/LOCF-and-retain-statement/m-p/758498#M239492</guid>
      <dc:creator>jimbarbour</dc:creator>
      <dc:date>2021-07-30T20:02:27Z</dc:date>
    </item>
    <item>
      <title>Re: LOCF and retain statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/LOCF-and-retain-statement/m-p/758898#M239700</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/223452"&gt;@r_behata&lt;/a&gt;&amp;nbsp;,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you so much, it works perfectly!&lt;/P&gt;</description>
      <pubDate>Mon, 02 Aug 2021 20:58:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/LOCF-and-retain-statement/m-p/758898#M239700</guid>
      <dc:creator>A_Kh</dc:creator>
      <dc:date>2021-08-02T20:58:55Z</dc:date>
    </item>
    <item>
      <title>Re: LOCF and retain statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/LOCF-and-retain-statement/m-p/758905#M239702</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you for checking in with me. There is not any case where OUTCOME is different by SUBJID. And I see two codes above work perfectly for solving my problem.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 02 Aug 2021 21:08:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/LOCF-and-retain-statement/m-p/758905#M239702</guid>
      <dc:creator>A_Kh</dc:creator>
      <dc:date>2021-08-02T21:08:31Z</dc:date>
    </item>
    <item>
      <title>Re: LOCF and retain statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/LOCF-and-retain-statement/m-p/758909#M239704</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/37107"&gt;@jimbarbour&lt;/a&gt;&amp;nbsp;,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you for explanation, that's very helpful. Your code is just awesome! I haven't seen intermediate variables like yours before (not considering first/last.variables, i and arrays..). Would you please give me references where I could learn these variables.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;As I already accepted the previous answer as solution I did not have a second chance to accept your code as the solution, but it did solve my problem. Thank you, again, for your time and guidance!&lt;/P&gt;</description>
      <pubDate>Mon, 02 Aug 2021 21:17:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/LOCF-and-retain-statement/m-p/758909#M239704</guid>
      <dc:creator>A_Kh</dc:creator>
      <dc:date>2021-08-02T21:17:45Z</dc:date>
    </item>
    <item>
      <title>Re: LOCF and retain statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/LOCF-and-retain-statement/m-p/758916#M239705</link>
      <description>&lt;P&gt;Well, there's really not too much to learn:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;For a numeric variable, just put it in your code.&amp;nbsp; If SAS encounters a new variable and there is no definition, SAS will create the variable for you and use 8. (numeric, 8 bytes of storage) as the definition.&lt;/LI&gt;
&lt;LI&gt;For a character variable, it's best to use a LENGTH statement otherwise SAS will use the first reference which can easily cause truncation.&amp;nbsp; However, as I recall, if you're using a function to create the value at first reference, the default is $200. which isn't too bad.&amp;nbsp; Still, I would eliminate this as a source of potential truncation and just use a LENGTH statement.&lt;/LI&gt;
&lt;LI&gt;I usually prefix my intermediate variables with an underscore and then code a "DROP _:" which will drop all variables that start with an underscore, but be careful here.&amp;nbsp; Some SAS procedures (Transpose, Compare, etc.) create variables that start with an underscore.&amp;nbsp; You may or may not want to drop those.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;Jim&lt;/P&gt;</description>
      <pubDate>Mon, 02 Aug 2021 22:07:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/LOCF-and-retain-statement/m-p/758916#M239705</guid>
      <dc:creator>jimbarbour</dc:creator>
      <dc:date>2021-08-02T22:07:35Z</dc:date>
    </item>
    <item>
      <title>Re: LOCF and retain statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/LOCF-and-retain-statement/m-p/758930#M239709</link>
      <description>&lt;P&gt;The solution provide by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/223452"&gt;@r_behata&lt;/a&gt;&amp;nbsp;is great for your stated problem.&amp;nbsp; But be aware of two issues:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;you need an explicit output statement.&amp;nbsp; Otherwise you would get only one observation per subjid:&lt;/LI&gt;
&lt;LI&gt;Missing values of all variables, not just OUTCOME will be replaced by preceding non-missing values.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;If #2 is a problem you want use a MERGE statement, as in:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data wantm;
  merge have (where=(outcome^=' ')) have (drop=outcome);
  by subjid;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This applies the implicit RETAIN just to the outcome variable.&amp;nbsp; Of course, it assumes you have only one non-missing instance of OUTCOME per SUBJID.&lt;/P&gt;</description>
      <pubDate>Tue, 03 Aug 2021 01:04:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/LOCF-and-retain-statement/m-p/758930#M239709</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2021-08-03T01:04:07Z</dc:date>
    </item>
    <item>
      <title>Re: LOCF and retain statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/LOCF-and-retain-statement/m-p/758934#M239712</link>
      <description>Hi &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;,&lt;BR /&gt;&lt;BR /&gt;I appreciate your feedback. Another good technique to solve my problem&lt;span class="lia-unicode-emoji" title=":grinning_face_with_big_eyes:"&gt;😃&lt;/span&gt;. &lt;BR /&gt;&lt;BR /&gt;I'll keep in mind the behavior of explicit output statement while using Update statement, vs implicit output when using Merge statement. &lt;BR /&gt;&lt;BR /&gt;Very helpful, thank you very much!</description>
      <pubDate>Tue, 03 Aug 2021 02:24:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/LOCF-and-retain-statement/m-p/758934#M239712</guid>
      <dc:creator>A_Kh</dc:creator>
      <dc:date>2021-08-03T02:24:56Z</dc:date>
    </item>
    <item>
      <title>Re: LOCF and retain statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/LOCF-and-retain-statement/m-p/758936#M239714</link>
      <description>The way you used DROP statement before set statement is something new that i learnt today. &lt;span class="lia-unicode-emoji" title=":grinning_face_with_big_eyes:"&gt;😃&lt;/span&gt;&lt;BR /&gt;I usually use RETAIN before SET statement,  DROP - at the very end or as a dataset options..  My confusion about intermediate variables started from there and I thought _: as a temporary (automatic) variable...  &lt;BR /&gt;&lt;BR /&gt;#1 and #2 are familiar to me.</description>
      <pubDate>Tue, 03 Aug 2021 02:41:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/LOCF-and-retain-statement/m-p/758936#M239714</guid>
      <dc:creator>A_Kh</dc:creator>
      <dc:date>2021-08-03T02:41:32Z</dc:date>
    </item>
    <item>
      <title>Re: LOCF and retain statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/LOCF-and-retain-statement/m-p/758940#M239717</link>
      <description>&lt;P&gt;DROP is actually not executed during the run.&amp;nbsp; DROP is processed at compile time.&amp;nbsp; So, it really doesn't matter where you put the DROP statement in the program (as long as it is valid SAS syntax).&amp;nbsp; I usually put the DROP near the top because that's typically where I put the SET, LENGTH, and DATA statements that create or bring in variables.&amp;nbsp; That way I have all my variable related actions in close proximity for ease of reference.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Jim&lt;/P&gt;</description>
      <pubDate>Tue, 03 Aug 2021 03:24:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/LOCF-and-retain-statement/m-p/758940#M239717</guid>
      <dc:creator>jimbarbour</dc:creator>
      <dc:date>2021-08-03T03:24:21Z</dc:date>
    </item>
  </channel>
</rss>

