<?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 How to retain a value from a separate dataset within a data step? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-retain-a-value-from-a-separate-dataset-within-a-data-step/m-p/363172#M85930</link>
    <description>&lt;P&gt;I need to retain a value from a separate dataset within a data step:&lt;BR /&gt;　&lt;/P&gt;&lt;P&gt;Here is what I have:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;message&amp;nbsp;&amp;nbsp; value&lt;BR /&gt;new_good 0.5&lt;BR /&gt;retain&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .&lt;BR /&gt;new_good 0.7&lt;BR /&gt;retain&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .&lt;BR /&gt;retain&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;.&lt;BR /&gt;new_bad&amp;nbsp;&amp;nbsp; 0.6&lt;BR /&gt;retain&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .&lt;BR /&gt;new_good 0.3&lt;BR /&gt;retain&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .&lt;BR /&gt;retain&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .&lt;BR /&gt;retain&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;and I also have a separate dataset:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data ultimately_good_value;&lt;BR /&gt;x=1;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This dataset is generated by a macro that is invoked through call execute in each instance where the message is new_bad. I do not include this macro for simplicity.&lt;BR /&gt;　&lt;/P&gt;&lt;P&gt;This&amp;nbsp;is what I would like to have:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;message&amp;nbsp;&amp;nbsp; value&amp;nbsp;&amp;nbsp; true_value&lt;BR /&gt;new_good 0.5&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0.5&lt;BR /&gt;retain&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0.5&lt;BR /&gt;new_good 0.7&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0.7&lt;BR /&gt;retain&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0.7&lt;BR /&gt;retain&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0.7&lt;BR /&gt;new_bad&amp;nbsp;&amp;nbsp; 0.6&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&lt;BR /&gt;retain&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&lt;BR /&gt;new_good 0.3&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0.3&lt;BR /&gt;retain&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;0.3&lt;BR /&gt;retain&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0.3&lt;BR /&gt;retain&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0.3&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;So when the message is new_good or retain I want to retain the true_value as value. But when the message is new_bad I want to retain value x from the dataset ultimately_good_value.&lt;/P&gt;&lt;P&gt;　&lt;/P&gt;&lt;P&gt;Here is my code:&lt;/P&gt;&lt;P&gt;　&lt;/P&gt;&lt;PRE&gt;data values;
infile datalines; 
input message $ value;
datalines; 
new_good 0.5
retain .
new_good 0.7
retain .
retain .
new_bad 0.6
retain .
new_good 0.3
retain .
retain .
retain .
;
run;

data ultimately_good_value;
x=1;
run;

*My intuition is that the solution lies in a call execute statement with proc sql inside;

data values_want;
set values; 
&lt;BR /&gt;retain true_value . ;

if message = "new_good" then true_value=value;

if message = "new_bad" then
true_value = call execute ("proc sql; select x from ultimately_good_value; quit; run;");

run;


*But since I do not see how to replace the true_value in the dataset with the outcome of &lt;BR /&gt;the proc execute statement, this approach does not work;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would appreciate any insight.&lt;/P&gt;</description>
    <pubDate>Wed, 31 May 2017 17:44:17 GMT</pubDate>
    <dc:creator>Financier</dc:creator>
    <dc:date>2017-05-31T17:44:17Z</dc:date>
    <item>
      <title>How to retain a value from a separate dataset within a data step?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-retain-a-value-from-a-separate-dataset-within-a-data-step/m-p/363172#M85930</link>
      <description>&lt;P&gt;I need to retain a value from a separate dataset within a data step:&lt;BR /&gt;　&lt;/P&gt;&lt;P&gt;Here is what I have:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;message&amp;nbsp;&amp;nbsp; value&lt;BR /&gt;new_good 0.5&lt;BR /&gt;retain&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .&lt;BR /&gt;new_good 0.7&lt;BR /&gt;retain&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .&lt;BR /&gt;retain&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;.&lt;BR /&gt;new_bad&amp;nbsp;&amp;nbsp; 0.6&lt;BR /&gt;retain&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .&lt;BR /&gt;new_good 0.3&lt;BR /&gt;retain&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .&lt;BR /&gt;retain&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .&lt;BR /&gt;retain&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;and I also have a separate dataset:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data ultimately_good_value;&lt;BR /&gt;x=1;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This dataset is generated by a macro that is invoked through call execute in each instance where the message is new_bad. I do not include this macro for simplicity.&lt;BR /&gt;　&lt;/P&gt;&lt;P&gt;This&amp;nbsp;is what I would like to have:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;message&amp;nbsp;&amp;nbsp; value&amp;nbsp;&amp;nbsp; true_value&lt;BR /&gt;new_good 0.5&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0.5&lt;BR /&gt;retain&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0.5&lt;BR /&gt;new_good 0.7&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0.7&lt;BR /&gt;retain&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0.7&lt;BR /&gt;retain&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0.7&lt;BR /&gt;new_bad&amp;nbsp;&amp;nbsp; 0.6&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&lt;BR /&gt;retain&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&lt;BR /&gt;new_good 0.3&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0.3&lt;BR /&gt;retain&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;0.3&lt;BR /&gt;retain&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0.3&lt;BR /&gt;retain&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0.3&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;So when the message is new_good or retain I want to retain the true_value as value. But when the message is new_bad I want to retain value x from the dataset ultimately_good_value.&lt;/P&gt;&lt;P&gt;　&lt;/P&gt;&lt;P&gt;Here is my code:&lt;/P&gt;&lt;P&gt;　&lt;/P&gt;&lt;PRE&gt;data values;
infile datalines; 
input message $ value;
datalines; 
new_good 0.5
retain .
new_good 0.7
retain .
retain .
new_bad 0.6
retain .
new_good 0.3
retain .
retain .
retain .
;
run;

data ultimately_good_value;
x=1;
run;

*My intuition is that the solution lies in a call execute statement with proc sql inside;

data values_want;
set values; 
&lt;BR /&gt;retain true_value . ;

if message = "new_good" then true_value=value;

if message = "new_bad" then
true_value = call execute ("proc sql; select x from ultimately_good_value; quit; run;");

run;


*But since I do not see how to replace the true_value in the dataset with the outcome of &lt;BR /&gt;the proc execute statement, this approach does not work;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would appreciate any insight.&lt;/P&gt;</description>
      <pubDate>Wed, 31 May 2017 17:44:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-retain-a-value-from-a-separate-dataset-within-a-data-step/m-p/363172#M85930</guid>
      <dc:creator>Financier</dc:creator>
      <dc:date>2017-05-31T17:44:17Z</dc:date>
    </item>
    <item>
      <title>Re: How to retain a value from a separate dataset within a data step?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-retain-a-value-from-a-separate-dataset-within-a-data-step/m-p/363174#M85932</link>
      <description>&lt;P&gt;I don't understand your example. If you want to combine data from two tables then merge them or use SQL join.&lt;/P&gt;</description>
      <pubDate>Wed, 31 May 2017 17:49:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-retain-a-value-from-a-separate-dataset-within-a-data-step/m-p/363174#M85932</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-05-31T17:49:38Z</dc:date>
    </item>
    <item>
      <title>Re: How to retain a value from a separate dataset within a data step?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-retain-a-value-from-a-separate-dataset-within-a-data-step/m-p/363186#M85938</link>
      <description>&lt;P&gt;Thanks for reply!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Unfortunately,&amp;nbsp;I can't simply merge them because the data ultimately_good_value does not exist at the beginning of the data step in my original code. I just assume that it exists here&amp;nbsp;to be more&amp;nbsp;clear what exactly&amp;nbsp;I need to do.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is the code that does not assume that ultimately_good_value&amp;nbsp;exists at the beginning of data step execution:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data values;
infile datalines; 
input message $ value;
datalines;                      
new_good 0.5
retain   .
new_good 0.7
retain   .
retain   .
new_bad  0.6
retain   .
new_good 0.3
retain   .
retain   .
retain   .
;
run;


%macro X;
data ultimately_good_value;
x=1;
run;
%mend;


data values_want;
set values; 

retain true_value .;

if message = "new_good" then true_value=value;

if message = "new_bad" then 

true_value = call execute ("%X;");

run;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So when the message is "new_bad" the code should do two things:&lt;/P&gt;&lt;P&gt;First,&amp;nbsp;invoke a macro that generates a dataset ultimately_good_value&amp;nbsp;(this macro depends on many things, including the current retained true_value and other variables, but for simplicity I just set x=1 here).&lt;/P&gt;&lt;P&gt;Second,&amp;nbsp;replace the currently retained true_value with the value x&amp;nbsp;from the newly created dataset while retain statement is running.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I do not know how to do the second step.&lt;/P&gt;</description>
      <pubDate>Wed, 31 May 2017 19:00:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-retain-a-value-from-a-separate-dataset-within-a-data-step/m-p/363186#M85938</guid>
      <dc:creator>Financier</dc:creator>
      <dc:date>2017-05-31T19:00:44Z</dc:date>
    </item>
    <item>
      <title>Re: How to retain a value from a separate dataset within a data step?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-retain-a-value-from-a-separate-dataset-within-a-data-step/m-p/363189#M85940</link>
      <description>&lt;P&gt;While it may be possible to get the result you want, it would not be possible to following the programming path you describe.&amp;nbsp; SAS is not able to put a DATA step on hold, switch over to another DATA step, do some computations, then switch back to the original DATA step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You don't show some of the details, so it's not possible to tell if this would work for you, but it should at least be considered.&amp;nbsp; Have your DATA step compute the different possible values for X:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data values_want;&lt;/P&gt;
&lt;P&gt;if _n_=1 then do;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; *** calculations for every possible value for X, using as many variable names as needed to hold them all;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;set values;&lt;/P&gt;
&lt;P&gt;...&lt;/P&gt;
&lt;P&gt;if message = "new_bad" then do;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; *** figure out which of the previously calculated values to use;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;</description>
      <pubDate>Wed, 31 May 2017 19:21:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-retain-a-value-from-a-separate-dataset-within-a-data-step/m-p/363189#M85940</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-05-31T19:21:03Z</dc:date>
    </item>
    <item>
      <title>Re: How to retain a value from a separate dataset within a data step?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-retain-a-value-from-a-separate-dataset-within-a-data-step/m-p/363219#M85952</link>
      <description>&lt;P&gt;It helps to think it terms of operations that can be applied to full datasets rather than thinking in terms of step-by-step instructions. &amp;nbsp;SAS is not really an asembly language programming system.&lt;/P&gt;
&lt;P&gt;So given this description:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;So when the message is "new_bad" the code should do two things:
First, invoke a macro that generates a dataset ultimately_good_value 
(this macro depends on many things, including the current retained
true_value and other variables, but for simplicity I just set x=1 here).

Second, replace the currently retained true_value with the value x 
from the newly created dataset while retain statement is running.
&lt;/PRE&gt;
&lt;P&gt;I would turn it around and say first calculate the "ultimately_good_value" for every observation (or at least those that will need it) and then when the current value is "bad" then use the "ultimately_good_value". &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or perhaps you want to split the data into good and bad parts. &amp;nbsp;Then run the bad part through the logic to generate the "good" values and then merge them together.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This can sometimes be done in a single data step. &amp;nbsp;For example if you just wanted to implement a last observation carried forward method.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or perhaps it basically just involves a lookup of valid values from another table. &amp;nbsp;Which you can do with a merge, join, or even using a SAS format.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 31 May 2017 20:44:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-retain-a-value-from-a-separate-dataset-within-a-data-step/m-p/363219#M85952</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-05-31T20:44:58Z</dc:date>
    </item>
    <item>
      <title>Re: How to retain a value from a separate dataset within a data step?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-retain-a-value-from-a-separate-dataset-within-a-data-step/m-p/363430#M86065</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/124208"&gt;@Financier&lt;/a&gt;: I am not quite sure why you want to create a dataset containing the "ultimately good value" and not just create a macro variable or a function to get it.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For instance, you can create a function with PROC FCMP:&lt;/P&gt;&lt;PRE&gt;proc fcmp outlib=work.funcs.test;
  function ultimately_good_value();
    return(1);
    endsub;
run;

options cmplib=work.funcs;

data values_want;
  set values; 
  retain true_value .;
  if message = "new_good" then true_value=value;
  if message = "new_bad" then 
  true_value = ultimately_good_value();
run;&lt;/PRE&gt;&lt;P&gt;Or, even simpler, put the value in a macro variable:&lt;/P&gt;&lt;PRE&gt;%let ultimately_good_value=1;

data values_want;
  set values; 
  retain true_value .;
  if message = "new_good" then true_value=value;
  if message = "new_bad" then 
  true_value = &amp;amp;ultimately_good_value.;
run;&lt;/PRE&gt;&lt;P&gt;But if, for some obscure reason, you have to invoke a datastep and use that, you can do it with DOSUBL():&lt;/P&gt;&lt;PRE&gt;data values_want;
  set values; 
  retain true_value .;
  if message = "new_good" then true_value=value;
  else if message = "new_bad" then do;
    _RC=dosubl('Data ultimately_good_value;x=1;run;');
    _dsid=open('ultimately_good_value');
    _RC=fetch(_dsid);
    true_value = getvarn(_dsid,varnum(_dsid,'x'));
    _RC=close(_dsid);
    end;
  drop _:;
run;&lt;/PRE&gt;&lt;P&gt;Only remember to call close(_dsid); otherwise the next call to DOSUBL will fail.&lt;/P&gt;&lt;P&gt;If you only need the ultimately_good_value data set created once, you can check if it exists:&lt;/P&gt;&lt;PRE&gt;proc delete data=ultimately_good_value;run;

data values_want;
  set values; 
  retain true_value .;
  if message = "new_good" then true_value=value;
  else if message = "new_bad" then do;
    if exist('ultimately_good_value')=0 then 
      _RC=dosubl('Data ultimately_good_value;x=1;run;');
    _dsid=open('ultimately_good_value');
    _RC=fetch(_dsid);
    true_value = getvarn(_dsid,varnum(_dsid,'x'));
    _RC=close(_dsid);
    end;
  drop _:;
run;&lt;/PRE&gt;&lt;P&gt;I put the PROC DELETE in in case this was not the first time the code was run.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Søren&lt;/P&gt;</description>
      <pubDate>Thu, 01 Jun 2017 13:45:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-retain-a-value-from-a-separate-dataset-within-a-data-step/m-p/363430#M86065</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2017-06-01T13:45:49Z</dc:date>
    </item>
    <item>
      <title>Re: How to retain a value from a separate dataset within a data step?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-retain-a-value-from-a-separate-dataset-within-a-data-step/m-p/363618#M86129</link>
      <description>&lt;P&gt;Thank you all for valuable&amp;nbsp;suggestions. I modified the first code by&amp;nbsp;Søren, so it actually does the trick. It puts the data step with a retain statement on hold, starts another data step, gets the value created through this auxiliary&amp;nbsp;data step and sends it to the&amp;nbsp;main data step without interruption of the retain statement.&amp;nbsp;Using DOSUBL may be faster.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is the code:&lt;/P&gt;&lt;PRE&gt;data values;
infile datalines; 
input message $ value;
datalines;                      
new_good 0.5
retain   .
new_good 0.7
retain   .
retain   .
new_bad  0.6
retain   .
new_good 0.3
retain   .
retain   .
retain   .
;
run;


data ultimately_good_value;
x=1;
run;


%macro data_step();

data temp;
set ultimately_good_value;
call symput('macro_x',x);
run;

%mend data_step;

proc fcmp outlib=work.funcs.test;
function getval();
rc = run_macro('data_step', macro_x ); 
return (macro_x);
endsub;
quit;
options cmplib=work.funcs;


data values_want;
set values; 

retain true_value .;

if message = "new_good" then true_value=value;
if message = "new_bad" then 
true_value = getval() ;

run;

&lt;/PRE&gt;&lt;P&gt;The dataset ultimately_good_value could also be created within the data_step macro.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Sorry for not giving out all the details. I can't create a macro variable before the data step because in my&amp;nbsp;original code&amp;nbsp;I do not know&amp;nbsp;what x is going to be equal to.&amp;nbsp;Every time&amp;nbsp;x is determined based on&amp;nbsp;several retained values, message flag and some other variables from a&amp;nbsp;different dataset. This created a viscious circle: I do not know&amp;nbsp;where to look for x in each case&amp;nbsp;before I&amp;nbsp;run a data step with a&amp;nbsp;retain statement, but if I start this data step, I do not know how&amp;nbsp;to fetch x once I learn where it is (and future values of x&amp;nbsp;depend on the previous ones).&lt;/P&gt;</description>
      <pubDate>Thu, 01 Jun 2017 21:40:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-retain-a-value-from-a-separate-dataset-within-a-data-step/m-p/363618#M86129</guid>
      <dc:creator>Financier</dc:creator>
      <dc:date>2017-06-01T21:40:40Z</dc:date>
    </item>
  </channel>
</rss>

