<?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: Using retain to create two variables at the same time? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Using-retain-to-create-two-variables-at-the-same-time/m-p/628187#M185572</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/314263"&gt;@Alppen&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can do this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
	set test1;
	if _N_=1 then do;
		b = 100;
		c= a*b;
	end;
	else do;
		retain c;
		b+(-c);
		c= a*b;
	end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 28 Feb 2020 10:56:22 GMT</pubDate>
    <dc:creator>ed_sas_member</dc:creator>
    <dc:date>2020-02-28T10:56:22Z</dc:date>
    <item>
      <title>Using retain to create two variables at the same time?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-retain-to-create-two-variables-at-the-same-time/m-p/628184#M185569</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a given variable which I want to use to dynamically create two new variables. For context, this is related to calculating life tables from mortality data. Here's the starting point:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data test1;
input a;
datalines;
0.1
0.2
0.1
0.2
;
run;&lt;/PRE&gt;&lt;P&gt;The dataset I want to create is one where variable b starts at an arbitrary number (here 100), c is defined as a*b, and all the following values for b are lag(b)-lag(c), as follows:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data want;
input a b c;
0.1 100 10
0.2 90 18
0.1 72 7.2
0.2 64.8 12.96
;
run;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tried using retain and lag, but something is off as the assignment of c uses a from the same row and b from the previous row, whereas I would like it to use b from the same row as well. So here's my effort that doesn't work as intended:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data test2;
set test;
retain b 100;
c = a*b;
lag_c = lag(c);
if lag_c ne . then b = b - lag_c;
run;&lt;/PRE&gt;&lt;P&gt;The solution and my mistake is probably pretty basic, but having mostly an R background these functionalities are still somewhat unintuitive, so any help is welcome!&lt;/P&gt;</description>
      <pubDate>Fri, 28 Feb 2020 10:39:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-retain-to-create-two-variables-at-the-same-time/m-p/628184#M185569</guid>
      <dc:creator>Alppen</dc:creator>
      <dc:date>2020-02-28T10:39:11Z</dc:date>
    </item>
    <item>
      <title>Re: Using retain to create two variables at the same time?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-retain-to-create-two-variables-at-the-same-time/m-p/628185#M185570</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/314263"&gt;@Alppen&lt;/a&gt;&amp;nbsp;and welcome to the SAS Support Communities!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can compute b after an OUTPUT statement and thus omit the LAG function:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set test1;
retain b 100;
c=a*b;
output;
b=b-c;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 28 Feb 2020 10:54:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-retain-to-create-two-variables-at-the-same-time/m-p/628185#M185570</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2020-02-28T10:54:10Z</dc:date>
    </item>
    <item>
      <title>Re: Using retain to create two variables at the same time?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-retain-to-create-two-variables-at-the-same-time/m-p/628186#M185571</link>
      <description>&lt;P&gt;You don't need lag at all, retaining b and an output-statement is all you need:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test2;
   set test1;

   retain b 100;

   c = a * b;

   output;

   b = b - c;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Edit: i really should have Firefox reload a page before starting to write an answer. &lt;/P&gt;</description>
      <pubDate>Fri, 28 Feb 2020 10:57:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-retain-to-create-two-variables-at-the-same-time/m-p/628186#M185571</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2020-02-28T10:57:30Z</dc:date>
    </item>
    <item>
      <title>Re: Using retain to create two variables at the same time?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-retain-to-create-two-variables-at-the-same-time/m-p/628187#M185572</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/314263"&gt;@Alppen&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can do this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
	set test1;
	if _N_=1 then do;
		b = 100;
		c= a*b;
	end;
	else do;
		retain c;
		b+(-c);
		c= a*b;
	end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 28 Feb 2020 10:56:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-retain-to-create-two-variables-at-the-same-time/m-p/628187#M185572</guid>
      <dc:creator>ed_sas_member</dc:creator>
      <dc:date>2020-02-28T10:56:22Z</dc:date>
    </item>
    <item>
      <title>Re: Using retain to create two variables at the same time?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-retain-to-create-two-variables-at-the-same-time/m-p/628188#M185573</link>
      <description>Wow, that was fast, thanks!</description>
      <pubDate>Fri, 28 Feb 2020 11:11:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-retain-to-create-two-variables-at-the-same-time/m-p/628188#M185573</guid>
      <dc:creator>Alppen</dc:creator>
      <dc:date>2020-02-28T11:11:24Z</dc:date>
    </item>
  </channel>
</rss>

